Lecture 07 Stack and Queue
Lecture 07 Stack and Queue
Data Structures
Lecture 7
1
Stacks and Queue
top
• Stacks
– Linear list.
– One end is called top.
– Other end is called bottom. bottom
– Additions to and removals from the top end only.
• Queue
– Linear list.
front
– One end is called front.
– Other end is called rear.
– Additions are done at the rear only.
– Removals are made from the front only. rear
2
Stack
3
Stack Of Cups
PUSH POP
top F
top E E top E
D D D
C C C
B B B
5
The Stack Abstract Data Type
Other supporting methods:
peek() / top(): Return the top object on the stack, without removing
it; an error occurs if the stack is empty.
Input: None; Output: Object
6
This table shows a series of stack operations and their effects.
The stack is initially empty.
Operation Output S
push(5) - (5) 5 3
push(3) - (5,3) 5
pop() 3 (5)
push(7) - (5,7) 5
pop() 7 (5)
top() 5 (5)
pop() 5 ()
pop() “error” ()
isEmpty() true ()
push(9) - (9)
push(7) - (9,7)
push(3) - (9,7,3)
push(5) - (9,7,3,5)
size() 4 (9,7,3,5)
pop() 5 (9,7,3)
push(8) - (9,7,3,8)
pop() 8 (9,7,3)
pop() 3 (9,7)
7
Linked Chain Implementation of
Stack
8
Linked Implementation
9
Linked Implementation
top
null
e d c b a
class Node:
def __init__(self, el = None, n = None):
self.next = n
self.element = el
class LinkedStack:
def __init__(self):
self.top = None
self.size = 0
LinkedStack.py
10
push(…)
top
null
e d c b a
11
peek()
top
null
e d c b a
12
pop()
top
null
e d c b a
null size = 0
14
stack_size()
top
null
e d c b a
size = 5
15
Linked Stack Example
Python Code
st = LinkedStack()
st.push(6);
top
16
Linked Stack Example
Python Code
st = LinkedStack()
st.push(6);
st.push(1);
top
17
Linked Stack Example
Python Code
st = LinkedStack()
st.push(6);
st.push(1);
st.push(7);
top 7
18
Linked Stack Example
Python Code
8 st = LinkedStack()
st.push(6);
st.push(1);
top 7 st.push(7);
st.push(8);
1
19
Linked Stack Example
Python Code
8 st = LinkedStack()
st.push(6);
st.push(1);
top 7 st.push(7);
st.push(8);
st.pop();
1
20
Linked Stack Example
Python Code
st = LinkedStack()
st.push(6);
st.push(1);
top 7 st.push(7);
st.push(8);
st.pop();
1
21
Array Implementation of Stack
22
Array Implementation
• Use a one-dimensional array stack whose data type
is Object.
• Use an int variable size to indicate the number of
elements in stack.
– Stack elements are in stack[0:size-1].
– Top element is in stack[size-1].
– Bottom element is in stack[0].
– Stack is empty iff size = 0.
23
Array Implementation
class ArrayStack:
def __init__(self):
self.stack = []
self.size = 0
ArrayStack.py
24
push(…)
a b c d e
0 1 2 3 4 top
25
peek()
a b c d e
0 1 2 3 4 top
26
pop()
a b c d e
0 1 2 3 4 top
27
empty()
0 1 2 3 4
size = 0
28
size()
a b c d e
0 1 2 3 4 top
size = 5
29
Applications of Stacks
30
Applications of Stacks
32
Checking for Balanced Braces
33
Checking for Balanced Braces
Figure 7-3
Traces of the algorithm that checks for balanced braces
34
Evaluating Postfix Expressions
Figure 7-8
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
36
Evaluate the following Postfix expression
• 32+4*51-/
Exercise
((3 + 2) * 4) / (5 - 1)
5 4 * 5 1 - /
20 5 1 - /
20 4 /
5
37
Queue
38
Bus Stop Queue
39
Bus Stop Queue
40
Bus Stop Queue
front rear
rear
41
Bus Stop Queue
front rear
rear
LinkedQueue.py
44
Linked List Implementation of
Queue
45
LinkedQueue
null
a b c d e
front rear
➢ when front is left end of list and rear is right end
LinkedQueue.py
46
LinkedQueue
front rear
null
a b c d e
class Node:
def __init__(self, el = None, n = None):
self.next = n
self.element = el
class LinkedQueue:
def __init__(self):
self.front = None
self.rear = None
self.size = 0 47
isEmpty()
front rear
null
a b c d e
48
getFrontElement()
front rear
null
a b c d e
49
getRearElement()
front rear
null
a b c d e
50
put(Object theElement)
front rear
null
a b c d e
51
remove()
front rear
null
a b c d e
a) A naive array-based implementation of a queue; b) rightward drift can cause the queue to
appear full
53
Summary
• Stacks
– Linear list.
– One end is called top.
– Other end is called bottom.
– Additions to and removals from the top end only.
• Queue
– Linear list.
– One end is called front.
– Other end is called rear.
– Additions are done at the rear only.
– Removals are made from the front only.
54