Queue Data Structure Implementation in C++, Exercises of Data Structures and Algorithms

The implementation of a queue data structure using c++. The user can perform dequeue, enqueue, display, and exit operations on the queue. The program checks for queue underflow and overflow conditions before performing the respective operations.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<conio.h>
#include<iostream.h>
int rear=-1,front=-1,queue[10],max=10;
void main (void)
{
int choice,input;
clrscr();
cout<<"Enter one of the following options\n\n\t--> To Dequeue enter:
1\n\t--> To enqueue enter: 2\n\t--> To display enter: 3\n\t--> To exit
enter: 4\n";
cin>>choice;
while (choice!=4)
{
switch (choice)
{
case 1:
if(front==-1 || rear==-1)
{
cout<<"The queue is empty\n";
}
else
{
for (int i=front;i<rear;i++)
{
queue[i]=queue[i+1];
}
rear--;
}
break;
case 2:
if (rear==(max-1))
{
cout<<"The queue is full\n";
}
else
{
if (front==-1)
{
front=0;
}
cout<<"Enter the number you want to enqueue: ";
cin>>input;
rear++;
queue[rear]=input;
}
break;
case 3:
cout<<"\n";
for(int i=0;i<=rear;i++)
{
cout<<queue[i]<<" ";
}cout<<"\n";
break;
default:
docsity.com
pf2

Partial preview of the text

Download Queue Data Structure Implementation in C++ and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include<conio.h> #include<iostream.h> int rear=-1,front=-1,queue[10],max=10; void main (void) { int choice,input; clrscr(); cout<<"Enter one of the following options\n\n\t--> To Dequeue enter: 1\n\t--> To enqueue enter: 2\n\t--> To display enter: 3\n\t--> To exit enter: 4\n"; cin>>choice; while (choice!=4) { switch (choice) { case 1: if(front==-1 || rear==-1) { cout<<"The queue is empty\n"; } else { for (int i=front;i<rear;i++) { queue[i]=queue[i+1]; } rear--; } break; case 2: if (rear==(max-1)) { cout<<"The queue is full\n"; } else { if (front==-1) { front=0; } cout<<"Enter the number you want to enqueue: "; cin>>input; rear++; queue[rear]=input; } break; case 3: cout<<"\n"; for(int i=0;i<=rear;i++) { cout<<queue[i]<<" "; }cout<<"\n"; break; default:

docsity.com

cout<<"You entered the wrong choice please enter again\n"; } cout<<"\nEnter choice : "; cin>>choice; } getch(); }

docsity.com