Basic Operations on a Queue
Queue using Arrays
Insert an Element
- ALGORITHM
Step 1: if rear = max - 1
Write Overflow
Go to step 4
[END OF if]
Step 2: if front = -1 and rear = -1
SET front = rear = 0
else
SET rear = rear + 1
[END OF if]
Step 3: SET queue[rear] = val
Step 4: EXIT
- CODE
Delete an Element
- ALGORITHM
Step 1: if front = -1 or front > rear
Write Underflow
else
SET val = Queue[front]
SET front = front + 1
[END OF if]
Step 2: EXIT
- CODE
Peek Operation
- ALGORITHM
Step 1: if front = -1 or front > rear
Write Underflow
else
return Queue[front]
[END OF if]
Step 2: EXIT
- CODE
Queue using Linked List
Insert an Element
- ALGORITHM
Step 1: Allocate memory for the new node and name it newNode
Step 2: SET newNode->data = val
Step 3: if front = NULL
SET front = rear = newNode
SET front->next = rear->next = NULL
else
SET rear->next = newNode
SET rear = newNode
SET rear->next = NULL
[END OF if]
Step 4: EXIT
- CODE