QUEUE using SLL
#include<iostream.h>
#include<conio.h>
template<class t>
struct sll
{
t data;
struct sll * next;
};
typedef struct sll<t> node;
node * r,*f;
class Q
{
public:
node * temp;
Q()
{
f=r=NULL;
}
void insert();
void remove();
void display();
};
template<class t>
void Q<t>::insert()
{
cout<<"\nEnter data to be insert : ";
if(f==NULL)
{
f=new node;
cin>>f->data;
r=f;
f->next=NULL;
}
else
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
r->next=temp;
r=temp;
}
}
template<class t>
void Q<t>::display()
{
cout<<"\nData of Queue : ";
if(f==NULL)
{
cout<<"\nEmpty Queue ";
}
else
{
temp=f;
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
temp=temp->next;
}
}
}
template<class t>
void Q<t>::remove()
{
if(f==NULL)
{
cout<<"\nNot possible ";
}
else
{
cout<<"\ndata removed : "<<f->data;
f=f->next;
if(f==NULL)
r=NULL;
}
}
void main()
{
Q <int>q;
int ch;
clrscr();
do
{
cout<<"\n1.Insert \n2.Display \n3.Delete\nexit";
cout<<"\nEnter your choice no : ";
cin>>ch;
switch(ch)
{
case 1:
q.insert();
break;
case 2:
q.display();
break;
case 3:
q.remove();
break;
}
}while(ch!=4);
getch();
}
/*
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 1
Enter data to be insert : 12
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 1
Enter data to be insert : 34
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 1
Enter data to be insert : 67
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 2
Data of Queue : 12 34 67
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 3
data removed : 12
1.Insert
2.Display
3.Delete
exit
Enter your choice no : 4
*/
0 comments:
Post a Comment