Linked List 2
Linked List 2
Lecture 4
Abstract Data Types (ADT): Stacks,
and Queue
(Chapter 3 of textbook 1)
1
4/9/2024
Polynomial (2)
struct Node {
int coef;
int exp;
struct Node* next;
};
2
4/9/2024
p 3 10 15
1200 50 0
4x100+30x50+5
q 4 30 5
100 50 0
r
3 4 40 20
1200 100 50 0
3
4/9/2024
2 3 11 7
NULL
NULL
c d e f
4
4/9/2024
Multi Lists
Advantage:
Saving space
Disadvantage:
Expense of time
5
4/9/2024
Stack ADT
Stack is a list where you can access add or delete elements at one end.
Stacks are called ``last in first out’’ (LIFO), the last added
element among the current ones can be accessed or deleted.
Operations of Stack:
Stack CreateStack ()
int isEmpty (Stack S)
ElementType Pop (Stack S) -> Delete the element at top of a stack
void Push(Stack S, ElementType X) -> Add an element X at top of
a stack
ElementType Top(Stack S) -> Access the element at top of a stack
EXAMPLE
1
3 3
5 5 5
Push 3 Push 1
Push 5
Get 1 Get 3 7
3 5
5 5
6
4/9/2024
Stack Implementation
Array
Example
Push 5, 3, 1, Pop, Pop, Push 7
1 5
2 5 3
3 5 3 1
2 5 3
1 5 25 7
7
4/9/2024
L Header 5 NULL
L Header 3 5 NULL
L Header 1 3 5 NULL
8
4/9/2024
L Header 3 5 NULL
L Header 5 NULL
L Header 7 5 NULL
9
4/9/2024
Stack Application
Function Calls
Mathematical Expression
Symbol Matching
Evaluation of Postfix Expressions
Infix to Postfix Conversion
10
4/9/2024
11
4/9/2024
EXAMPLE
Check brace, bracket parentheses matching [a+b{1*2]9*1}+(2-1)
{
[ [
Push {
Push [
[
Pop Expect?
12
4/9/2024
EXAMPLE
Check brace, bracket parentheses matching [a+b{1*2}9*1]+(2-1)
{
[ [ [
Push { Pop
Push [ Perfect! Expect {
Get (
Get [
(
Pop Expect [ Push ( Pop Expect (
Symbol Priority
A*B + C
A*(B + C)
A+B+C*D
A*D+B+C*E
13
4/9/2024
Postfix
A*B + C = AB*C+
A+B+C*D = AB+CD*+
A*D+B+C*E = AD*B+CE*+
Computation of a Postfix
Expression
Whenever you see a number push it in the stack.
Whenever you see an operator,
pop 2 numbers,
apply the operator,
Push the result
At end Pop to get answer
Complexity? O(n)
14
4/9/2024
EXAMPLE
Compute 5*4+6+7*2 Result: 40
Postfix: 5 4*6+7 2*+
Get 4
4
5 5 5
Push 4 Pop
Push 5
Get 5
6
20 20
Get 6 Get 20
20 26
Get 2
2
7 7 7
26 26 26
Push 7 Push 2 Pop
Get 7
Get 14
14 26
26
26 Pop
Push 14
Pop
15
4/9/2024
Get 26
40
Pop Push 40
Example: a + b*c + ( d *e + f ) *g
a b c * + d e * f +g * +
16
4/9/2024
Queues ADT
List where an element is inserted at the end, and deleted
from the beginning. First in First out
Insertion and deletion at different ends. Deletion and
Insertion should be O(1)
Some terminologies:
Dequeue = Pop = Delete
Enqueue = Push = Insert
Head = Front
Tail = Rear
Queue CreateQueue ()
int isEmpty (Queue Q)
ElementType Dequeue (Queue Head) -> Delete the element at
head of a Queue (Deleting)
void Enqueue(Queue Tail, ElementType X) -> Add an element X at
tail of a Queue (Inserting)
17
4/9/2024
Tail
Tail
18
4/9/2024
Tail
Tail
Tail
19
4/9/2024
Deque ADT
A Deque is an abstract data structure which emphasizes specific
operations: Allows insertions and deletion at both the front and
back of the deque
20
4/9/2024
Summary
• Doubly Linked List
• Circular Linked List
• Stack
• Queue, Deque
• Next week: Tree (chapter 4 of textbook 1)
• Please finish your homework
21