Wednesday, 5 June 2013

Simple Queue program in Data Structure



#include<iostream.h>
#include<conio.h>
class queue
{
private:
int data;
queue *prev;
queue *next;
public:
void add(int);
void display(void);
void remove(void);
};
queue *front;
queue *rear;
void queue::add(intnum)
{
if((front==NULL)&&(rear==NULL))
{
front=rear=new queue;
front->data=num;
front->next=NULL;
front->prev=NULL;
}
else
{
rear->next=new queue;
rear->next->data=num;
rear->next->next=NULL;
rear->next->prev=rear;
rear=rear->next;
}}
void queue::display(void)
{
queue *q=front;
if((front==NULL)&&(rear==NULL))
  {
cout<<"Queue is empty";
  }
else
  {
while(q!=NULL)
    {
cout<<q->data<<endl;
      q=q->next;

    }}}
void queue::remove(void)
{
queue *q=front;
if((front==NULL)&&(rear==NULL))
   {
cout<<"Queue is empty"<<endl;
   }
else
   {

front=front->next;
front->prev=NULL;
cout<<q->data<<"/t"<<"is deleted"<<endl;
delete(q);
   }}
void main()
  {
clrscr();
queue q1;
intnum;
int option;
char choice='y';
while(choice=='y')
   {
cout<<"1.ADD"<<endl;
cout<<"2.DISPLAY"<<endl;
cout<<"3.REMOVE"<<endl;
cout<<"Select the option";
cin>>option;
switch(option)
     {
case 1:
       {
cout<<"enter the data";
cin>>num;
q1.add(num);
cout<<"Continue?"<<endl;
break;
       }
case 2:
       {
                q1.display();
                cout<<endl;
                cout<<"Continue?"<<endl;
                break;  }
case 3:
       {
                q1.remove();
                cout<<endl;
                cout<<"Continue?";
                break;}
default:
                {
                cout<<"wrong option";
                cout<<"Continue";
                break;
                 }}
                choice=getch();
     }   }

No comments:

Post a Comment