Sunday, August 15, 2010

c++ Program list4

Program list
1. queue with link list
2. Program for bubble sort
3. Circular list with the delete option
queue with link list

#include<iostream>

using namespace std;


template<class T>
class node
{
public:
T data;
node<T> *next;
};

template<class T>
class queue
{
public:
node<T> *front;
node<T> *rear;
queue();
void add(T data);
int remove();
friend ostream& operator<< <T>(ostream&a,queue<T>& qu);
};

template<class T>
queue<T>::queue()
{
front =NULL;
rear =NULL;
}

template<class T>
void queue<T>::add(T data)
{
node<T> *p =new node<T>;
p->data =data;
p->next =NULL;
if(rear==NULL)
front=rear=p;
else
{
rear->next=p;
rear=rear->next;
}
}
template<class T>
int queue<T>::remove()
{
if(front==NULL)
cout<<"queue is empty"<<endl;
else
{
p=front;
if(front->next!=NULL)
front=front->next;
return p->no;
}
}
template<class T>
ostream& operator<< (ostream& o,queue<T>&q)
{
node<T>* last =q.rear;
while(last!=q.front)
{
o<<q.front->data;
q.front =q.front->next;
}
}

int main()
{
queue<int>a;
for(int i=0;i<5;i++)
a.add(i);
cout<<a;
}

/*

* Program for bubble sort
* */
#include<iostream>

using namespace std;

void bubblesort(int a[],int n)
{
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int a[5];
cout<<"enter data"<<endl;
for(int i=0;i<5;i++)
cin>>a[i];
bubblesort(a,5);
for(int i=0;i<5;i++)
cout<<a[i]<<" "<<;
return 0;
}

/*

* Circular list with the delete option
*1. delete before
*2. delete after
*/
#include<iostream>

using namespace std;

template<class T>
class node
{
public:
T data;
node *next;
};

template<class T>
class link_list
{
public:
link_list();
void create_link_list_list(T data);
void delete_after(T data);
void delete_before(T data);
node<T> *first;
template<typename T2>
friend ostream& operator<<(ostream&o,link_list<T2> &li);
};

template<class T>
link_list<T>::link_list()
{
first =NULL;
}
template<class T>
void link_list<T>::create_link_list_list(T data)
{
node<T> *last;
node<T> *p =new node<T>;
p->data =data;
if(first==NULL)
{
first=p;
p->next =first;
}
else
{
last =first;
while(last->next!=first)
last =last->next;
p->next =first;
last->next =p;
}
}
template<class T>
void link_list<T>::delete_after(T data)
{
node<T> *last =first;
if(first==NULL)
cout<<" first node is empty"<<endl;
else
{
node<T> *p;
do
{
if(last->data ==data)
{
p=last->next;
last->next =p->next;
if(first==p)
first=first->next;
}
last=last->next;
}while(last!=first);
}
}

template<class T>
void link_list<T>::delete_before(T data)
{
node<T> *last =first;
node<T> *p;
if(first==NULL)
cout<<" first node is empty"<<endl;
else
{
do
{
if(last->next->next->data ==data)
{
p=last->next;
last->next =p->next;
}
last=last->next;
}while(last!=first);
}
if(last->next->next->data ==data)
{
p=first;
p->next =first;
last->next =first;
}
}
template<class T>
ostream& operator<<(ostream &o,link_list<T>& ll)
{node<T> *last =ll.first;
do
{
o<<" --->"<<last->data;
last=last->next;
}while(last!=ll.first);
return o;
}

int main()
{
link_list<int> c;
for(int i =1;i<9;i++)
c.create_link_list_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. delete after node"<<endl;
\cout<<" 3. delter before node"<<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_list_list(number);
cout<<c;
break;
case 2:
int delete_number;
cout<<"enter the data which you want to delete"<<endl;
cin>>delete_number;
c.delete_after(delete_number);
cout<<c;

break;
case 3:
int delete_d;
cout<<"enter the data number2 which we delter before"<<endl;
cin>>delete_d;
c.delete_before(delete_d);
cout<<c;
break;
default:
break;
}
}while(choice ==2 choice == choice ==3);

}