DATA STRUCTURES AND ALGORITHMS
IMRAN IHSANASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABADWWW.IMRANIHSAN.COM
LECTURES ADAPTED FROM:DANIEL KANE, NEIL RHODESDEPARTMENT OF CS & ENGINEERINGUNIVERSITY OF CALIFORNIA, SAN DIEGO
08STACKSIMPLEMENTATION & APPLICATIONS
STACK
2
ABSTRACT DATA TYPE
• An Abstract Stack (Stack ADT) is an abstract data type which emphasizes specific operations:• Uses a explicit linear ordering• Insertions and removals are performed individually• Inserted objects are pushed onto the stack• The top of the stack is the most recently object pushed onto the stack• When an object is popped from the stack, the current top is erased
• Also called a last-in–first-out (LIFO) behavior• Graphically, we may view these operations as follows:
• There are two exceptions associated with abstract stacks:• It is an undefined operation to call either pop or top on an empty stack
STACK
3
APPLICATIONS
• Numerous applications:• Parsing code:
• Matching parenthesis• XML (e.g., XHTML)
• Tracking function calls• Dealing with undo/redo operations• Reverse-Polish calculators• Assembly language
• The stack is a very simple data structure• Given any problem, if it is possible to use a stack,
this significantly simplifies the solution
• Problem solving:• Solving one problem may lead to subsequent problems• These problems may result in further problems• As problems are solved, your focus shifts back to the problem which lead to
the solved problem
• Notice that function calls behave similarly:• A function is a collection of code which solves a problem
STACK
4
DEFINITION
• Abstract data type with the following operations:
Push(Key): adds key to collection
Key Top(): returns most recently-added key
Key Pop(): removes and returns most recently-added key
Boolean Empty(): are there any elements?
BALANCED BRACKETS
5
• Input:
• A string str consisting of ‘(‘, ‘)’, ‘[‘,‘]’ characters.
• Output:
• Return whether or not the string’s parentheses and square brackets are balanced.
Balanced: Unbalanced:
“([])[]()”, “([]]()”
“((([([])]))())” “][”
BALANCED BRACKETS
6
IsBalanced(str )
Stack stack
for char in str:
if char in [‘(‘, ‘[‘]:
stack.Push(char)
else:
if stack.Empty():
return False
top ← stack.Pop()
if (top = ‘[‘ and char != ‘]’) or
(top = ‘(‘ and char != ‘)’):
return False
return stack.Empty()
STACK IMPLEMENTATION WITH ARRAY
7
numElements: 0
STACK IMPLEMENTATION WITH ARRAY
8
numElements: 0
Push(a)
STACK IMPLEMENTATION WITH ARRAY
9
numElements: 1
Push(a)
a
STACK IMPLEMENTATION WITH ARRAY
10
numElements: 2
Push(b)
a b
STACK IMPLEMENTATION WITH ARRAY
11
numElements: 2
Top() b
a B
STACK IMPLEMENTATION WITH ARRAY
12
numElements: 3
Push(c)
a b c
STACK IMPLEMENTATION WITH ARRAY
13
numElements: 2
Pop() c
a b
STACK IMPLEMENTATION WITH ARRAY
14
numElements: 3
Push(d)
a b d
STACK IMPLEMENTATION WITH ARRAY
15
numElements: 4
Push(e)
a b d e
STACK IMPLEMENTATION WITH ARRAY
16
numElements: 5
Push(f)
a b d e f
STACK IMPLEMENTATION WITH ARRAY
17
numElements: 5
Push(g) ERROR
a b d e f
STACK IMPLEMENTATION WITH ARRAY
18
numElements: 5
Empty() False
a b d e f
STACK IMPLEMENTATION WITH ARRAY
19
numElements: 4
Pop() f
a b d e
STACK IMPLEMENTATION WITH ARRAY
20
numElements: 3
Pop() e
a b d
STACK IMPLEMENTATION WITH ARRAY
21
numElements: 2
Pop() d
a b
STACK IMPLEMENTATION WITH ARRAY
22
numElements: 1
Pop() b
a
STACK IMPLEMENTATION WITH ARRAY
23
numElements: 0
Pop() a
STACK IMPLEMENTATION WITH ARRAY
24
numElements: 0
Empty() True
STACK IMPLEMENTATION WITH LINKED LIST
25
Empty Linked List/
head
STACK IMPLEMENTATION WITH LINKED LIST
26
Push(a)
a
/
head
STACK IMPLEMENTATION WITH LINKED LIST
27
Push(b)
a
/
head
b
STACK IMPLEMENTATION WITH LINKED LIST
28
Top() b
a
/
head
b
STACK IMPLEMENTATION WITH LINKED LIST
29
Push(c)
a
/
head
bc
STACK IMPLEMENTATION WITH LINKED LIST
30
Pop() c
a
/
head
b
STACK IMPLEMENTATION WITH LINKED LIST
31
Push(d)
a
/
head
bd
STACK IMPLEMENTATION WITH LINKED LIST
32
Push(e)
a
/
head
bde
STACK IMPLEMENTATION WITH LINKED LIST
33
Push(f)
a
/
head
bdef
STACK IMPLEMENTATION WITH LINKED LIST
34
Empty() False
a
/
head
bdef
STACK IMPLEMENTATION WITH LINKED LIST
35
Pop() f
a
/
head
bde
STACK IMPLEMENTATION WITH LINKED LIST
36
Pop() e
a
/
head
bd
STACK IMPLEMENTATION WITH LINKED LIST
37
Pop() d
a
/
head
b
STACK IMPLEMENTATION WITH LINKED LIST
38
Pop() b
a
/
head
STACK IMPLEMENTATION WITH LINKED LIST
39
Pop() a
/
head
STACK IMPLEMENTATION WITH LINKED LIST
40
Empty() True
/
head
PARSING XHTML
41
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
42
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
43
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
44
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
45
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
46
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
47
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
48
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
49
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
50
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
51
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
PARSING XHTML
52
STACK APPLICATIONS
<html>
<head><title>Hello</title></head>
<body><p>This appears in the <i>browser</i>.</p></body>
</html>
SUMMARY
53
• Stacks can be implemented with either an array or a linked list.
• Each stack operation is O(1):
• Push, Pop, Top, Empty.
• Stacks are occasionally known as LIFO queues.