Sunday, August 15, 2010

C++ Program list6

The Program list
1. The Program for the doubly circular
2. The Program for hcf & lcm
3. Program for quick sort 
4. Stack Operation


/*

* The Program for the doubly circular
* */
#include<iostream>


using namespace std;
template<class T>
class node
{
public:
T data;
node<T> *next;
node<T> *pre;
};
template<class T>
class link_1
{
public:
link_1();
void create_link_1_list(T data);
void add_after(T data);
void add_before(T data);
void delete_after(T data);
void delete_before(T data);
node<T> *first;
template<typename T2>
friend ostream& operator<< (ostream &o,link_1<T2> li);
};

template<class T>
link_1<T>::link_1()
{
first=NULL;
}

template<class T>
void link_1<T>::create_link_1_list(T data)
{
node<T> *p =new node<T>;
p->data =data;

if(first==NULL)
{
first=p;
p->next=first;
p->pre=first;
}
else
{
node<T> *last =first;
while(last->next!=first)
last =last->next;

p->next =last->next;
p->pre =last;
last->next->pre =p;
last->next =p;
last=p;
}
}

template<class T>
void link_1<T>::add_after(T data)
{
node<T> *p =new node<T>;
p->data =data;
node<T> *last = first;
if(first==NULL)
{
cout<<"your list is empty"<<endl;
}
else if(first!=NULL)
{while(last->next!=first)
{
if(last->data ==data)
{p->next =last->next;
p->pre =last;
last->next->pre =p;
last->next =p;
last=p;
}
last=last->next;
cout<<" abhi"<<endl;
}
}
else
{
if(last->data ==data)
{p->next =first;
p->pre =last;
last->next->pre =p;
last->next =p;
}
}
}

template<class T>
void link_1<T>::add_before(T data)
{
node<T> *last =first;
node<T> *p =new node<T>;
if(first==NULL)
cout<<" it is null"<<endl;
else if(first->data ==data && first->next ==first)
{
p->next =first;
first->next =p;
first->pre =p;
p->pre =first;

first =p;
}
else
{
do
{
if(last->next->data ==data)
{p->next =last->next;
p->pre =last;

last->next->pre =p;
last->next =p;
}
last =last->next;
}while(last!=first);
}
}

template<class T>
void link_1<T>::delete_after(T data)
{
node<T> *p;
if(first==NULL)
cout<<" link list is empty"<<endl;
else if(first->data ==data)
cout<<" you have one single node"<<endl;

else
{
node<T> *last =first;
do
{
if(last->data ==data)
{
p=last->next;
p->pre =last;

if(p==first)
first=first->next;
last->next =p->next;
last =p->next->pre;
}
last =last->next;
}while(last!=first);
}
}

template<class T>
void link_1<T>::delete_before(T data)
{
node<T> *last;
node<T> *p;
if(first==NULL)
cout<<" link list is empty"<<endl;
else
{
last=first;
while(last->next!=first)
{
if(last->data ==data)
{
p=last->pre;
last->pre = p->pre;
p->pre->next =p->next;
}
last =last->next;
}
}
}

template<typename T2>
ostream& operator<<(ostream &o,link_1<T2> li)
{
node<T2> *last =li.first;
do
{
o<<" --->"<<last->data;
last =last->next;
}while(last!=li.first);

}
int main()
{
link_1<int> c;
for(int i=0;i<5;i++)
c.create_link_1_list(i);
cout<<c;
int choice;
do
{
cout<<"enter your choice between 1 or 2"<<endl;
cout<<" 1. add data into list"<<endl;
cout<<" 2. add after node"<<endl;
cout<<" 3. add before node"<<endl;
cout<<" 4. delete before"<<endl;
cout<<" 5. delete after "<<endl;
cout<<c;
cout<<endl<<endl;
cin>>choice;
switch(choice)
{
case 1:
int number;
cout<<"enter the data which you want to add"<<endl;
cin>>number;
c.create_link_1_list(number);
cout<<c;
break;
case 2:
int delete_number;
cout<<"enter the data which you want to add after"<<endl;
cin>>delete_number;
c.add_after(delete_number);
cout<<c;
break;
case 3:
int delete_d;
cout<<"enter the data number2 which we add before"<<endl;
cin>>delete_d;
c.add_before(delete_d);
cout<<c;
break;
case 4:
int add_d;
cout<<" enter the data which after you want to delete "<<endl;
cin>>add_d;
c.delete_after(add_d);
cout<<c;
break;
case 5:
int add_b;
cout<<" enter the data which before you want to delete"<<endl;
cin>>add_b;
c.delete_before(add_b);
cout<<c;
break;
}
}while(choice ==1 choice ==2 choice ==choice==4 choice ==5);
return 0;
}

/*

* The Program for hcf & lcm
* */


#include<iostream>
using namespace std;
int hcf(int a,int b)
{
if(a==b)
return b;
else if(a>b)
hcf(a,a-b);
else
hcf(a-b,a);
}

int lcm(int a,int b)
{
if(a==b)
return b;
else if(a<b)
lcm(a+b,a);
else
lcm(a,a+b);
}
int main()
{
int a;
cout<<"enter a value"<<endl;
cin>>a;
int b;
cout<<"enter b value"<<endl;
cin>>b;
cout<<hcf(a,b);
cout<<lcm(a,b);
return 0;
}

/*

* Program for quick sort
* */
#include<iostream>
using namespace std;
void quicksort(int a[],int first,int last)
{
int temp;
int low =first;
int high =last;

int pivot = a[(first+last)/2];
do
{
while(a[low]<pivot)
low++;
while(a[high]>pivot)
high--;
if(low<=high)
{
temp=a[low];
a[low]=a[high];
low++;
a[high]=temp;
high--;
}
}while(low<=high);

if(low<last)
quicksort(a,low,last);

if(first<high)
quicksort(a,first,high);
}

int main()
{
int a[20];
for(int i=0;i<19;i++)
a[i]=i;
quicksort(a,1,19);
cout<<a;
return 0;
}

*

* Stack Operation
* Push
* Pop
* Peep
* */

#include<iostream>
using namespace std;
#define size 5
using namespace std;
int top =-1;
int a[5];
void push(int no)
{
if(top==size-1)
cout<<"stack is full"<<endl;
else
a[++top]=no;
}
int pop(int no)
{
if(top==-1)
cout<<"stack is empty"<<endl;
else
return a[top--];
}
void peep()
{
if(top==-1)
cout<<"stack is empty"<<endl;
else
cout<<a[top];
}
int main()
{
cout<<pop(3);
push(5);
peep();
return 0;
}