Download www.revision-notes.co.cc

Transcript
-9

Insertions are made to the rear of the queue and deletions are made from the front of the
queue
First item in becomes the first item to be out (FIFO)
A queue needs two pointers because adding data to the queue is done at the rear of the queue
and deleting an item from the queue is done at the front of the queue. The Head Pointer (HP)
points to the front of the queue and the Tail Pointer (TP) points to the rear of the queue.
Algorithms involving Stacks and Queues
A problem that can occur when a program tries to insert a new value to a stack or a queue
which is represented as an array is encountering the situation where the array is full. The stack
pointer helps to recognize this situation by pointing to the outside of the array.
A problem that can occur when a program tries to read a value from a stack or a queue which is
represented as an array is encountering the situation where the array is empty. The stack
pointer helps to recognize this situation by pointing to the null value.
Algorithm 4: Insertion of an item to a stack (when the stack is represented by an array)
Memory addresses increase in this direction
Data
Stack Pointer (points to the top of the stack)
Data
Data
Data
IF stack is full
DISPLAY message “Stack is full.”
STOP
ELSE
Increment stack pointer by 1
Store the new data at the location pointed to by the stack pointer
STOP
ENDIF
Page 36 of 115
www.revision-notes.co.cc