Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #define max_size 10
- int a[max_size];
- int front = -1;
- int rear = -1;
- bool IsFull()
- {
- return((rear + 1) % max_size == front);
- }
- bool IsEmpty()
- {
- return(front == -1 && rear == -1);
- }
- void Enqueue(int x)
- {
- if (IsFull())
- {
- return;
- }
- else if (IsEmpty())
- {
- front = rear = 0;
- }
- else
- {
- rear = (rear + 1) % max_size;
- }
- a[rear] = x;
- }
- void Dequeue()
- {
- if (IsEmpty())
- {
- return;
- }
- else if (front == rear)
- {
- front = rear = -1;
- }
- else
- {
- front = (front + 1) % max_size;
- }
- }
- int Front()
- {
- return a[front];
- }
- void Print()
- {
- int count = (rear + max_size - front) % max_size + 1;
- for (int i = 0; i < count; i++)
- {
- int index = (front + i) % max_size;
- cout << a[index] << " ";
- }
- cout << "\n\n";
- }
- int main()
- {
- Enqueue(2); Print();
- Enqueue(4); Print();
- Enqueue(6); Print();
- Dequeue(); Print();
- Enqueue(8); Print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement