Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

CSCI2100E: Data Structures

Lecture 5: Stacks and Queues

Danny Yip
Department of Information Engineering
Outline
1. Stack (LIFO)
○ Abstract Data Type
○ Basic Operations - Push, Pop, Top
○ Array Implementation, Linked List Implementation
○ Applications
2. Queue (FIFO)
○ Abstract Data Type
○ Basic Operations - Enqueue, Dequeue
○ Circular Array Implementation, Linked List Implementation
○ Applications

CSCI2100E Data Structures | Spring 2021 2


Stack Abstract Data Type
● A stack is a list that can only insert and delete in only one position - top
● A stack is last-in, first-out list (LIFO)
● Push: insert to top
● Pop: delete from top
● Top: check the top without popping it

Image Credit. Dr. Matthew Tang


CSCI2100E Data Structures | Spring 2021 original push(A3) pop() 3
Example: Stack Abstract Data Type
Operation Stack Return value

CSCI2100E Data Structures | Spring 2021 4


Stack: Array Implementation
● Pros: Less pointer manipulations
● Cons: Need to declare an array size ahead of time

● Key Idea:
○ (variable) is defined to be -1 when the stack is empty
○ - increase and assign to
○ - decrease
○ - return

CSCI2100E Data Structures | Spring 2021 5


Stack: Array Implementation - Declaration

A0 A1 A2 A3
A0 A1 A2

A0 A1 6
CSCI2100E Data Structures | Spring 2021
Stack: Array Implementation - Init, Clear

CSCI2100E Data Structures | Spring 2021 7


Stack: Array Implementation - Push

A0 A1 A2 A0 A1 A2 A3
CSCI2100E Data Structures | Spring 2021 8
Stack: Array Implementation - Top, Pop

A0 A1 A2 A0 A1
CSCI2100E Data Structures | Spring 2021 9
Stack: Array Implementation - topandpop, Free

CSCI2100E Data Structures | Spring 2021 10


Stack: Linked List Implementation
● Pros: The size of stack is flexible
● Cons: Pointer manipulations may be a problem to program beginner

● Key Idea:
○ Keep a node for easy coding
○ - insert the element to the front
○ - delete the element at the front
○ - return the element at the front

CSCI2100E Data Structures | Spring 2021 11


Stack: Linked List Implementation - Declaration

Note: stores

A0 A1 A2 A3
A0 A1 A2

A0 A1 12
CSCI2100E Data Structures | Spring 2021
Stack: Linked List Implementation - Init, Clear

CSCI2100E Data Structures | Spring 2021 13


Stack: Linked List Implementation - Push

A0 A1 A2 A0 A1 A2 A3

CSCI2100E Data Structures | Spring 2021 14


Stack: Linked List Implementation - Pop, Top
Note: stack_topandpop(),
stack_free() are left for
your work.

A0 A1 A2

A0 A1

CSCI2100E Data Structures | Spring 2021 15


Application of Stack: Balancing Symbols
● Compilers check for syntax errors, but frequently a lack of one symbol will
cause it to spill out hundreds of lines of warning/errors.
● A useful tool is to check whether everything is balanced: every brace, bracket
and parenthesis
e.g. ( [ ] ) is legal but not [ ( ] )
● We can use a stack to help checking:
○ If a character is an opening symbol, push it onto the stack
○ If it is a closing, pop the stack and check if it matches to top

CSCI2100E Data Structures | Spring 2021 16


Application of Stack: Postfix Evaluation
● We can also use stacks to evaluate arithmetic expressions.
● For instance, we need to find the value of a simple arithmetic expression with
multiplications and additions of integers.

● This involves saving intermediate results.

● The following three expressions are exactly the same


○ Infix:
○ Prefix:
○ Postfix:

CSCI2100E Data Structures | Spring 2021 17


Queue Abstract Data Type
● A queue is a list that insert at the end (called the rear/tail) and delete at the
start (front/head)
● A queue is first in, first out (FIFO)
● Enqueue(put): inserts an element at the end
● Dequeue(get): deletes (and returns) the element at the start

Image Credit. Dr. Matthew Tang


CSCI2100E Data Structures | Spring 2021 18
Example: Queue Abstract Data Type
Operation Queue Return value

CSCI2100E Data Structures | Spring 2021 19


Queue: Implementation
● Array Implementation: Less pointer manipulations
● Linked List Implementation: Flexible size

● Key Idea:
○ - append x to the rear of the queue
○ - delete the front of the queue, and return its value

CSCI2100E Data Structures | Spring 2021 20


Queue: Array Implementation with Circular Array
● Motivation
○ After a number of enqueue and dequeue, the queue appears to be full
● Solution
○ wrap around front and rear whenever it gets to the end of the array
○ In C, we may use the modulus operator (%)

Image Credit. Dr. Matthew Tang


CSCI2100E Data Structures | Spring 2021 21
Queue: Implementation - Declaration

CSCI2100E Data Structures | Spring 2021 22


Queue: Implementation - Init

CSCI2100E Data Structures | Spring 2021 23


Queue: Implementation - Is Empty

CSCI2100E Data Structures | Spring 2021 24


Queue: Implementation - Enqueue

CSCI2100E Data Structures | Spring 2021 25


Queue: Implementation - Dequeue

CSCI2100E Data Structures | Spring 2021 26


Queue: Other Operations
1. Make the queue empty
2. Free the queue
3. Print the queue
4. Check whether the queue is full or not (Circular array implementation)

CSCI2100E Data Structures | Spring 2021 27


Real Life Applications of Queue
● Printer queues
● Process queues
● Customer service ticket systems
● Anything that requires FIFO (First In, First Out)

CSCI2100E Data Structures | Spring 2021 28

You might also like