Data Structures and Algorithms: Ex. No. 2
Data Structures and Algorithms: Ex. No. 2
Aim
To demonstrate the array implementation of Queue.
Algorithm
Step 1: Start the program.
Step 2: Create a class Queue with data members, member functions and constructor.
Step 5:In a Dequeue function check ((top == -1) && (rear == -1) .
Step 6: In display function, by the usage of for loop display all the elements.
Step 8: By using the switch case access the required functions by user choice.
Program
#include<iostream>
using namespace std;
class Queue
{
private:
int soq,top,rear,q[100];
public:
Queue(int s)
{
top=-1;
soq=s;
rear=-1;
q[soq+1];
}
void enqueue(int x)
{
if((top==-1)&&(rear==-1))
{
top=0;
rear=0;
q[rear]=x;
}
else if((rear+1)%soq==top)
{
cout<<"Queue is full";
}
else
{
rear=(rear+1)%soq;
q[rear]=x;
}
}
int dequeue()
{
if((top==-1)&&(rear==-1))
{
cout<<"Can't Dequeue because queue is Empty";
return 0;
}
else
{
cout<<"Dequeued element is :";
cout<<q[top];
if(rear==top)
{
rear=-1;
top=-1;
}
else
{
top=(top+1)%soq;
}
}
}
void display()
{
int temp;
if((top==-1)&&(rear==-1))
{
cout<<"Queue is empty";
}
else
{
temp=top;
do
{
cout<<"\n Element in the queue are :";
cout<<q[temp];
temp=(temp+1)%soq;
}
while(temp!=(rear+1)%soq);
}
}
};
int main()
{
int x,s,co;
int y,a;
cout<<"\n Enter the size of Queue \n";
cin>>s;
Queue abc(s);
while(1)
{
cout<<"\n Press 1: Enqueue";
cout<<"\n Press 2: Dequeue";
cout<<"\n Press 3: Display";
cout<<"\n Press 4: Exit \n";
cin>>x;
switch(x)
{
case 1:
cout<<"Enter an element to Enqueue";
cin>>y;
abc.enqueue(y);
break;
case 2:
a=abc.dequeue();
cout<<"Dequeued element is :"<<a;
break;
case 3:
abc.display();
break;
case 4:
co=10;
break;
default:
cout<<"\n Enter the correct choice \n";
}
if(co==10)
{
break;
}
}
return 0;
}
Aim
To demonstrate the conversion of infix to postfix notation using Stack operations.
Algorithm
Step 1: Start the program.
Step 3: In a function enqueue ((rear == size of queue -1) && (top == 0) || (rear == top -1)).
Step 4: By checking the above condition we can do enqueue operation.
Step 5: In a function Dequeue ((top == -1), By this condition we can do the Dequeue of
elements in Queue.
Step 6: Display the elements of queue by using checking if((top == 1) && (rear == -1)), we can
display them.
Program
#include<iostream>
using namespace std;
class Queue
{
private:
int soq,top,rear,q[100];
public:
Queue(int s)
{
top=-1;
soq=s;
rear=-1;
q[soq+1];
}
void enqueue(int x)
{
if((top==-1)&&(rear==-1))
{
top=0;
rear=0;
q[rear]=x;
}
else if((rear+1)%soq==top)
{
cout<<"Queue is full";
}
else
{
rear=(rear+1)%soq;
q[rear]=x;
}
}
int dequeue()
{
if((top==-1)&&(rear==-1))
{
cout<<"Can't Dequeue because queue is Empty";
return 0;
}
else
{
cout<<"Dequeued element is :"<< q[top]<<endl;
if(rear==top)
{
rear=-1;
top=-1;
}
else
{
top=(top+1)%soq;
}
}
}
void display()
{
int temp;
if((top==-1)&&(rear==-1))
{
cout<<"Queue is empty";
}
else
{
temp=top;
do
{
cout<<"\n Element in the circular queue are :";
cout<<q[temp];
temp=(temp+1)%soq;
}
while(temp!=(rear+1)%soq);
}
}
};
int main()
{
int x,s,co;
int y,a;
cout<<"\n Enter the size of Queue \n";
cin>>s;
Queue abc(s);
while(1)
{
cout<<"\n Press 1: Enqueue";
cout<<"\n Press 2: Dequeue";
cout<<"\n Press 3: Display";
cout<<"\n Press 4: Exit \n";
cin>>x;
switch(x)
{
case 1:
cout<<"Enter an element to Enqueue";
cin>>y;
abc.enqueue(y);
break;
case 2:
a=abc.dequeue();
cout<<"Dequeued element is :"<<a;
break;
case 3:
abc.display();
break;
case 4:
co=10;
break;
default:
cout<<"\n Enter the correct choice \n";
}
if(co==10)
{
break;
}
}
return 0;
}
Input and Output
Result
The above programs of array implementation of Queue and CircularQueue are implemented
successfully and verified.
Video link:
https://drive.google.com/drive/u/0/folders/13FUOcxurgAdx6JIWjAfFVX19B
TF5YDMX