C++ program to implement queue operations using array
Aim:
To write a program to implement queue operations using array.
Algorithm:
Insert ( ):
QUEUE (FRONT, QUEUE, N, REAR, ITEM)
Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE. ITEM is the value to be inserted.
1. [Check for overflow]
If (REAR == N) Then
2. Print: Overflow
3. Else
4. [Check if QUEUE is empty]
If (FRONT and REAR == 0) Then
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1
[End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted
[End of Step 1 If]
9. Exit
Delete ( ):
QUEUE (FRONT, QUEUE, N, REAR, ITEM)
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE.
- [Check for underflow]
If (FRONT == 0) Then
- Print: Underflow
-
Else
-
ITEM = QUEUE[FRONT]
-
[Check if only one element is left]
If (FRONT == REAR) Then
(a) Set FRONT = 0
(b) Set REAR = 0
- Else
-
[Increment FRONT by 1]
Set FRONT = FRONT + 1
[End of Step 5 If]
- Print: ITEM deleted
[End of Step 1 If]
- Exit