Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
136 views

C++ Queue Program

This document implements a queue using an array data structure in C++. It defines functions for pushing elements onto the queue, popping elements off the queue, traversing the queue to display its contents, and exiting the program. The code includes a menu loop that allows the user to select these options and demonstrates pushing multiple elements onto the queue, popping elements off, and traversing the remaining contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

C++ Queue Program

This document implements a queue using an array data structure in C++. It defines functions for pushing elements onto the queue, popping elements off the queue, traversing the queue to display its contents, and exiting the program. The code includes a menu loop that allows the user to select these options and demonstrates pushing multiple elements onto the queue, popping elements off, and traversing the remaining contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//Implementation of a queue as an array

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{const int max=10;
int ch,num,front=-1,rear=-1;
int queue[max];
clrscr();
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the number:";
cin>>num;
if(rear==max-1)
cout<<"\n\aQueue overflow";
else
{
if(front==-1)
{
front++;
queue[front]=num;
rear=front;
}
else
{
rear++;
queue[rear]=num;
}
}
break;
case 2:
if(front==-1)
cout<<"\nQueue does not exist";
else if(rear==front)
{
num=queue[front];
front=-1;
rear=-1;
cout<<"\nDeleted element:"<<num;
}
else
{
num=queue[front];
front++;
cout<<"\nDeleted Element:"<<num;
}
break;
case 3:
if(front==-1)
cout<<"\nQueue does not exist";
else
for(int i=front;i<=rear;i++)

cout<<queue[i]<<" ";
break;
case 4:
getch();
exit(0);
default:
cout<<"Wrong Choice!Please Re-enter:";
}
goto menu;
}
//OUTPUT
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter the number:42
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter the number:24
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter the number:33
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter the number:22
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:3
42 24 33 22
Menu
1.Push
2.Pop
3.Traverse
4.Exit

Enter your choice:2


Deleted Element:42
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:3
24 33 22
Menu
1.Push
2.Pop
3.Traverse
4.Exit
Enter your choice:4

You might also like