Stacks and queues
Stacks and queues
Items are pushed onto the top of the stack when they are added to it and
popped off the top when they are deleted from it.
Can only add or remove from top or bottom not in between. Stacks are
LIFO, last in first out.
89. OCR A Level (H046-H446) SLR14 – 1.4 Data structures part 3 – Stacks
& queues – Craig 'n' Dave knowledge video index (animation of stack
throughout program)
Stacks are used for:
Items are enqueued at the back of the queue and dequeued from the
front of the queue.
Back pointer – points to the last item (sometimes called tail pointer).
Front pointer - points to the first item (sometimes called head pointer).
process scheduling
transferring data between processors and printer spooling
performing breadth-first searches on graph data structures
How to add to stacks
1. Check for stack underflow. Output an error if the stack pointer does
not point to a node
2. Output the node pointed to by the stack pointer.
3. Set the stack pointer to the previous item.
1. Check for queue underflow. Output an error if the front pointer does
not point to a node.
2. Output the node pointed to by the front pointer.
3. Set the front pointer to the previous item.
Use peek to look at the top or front item to see what it is without
altering it
Repeatedly pop or dequeue items following the same process as
removing an item to output the contents