TheAlgorithms-C-Plus-Plus/data_structure/queue_using_array2.cpp

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