TheAlgorithms-C-Plus-Plus/data_structure/Queue Using Array.cpp

76 lines
865 B
C++
Raw Normal View History

2019-08-21 10:10:08 +08:00
#include <iostream>
2017-12-24 01:30:49 +08:00
using namespace std;
int queue[10];
2019-08-21 10:10:08 +08:00
int front = 0;
int rear = 0;
2017-12-24 01:30:49 +08:00
void Enque(int x)
{
2019-08-21 10:10:08 +08:00
if (rear == 10)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cout << "\nOverflow";
2017-12-24 01:30:49 +08:00
}
else
{
2019-08-21 10:10:08 +08:00
queue[rear++] = x;
2017-12-24 01:30:49 +08:00
}
}
void Deque()
{
2019-08-21 10:10:08 +08:00
if (front == rear)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cout << "\nUnderflow";
2017-12-24 01:30:49 +08:00
}
2019-08-21 10:10:08 +08:00
2017-12-24 01:30:49 +08:00
else
{
2019-08-21 10:10:08 +08:00
cout << "\n"
<< queue[front++] << " deleted";
2017-12-24 01:30:49 +08:00
for (int i = front; i < rear; i++)
{
2019-08-21 10:10:08 +08:00
queue[i - front] = queue[i];
2017-12-24 01:30:49 +08:00
}
2019-08-21 10:10:08 +08:00
rear = rear - front;
front = 0;
2017-12-24 01:30:49 +08:00
}
}
void show()
{
for (int i = front; i < rear; i++)
{
2019-08-21 10:10:08 +08:00
cout << queue[i] << "\t";
2017-12-24 01:30:49 +08:00
}
}
int main()
{
int ch, x;
do
{
2019-08-21 10:10:08 +08:00
cout << "\n1. Enque";
cout << "\n2. Deque";
cout << "\n3. Print";
cout << "\nEnter Your Choice : ";
cin >> ch;
if (ch == 1)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cout << "\nInsert : ";
cin >> x;
2017-12-24 01:30:49 +08:00
Enque(x);
}
2019-08-21 10:10:08 +08:00
else if (ch == 2)
2017-12-24 01:30:49 +08:00
{
Deque();
}
2019-08-21 10:10:08 +08:00
else if (ch == 3)
2017-12-24 01:30:49 +08:00
{
show();
}
2019-08-21 10:10:08 +08:00
} while (ch != 0);
2017-12-24 01:30:49 +08:00
return 0;
}