Wednesday, 5 June 2013

Double-Ended Queue in Data Structure

#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
voidaddrear(int);
void display(void);
voidremovefront(void);
voidaddfront(int);
voidremoverear(void);
};
squeue *front;
squeue *rear;
voidsqueue::addrear(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
 {
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
 }
else
 {
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=NULL;
rear=rear->next;
 }}
voidsqueue::addfront(intnum)
{
squeue *temp=front;
if(front==NULL && rear==NULL)
 {
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
 }
else
 {
front=new squeue();
front->data=num;
front->prev=NULL;
front->next=temp;
temp->prev=front;
 }}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{
while(q!=NULL)
{
cout<<q->data<<"\t";
q=q->next;
}}}
voidsqueue::removefront()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is removed\n";
delete(temp);
display();
}
voidsqueue::removerear()
{
squeue *temp=rear;
rear=rear->prev;
rear->next=NULL;
cout<<endl<<temp->data<<" is removed\n";
delete(temp);
display();
}
void main(){
clrscr();
squeue s;
char c='y';
intopt,num;
while(c=='y')
{
cout<<"\n 1.add R \n 2. display  \n 3. remove F \n 4. add F \n 5. Remove R \n enter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.addrear(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.removefront();
cout<<"\ndo you want to continue:";
break;
case 4:
cout<<"enter number in queue:";
cin>>num;
s.addfront(num);
cout<<"\ndo you want to continue:";
break;
case 5:
s.removerear();
cout<<"\ndo you want to continue:";
break;
default:
cout<<"wrng selection";
cout<<"\ndo you want to continue:";
break;}
c=getch();
}
getch();
}

Circular Queue program in Data Structure



#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
void add(int);
void display(void);
void remove(void);
};
squeue *front;
squeue *rear;
voidsqueue::add(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
 {
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
 }
else
 {
temp=front;
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=front;
rear=rear->next;
front->prev=rear;
 }}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{do
{
cout<<q->data<<"\t";
q=q->next;
}
while(q!=rear->next);
}}
voidsqueue::remove()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is removed\n";
delete(temp);
display();
}
void main()
{
clrscr();
squeue s;
char c='y';
intopt,num;
while(c=='y')
{
cout<<"\n 1.add \n 2. display  \nenter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.add(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.remove();
cout<<"\ndo you want to continue:";
break;
default:
cout<<"wrng selection";
cout<<"\ndo you want to continue:";
break;
}c=getch();
}getch();}

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();
     }   }

Stack program in Data Structure



#include<iostream.h>
#include<conio.h>
class stack
{
int data;
stack *next,*prev;
public:
void push(int);
int pop();
void display();
};
stack *bottom,*top;

/* push function*/
void stack::push(int num)
{
if((bottom==NULL)&&(top==NULL))
{
top=bottom=new stack;
top->data=num;
top->next=NULL;
top->prev=NULL;
}
else
{
top->next=new stack;
top->next->data=num;
top->next->prev=top;
top->next->next=NULL;
top=top->next;
}
}
/* display  function*/

 void stack:: display()
{       
 stack *q=bottom;
 if(bottom==NULL)
 {
 cout<<"\n\nStack is empty";
 }
 else {
 while(q!=NULL)
{
cout<<"\t "<<q->data;
q=q->next;
}
}
}
/*pop  function*/
int stack::pop()
{     int result;
if((bottom==NULL)&&(top==NULL))
{
return -1;
}
result=top->data;
top=top->prev;

if(top!=NULL)
{
delete(top->next);
top->next=NULL;
return result;
}
else
{
delete(bottom);
bottom=NULL;
return result;
}
}
/*main()  function*/
void main()
{
stack n1;
int ch;
char c;
do
{    
clrscr();
cout<<"\n 1: PUSH  2: DISPLAY  3: POP";
cout<<"\n\n Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\n Enter Any number: " ;
int num;
cin>>num;
n1.push(num);
break;
case 2:
n1.display();
break;
case 3:
int n=n1.pop();
if(n==-1)
{
cout<<"\n\n Stack is empty";
}
else
{
cout<<"\n\n Popped Item is: "<<n;
}
break;
default:
cout<<"\n wrong selection";
break;
}
cout<<"\n\n Do you want to continue(y/n): ";
cin>>c;
}
while(c=='y');
}