#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
int array[15];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void enqueue(int);
void dequeue();
void print();
};
void queue::print()
{
if(front <=rear)
{
for(int i=front;i<=rear;i++)
{
cout<<endl<<array[i];
}
}
else
cout<<"\n queue is empty ......";
}
void queue::dequeue()
{
if(rear < front)
{
rear=-1;
front=-1;
cout<<"\n queue is empty ......";
}
else
{
cout<<array[front]<<" is dequeue out form the queue";
front++;
}
}
void queue::enqueue(int data)
{
if(rear<15)
{
rear=rear+1;
array[rear]=data;
if(front<0) front=0;
}
else
cout<<"\n\t queue Full";
}
void main()
{
clrscr();
queue s;
int count,data;
char choice,option;
do
{
clrscr();
cout<<"\n\t Select Operation \n";
cout<<"\n\t1. To Create the queue";
cout<<"\n\t2. To Display the queue";
cout<<"\n\t3. To Add the data in the queue";
cout<<"\n\t4. To Delete the data from the queue";
cout<<"\n\t0. Exit";
cout<<"\n\n\t Enter your choice:- ";
cin>>option;
switch(option)
{
case '1':
{
cout<<"\n \t How many item you want to added in the queue : ";
cin>>count;
for(int i=0;i<count;i++)
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
}
break;
}
case '2':
{
cout<<"\n \t Data in the queue";
s.print();
break;
}
case '3':
{
cout<<"\n \t Enter data which is need to be added in the queue: ";
cin>>data;
s.enqueue(data);
break;
}
case '4':
{
s.dequeue();
break;
}
case '0':
{
exit(0);
break;
}
default:
{
cout<<"\n \tInvalid Option";
break;
}
}
cout<<"\n\n \tDo you want to continue (y/n):- ";
cin>>choice;
}while(choice!='n');
getch();
}
Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts
Wednesday, 5 June 2013
Priority Queue in Data Structure
#include<iostream.h>
#include<conio.h>
class pqueue
{
pqueue *pre;
int data;
int pri;
int num,prio;
pqueue *next;
void add(int,int);
int remove(void);
public :
void display(void);
void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{
if((front==NULL) && (rear==NULL))
{
front=rear= new pqueue;
rear->next=NULL;
rear->data=num;
rear->pri=pri;
rear->pre=NULL;
}
else
{
rear->next= new pqueue;
rear->next->pre= rear;
rear->next->data = num;
rear->next->next=NULL;
rear->next->pri=pri;
rear = rear->next;
}
}
void pqueue :: display(void)
{
pqueue *q = front;
if(front==NULL && rear== NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(q !=NULL)
{
cout<<"Numbers are:"<<endl<< q->data<<endl;
q = q->next;
}
}
}
int pqueue :: remove (void)
{
int result;
// queue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"Queue is empty"<<endl;
return -32767;
}
result = front->data;
front = front->next;
front->pre = NULL;
if(front != NULL)
{
delete (rear->next);
rear ->next = NULL;
}
else
{
delete (front);
front=NULL;
}
return result;
}
void pqueue :: priority(void)
{
if(front==NULL && rear == NULL)
{
cout<<"Queue is Empty" <<endl;
char ch='y';
cout<<"Do you want to add new?"<<endl;
cin>>ch;
while(ch=='y')
{
cout<<"Enter the data "<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
break;
}
}
else
{
if( rear->pri >= front->pri)
{
cout<<"Enter The data"<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
}
else
{
int r= remove();
cout<<"Removed = "<<r<<endl;
}
}
}
void main()
{
clrscr();
int num;
char choice='y';
pqueue q;
front=rear=NULL;
int option;
while(choice == 'y')
{
cout<<"1.Priority operation"<<endl;
cout<<"2.Display"<<endl;
cout<<"enter the option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
q.priority();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 2:
{
q.display();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 5:
{
int r = q.remove();
cout<<"Removed ="<<r<<endl;
cout<<"Do you want to cnt....?"<<endl;
break;
} */
default:
{
cout<<"Wrong input"<<endl;
cout<<"Do you want to cont...?"<<endl;
break;
}
}
choice=getch();
}
}
#include<conio.h>
class pqueue
{
pqueue *pre;
int data;
int pri;
int num,prio;
pqueue *next;
void add(int,int);
int remove(void);
public :
void display(void);
void priority(void);
};
pqueue *front;
pqueue *rear;
void pqueue :: add(int num,int pri)
{
if((front==NULL) && (rear==NULL))
{
front=rear= new pqueue;
rear->next=NULL;
rear->data=num;
rear->pri=pri;
rear->pre=NULL;
}
else
{
rear->next= new pqueue;
rear->next->pre= rear;
rear->next->data = num;
rear->next->next=NULL;
rear->next->pri=pri;
rear = rear->next;
}
}
void pqueue :: display(void)
{
pqueue *q = front;
if(front==NULL && rear== NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(q !=NULL)
{
cout<<"Numbers are:"<<endl<< q->data<<endl;
q = q->next;
}
}
}
int pqueue :: remove (void)
{
int result;
// queue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"Queue is empty"<<endl;
return -32767;
}
result = front->data;
front = front->next;
front->pre = NULL;
if(front != NULL)
{
delete (rear->next);
rear ->next = NULL;
}
else
{
delete (front);
front=NULL;
}
return result;
}
void pqueue :: priority(void)
{
if(front==NULL && rear == NULL)
{
cout<<"Queue is Empty" <<endl;
char ch='y';
cout<<"Do you want to add new?"<<endl;
cin>>ch;
while(ch=='y')
{
cout<<"Enter the data "<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
break;
}
}
else
{
if( rear->pri >= front->pri)
{
cout<<"Enter The data"<<endl;
cin>>data;
cout<<"Enter the priority"<<endl;
cin>>pri;
add(data,pri);
}
else
{
int r= remove();
cout<<"Removed = "<<r<<endl;
}
}
}
void main()
{
clrscr();
int num;
char choice='y';
pqueue q;
front=rear=NULL;
int option;
while(choice == 'y')
{
cout<<"1.Priority operation"<<endl;
cout<<"2.Display"<<endl;
cout<<"enter the option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
q.priority();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 2:
{
q.display();
cout<<"Do you want to cont...?"<<endl;
break;
}
case 5:
{
int r = q.remove();
cout<<"Removed ="<<r<<endl;
cout<<"Do you want to cnt....?"<<endl;
break;
} */
default:
{
cout<<"Wrong input"<<endl;
cout<<"Do you want to cont...?"<<endl;
break;
}
}
choice=getch();
}
}
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');
}
Subscribe to:
Posts (Atom)