You are on page 1of 18

Stack and Queue Data Structures

Stack and Queue Data Structures


• Both are lists of items that are retrieved in
different orders
• Stack is “Last in First Out” or LIFO because
when you remove an item you get the most
recently added item.
• Queue is “First in First Out” or FIFO because
when you remove an item you get the item
that has been waiting the longest.
Stack Operations
• Push(item) – Adds the item to the ‘top’ of the stack.
• Pop() – Removes the item at the top of the stack and
returns it.
• IsEmpty() – Returns True if there are no items in the
stack.
Optional Operations:
• Top() – Returns the item at the top of the stack without
removing it from the stack, sometimes called Peek().
• IsFull() – If the stack has been implemented with a size
limit, this method returns True when the stack is full.
Stack Uses
• Remembering Partially completed tasks
– Calculator Registers
– Computer Program Function Calls
• Undoing or Backtracking completed actions
– Undo function in Word. All edits are added to a
stack and the undo removes the last edit and
reverses it
– Artificial Intelligence search algorithms sometimes
explore different paths and can use a Stack to
backtrack if a path is found to be a bad one.
Stack Example
• Create Empty Stack with room for 3 items, Count = 0
_____
_____
_____

• Push item on stack with push(56); Count = 1


_____
_____
__56_

• Push another item on stack with push(123); Count = 2


_____
__123
___56

• Pop an item from stack with pop() which will return 123, Count = 1
_____
_____
___56

• Push 2 items with calls to push(45); followed by push(87); Count = 3 stack full
___87
___45
___56

• Three calls to pop(); return 87, 45, and 56 in order, and empty the stack
Stack Implementations
• Think about the requirements:
– Be able to save a list of items in some kind of
ordered structure
– Be able to retrieve the last item added while
keeping track of the new top of the stack and
preserving the order of the stack contents
– Be able to tell if there are no items in the stack
– Is there a requirement to have a stack of unlimited
size? This may affect what is the best
implementation.
Stack Implentations II
• Arrays are a natural ordered list of items
– Maintain an index variable that holds the array
index for the next item to add to the stack
– Push adds the item at this index position
– The index == 0 when the stack is empty
– Pop gets the item at (index – 1) and reduces index
by 1 to point at the next position as the top.
– Stack is limited in size unless we can dynamically
increase the array size
Queue Operations
• Add(item) – Adds an item to the Queue at the end
(called the tail of the queue). Also called Enqueue.
• Remove() – Removes the item at the front of the queue
(called the head of the queue) and returns it. Also
called Dequeue. Returns NULL if empty.
Optional Operations:
• Top() – Returns the next item in the queue without
removing it.
• IsEmpty() – Returns true if the queue is empty.
• Count() – Returns the number of queued items
Queue Uses
• First come first served resource allocation
– Line at the Registrar’s office. We would not be
happy if this used a stack instead of a queue.
• Queues can also be “Round Robin” where an
item is removed, serviced, and then placed
back in the queue at the end.
– Sharing of CPU time among Computer Processes
– Taking turns in a game amongst all the players
Queue Example
• Create an empty Queue = { }
• Add an item to the Queue with Add(45); Queue = { 45}
• Add another item to the Queue with Add(89); Queue = { 45, 89}
• Add another item to the Queue with Add(23); Queue = { 45, 89, 23}
• Remove an item for servicing with Remove(); returning 45,
Queue = { 89, 23}
• Add another item to the Queue with Add(90); Queue = { 89, 23, 90}
• Remove an item for servicing with Remove(); returning 89,
Queue = { 23, 90}
• Add 32, 78, 102 to with three calls to Add(n); Queue = {23, 90, 32, 78, 102}
• Remove two items from queue with two calls to Remove(); returning 23
and 90, Final Queue = { 32, 78, 102}
Queue Implementations
• Again think about the requirements
– Keep an ordered arrangement of items
– Be able to identify the item that has been in the
queue the longest, the head of the queue
– Be able to identify the end or tail of the queue
where new items will be added
– Be able to remove the head of the queue and
change the head index to the next in line
Queue Implementations II
• Arrays are also good ordered structures for Queues
– Keep an index variable that contains the index position of
the head item in the queue
– Keep another index variable that contains the index of the
last item as the tail (head and tail could be same item)
– Empty Queue uses -1 as head & tail markers
– Queue is full when head = ((tail +1) % length)
– When Head and Tail positions get to the end of the array,
they will move to the beginning again
– The Queue size is limited to the size of the array unless we
can dynamically increase the array size
Array Limitations
• Arrays are fixed size blocks of sequential
memory locations.
• If we need to store more items than the array
can hold, we need to create a new larger array
in memory and copy all the items
• If we need to insert an item in the middle of
ordered items, we need to move items to
create a space in the proper position
Linked Lists
• Linked Lists are chains of items in different memory
locations that use a pointer to the next item to
maintain order of the items
• You can start at the first item and follow the pointers to
sequentially access all items in order
• Because memory is created for each new item, the
linked list size can grow with the only limitation being
available computer memory
• Insertion of new items is done by changing pointers to
include the new item in the proper order without
having to copy items to create space as in an array
Singly Linked List
• List of ‘nodes’ containing data items
• Each node contains a data item and a
reference to the next node
• The linked list stores a reference to the first
node in the list as the head of the list
• The rest of the nodes can be accessed by
following the path of next references
• The last node has NULL for its next reference
Singly Linked List
Linked List Operations
• Add(item) – Adds an item to the end of the
list
• Remove(item) – Removes an item from the list
• Find(item) – Finds an item in the list and
returns it.
No great discovery was ever made without
a bold guess – Unknown author