Tuesday 24 February 2015

Queue Using C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class QueueDemo
{
private:
struct Queue
{
int Data;
struct Queue *Next;
}*Front,*Rear;

public:
QueueDemo()
{
Front=NULL;
Rear=NULL;
}
Push()
{
Queue *Source=new Queue;

cout<<"\n Enter Data For Queue\n";
cin>>Source->Data;
Source->Next=NULL;

if(Front==NULL)
{
Front=new Queue;
Rear=new Queue;
Front=Rear=Source;
}
else
{
Rear->Next=Source;
Rear=Source;
}
}
Pop()
{
Queue *Source=new Queue;
Source=Front;

if(Source==NULL)
{
cout<<"\n Queue Is Empty";
}
else
{
cout<<"\n Poped Item is       :"<<Source->Data;
Front=Front->Next;
delete Source;
}
getch();
}
Display()
{
Queue *Source=new Queue;
Source=Front;

while(Source!=NULL)
{
cout<<"\n Queue Item is  :"<<Source->Data;
Source=Source->Next;
}
getch();
}
void ChoiceGet()
{
int choice;
Home:
clrscr();
cout<<"\n\t\t\t Queue Demo \n";
cout<<"\n\t\t\t1.. Push";
cout<<"\n\t\t\t2.. Pop";
cout<<"\n\t\t\t3.. Display";
cout<<"\n\t\t\t4.. Exit";
cout<<"\n\n\t\t Enter Your Choice\n";
cin>>choice;
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
cout<<"\n Wrong Press";
}
goto Home;
}
};
void main()
{
clrscr();
QueueDemo QD;
QD.ChoiceGet();
getch();
}

No comments:

Post a Comment