0% found this document useful (0 votes)
35 views12 pages

Data Structures and Algorithms: Ex. No. 2

The document describes programs to implement a queue and circular queue using an array. It includes the aim, algorithms, program code, inputs, outputs, and results. The queue and circular queue programs are implemented using classes with member functions for enqueue, dequeue, and display. The programs use arrays, pointers, and loops to add and remove elements from the queues. The output shows the programs successfully verify the array implementations of a queue and circular queue.

Uploaded by

grace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

Data Structures and Algorithms: Ex. No. 2

The document describes programs to implement a queue and circular queue using an array. It includes the aim, algorithms, program code, inputs, outputs, and results. The queue and circular queue programs are implemented using classes with member functions for enqueue, dequeue, and display. The programs use arrays, pointers, and loops to add and remove elements from the queues. The output shows the programs successfully verify the array implementations of a queue and circular queue.

Uploaded by

grace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DATA STRUCTURES AND ALGORITHMS URK17CS106

Ex. No. 2 IMPLEMENTATION OF QUEUE AND CIRCULAR QUEUE USING ARRAY

Date of Exercise 15-12-2018 Mark (5)

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 3: In a function Enqueue

If((top == -1) && (rear == -1))

Step 4: Make top =0 and rear =0, q[rear] =x .

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 7: In main function create an object, and declare required variables.

Step 8: By using the switch case access the required functions by user choice.

Step 9: stop the program.

Program

#include<iostream>
using namespace std;
class Queue
{
private:
int soq,top,rear,q[100];
public:
Queue(int s)

Ex. No. 1 |abstract class and interface 1


DATA STRUCTURES AND ALGORITHMS URK17CS106

{
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

Ex. No. 1 |abstract class and interface 2


DATA STRUCTURES AND ALGORITHMS URK17CS106

{
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)
{

Ex. No. 1 |abstract class and interface 3


DATA STRUCTURES AND ALGORITHMS URK17CS106

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

Ex. No. 1 |abstract class and interface 4


DATA STRUCTURES AND ALGORITHMS URK17CS106

Input and Output

Ex. No. 1 |abstract class and interface 5


DATA STRUCTURES AND ALGORITHMS URK17CS106

Ex. No. 1 |abstract class and interface 6


DATA STRUCTURES AND ALGORITHMS URK17CS106

Aim
To demonstrate the conversion of infix to postfix notation using Stack operations.

Algorithm
Step 1: Start the program.

Step 2: Create a class CircularQueue with data members and constructors.

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.

Step 7: stop the program.

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))
{

Ex. No. 1 |abstract class and interface 7


DATA STRUCTURES AND ALGORITHMS URK17CS106

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))
{

Ex. No. 1 |abstract class and interface 8


DATA STRUCTURES AND ALGORITHMS URK17CS106

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:

Ex. No. 1 |abstract class and interface 9


DATA STRUCTURES AND ALGORITHMS URK17CS106

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

Ex. No. 1 |abstract class and interface 1


0
DATA STRUCTURES AND ALGORITHMS URK17CS106

Result
The above programs of array implementation of Queue and CircularQueue are implemented
successfully and verified.

Ex. No. 1 |abstract class and interface 1


1
DATA STRUCTURES AND ALGORITHMS URK17CS106

Video link:
https://drive.google.com/drive/u/0/folders/13FUOcxurgAdx6JIWjAfFVX19B
TF5YDMX

Ex. No. 1 |abstract class and interface 1


2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy