0% found this document useful (0 votes)
3 views

Stacks and queues

Stacks and queues are essential data structures in computing, with stacks operating on a last in, first out (LIFO) basis and queues on a first in, first out (FIFO) basis. Stacks are used for program flow control, recursion, and depth-first searches, while queues are utilized for process scheduling and breadth-first searches. Both structures have specific methods for adding and removing items, including handling overflow and underflow conditions.

Uploaded by

scouts.amy07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Stacks and queues

Stacks and queues are essential data structures in computing, with stacks operating on a last in, first out (LIFO) basis and queues on a first in, first out (FIFO) basis. Stacks are used for program flow control, recursion, and depth-first searches, while queues are utilized for process scheduling and breadth-first searches. Both structures have specific methods for adding and removing items, including handling overflow and underflow conditions.

Uploaded by

scouts.amy07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Stacks and queues

Stack = a data structure that is essential for the operation of a computer

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.

You can peek at the top item without deleting it.

Can only add or remove from top or bottom not in between. Stacks are
LIFO, last in first out.

Stack pointer – points to the node at the top.

Stack Overflow = any attempt to push an item onto a full stack

Stack Underflow = any attempt to pop an item off an empty stack

Stacks are used by processors to track the flow of programs, when a


procedure/function is called, it changes the value of the program counter
to the first instruction of the subroutine. When the subroutine ends, the
processor must return the program counter to its previous value.

Stacks allow subroutine calls to be nested (recursion).

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:

 performing depth-first searched on graph data structures.

 keeping track of user inputs for undo operations


 backtracking algorithms, e.g. pathfinding maze solutions
 evaluating mathematical expressions without brackets, using a
shunting yard algorithm and reverse Polish notation
Queue

Queue = a linear data structure

Items are enqueued at the back of the queue and dequeued from the
front of the queue.

You can peek at the front item without deleting it.

By nature, a queue system allows people to jump the queue, in computing


this is called a priority queue (in special circumstances new items can join
at the front of the queue).

Queues are FIFO, first in first out.

Back pointer – points to the last item (sometimes called tail pointer).

Front pointer - points to the first item (sometimes called head pointer).

Queue overflow = an attempt to enqueue an item in a full queue

Queue underflow = an attempt to dequeue an item from an empty queue


An array with a circular queue is ideal if you want to restrict the number of
items and have a known memory footprint.

Example) Useful in game design where the number of sprites on the


screen affects the framerate, by only spawning sprites up to the limit of
the queue a stable frame rate can be achieved.

Queues are used for:

 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 overflow. Output an error if the stack is full.


2. Increment the stack pointer.
3. Insert the new data item at the location pointed to by the stack
pointer.

How to add to queues

1. Check for queue overflow. Output an error if the queue is full.


2. Increment the back/tail pointer.
3. Insert the new data item at the location pointed to by the
back/tail pointer.

How to add to queues (OOP)

1. Check for queue overflow. Output an error if no free memory is


available
2. Create a new node and insert data into it
3. The back pointer is set to point to the new nodes
4. If this is the first node in the list, the front pointer is set to point to
the new node

Removing an item from a stack

1. Check for stack underflow. Output an error if the stack is empty.


2. Copy/output the data item pointed to by the pointer.
3. Decrement the pointer.

Removing an item from a stack (OOP)

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.

Removing an item from a queue

1. Check for queue underflow. Output an error if the queue is empty.


2. Copy/output the data item pointed to by the front/head pointer.
3. Increment the front/head pointer.

Removing an item from a queue (OOP)

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.

Traversing a stack or queue

 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

You might also like