mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-08 04:59:21 +08:00
Stack Queue and Circular Queue
This commit is contained in:
77
Circular Queue Using Array.cpp
Normal file
77
Circular Queue Using Array.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int queue[10];
|
||||
int front=0;
|
||||
int rear=0;
|
||||
int count=0;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if(count==10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear]=x;
|
||||
rear=(rear+1)%10;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (front==rear)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<queue[front]<<" deleted";
|
||||
front=(front+1)%10;
|
||||
count--;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i<count; i++)
|
||||
{
|
||||
cout<<queue[(i+front)%10]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Push";
|
||||
cout<<"\n2. Pop";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user