Author: Sanif S S
Site: theeduzone.blogspot.com
Email: sanifss@gmail.com
#include<stdio.h>
#include<conio.h>
#define max 3
class queue
{
int item[max];
int front;
int rear;
public:
void insert_rear();
void delete_front();
void insert_front();
void delete_rear();
void display();
queue()
{
rear=-1;
front=-1;
}
};
void queue::insert_rear()
{
int value;
if(rear==max-1)
{
cout<<"Queue is full insertion not possible\n";
}
else
{
cout<<"\nEnter the value to be inserted\n";
cin>>value;
rear=rear+1;
item[rear]=value;
if(front==-1)
{ front=0;}
}
}
void queue::delete_front()
{
int value;
if(front==-1)
{
cout<<"Queue empty\n ";
}
else
{
value=item[front];
cout<<"deleted item is"<
if(front==rear)
front=rear=-1;
else
front=front+1;
}
}
void queue::insert_front()
{
int value;
if(front==0)
{
cout<<"Queue is full\n";
}
else
{
if(front==-1)
{
front=0 ; }
front=front-1;
cout<<"Enter the element to insert\n";
cin>>value;
item[front]=value;
}
}
void queue::delete_rear()
{
int value;
if(front<0)
cout<<"Queue is empty\n";
else
{
value=item[rear];
if(front==rear)
{
front=rear=-1;
}
else
rear=rear-1;
}
cout<<"deleted element is "<
}
void queue::display()
{
if(front==-1&&rear==-1)
{
cout<<"queue is empty\n" ;
}
else
{
cout<<"Elements in queue are\n";
for(int i=front;i<=rear;i++)
{
cout<
}
getch();
}
}
void main()
{
int choice,c;
char ch;
queue q;
clrscr();
do
{
cout<<" MENU\n";
cout<<"1.Input restricted queue\n\n" ;
cout<<"2.Output restricted queue\n\n";
cout<<"3.Display\n";
cout<<"nter your choice\n";
cin>>choice;
switch(choice)
{
case 1 :cout<<" menu\n";
cout<<"1.Insertion at rear\n";
cout<<"2.Deletion from front\n";
cout<<"3.Deletion from rear\n";
cout<<"Enter your choice\n";
cin>>c;
switch(c)
{
case 1:q.insert_rear();break;
case 2:q.delete_front();break;
case 3:q.delete_rear();break;
default :cout<<"Invalid choice\n";break;
}
break;
case 2 :cout<<" menu \n";
cout<<"1.Insertion at rear\n";
cout<<"2.Insertion at front\n";
cout<<"3.Deletion from front\n";
cout<<"Enter your choice\n";
cin>>c;
switch(c)
{
case 1:q.insert_rear();break;
case 2:q.insert_front();break;
case 3:q.delete_front();break;
default:cout<<"Invalid choice\n";break;
}
break;
case 3:q.display();break;
default:cout<<"Invalid choice\n"; break;
}
cout<<"\nDo you want to continue(yes=y,no=n)";
cin>> ch;
}while(ch=='Y'||ch=='y');
getch();
}
No comments:
Post a Comment