CSCI2100E: Data Structures Lecture 5: Stacks and Queues: Danny Yip Department of Information Engineering
CSCI2100E: Data Structures Lecture 5: Stacks and Queues: Danny Yip Department of Information Engineering
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
● Key Idea:
○ (variable) is defined to be -1 when the stack is empty
○ - increase and assign to
○ - decrease
○ - return
A0 A1 A2 A3
A0 A1 A2
A0 A1 6
CSCI2100E Data Structures | Spring 2021
Stack: Array Implementation - Init, Clear
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
● 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
Note: stores
A0 A1 A2 A3
A0 A1 A2
A0 A1 12
CSCI2100E Data Structures | Spring 2021
Stack: Linked List Implementation - Init, Clear
A0 A1 A2 A0 A1 A2 A3
A0 A1 A2
A0 A1
● Key Idea:
○ - append x to the rear of the queue
○ - delete the front of the queue, and return its value