8_ADTStack
8_ADTStack
STACKS
CS212: Data Structure
2
Stacks
• A stack is a container of objects that are inserted and
removed according to the last-in-first-out (LIFO)
principle.
• Objects can be inserted at any time, but only the last (the
most-recently inserted) object can be removed.
• Inserting an item is known as “Pushing” onto the stack.
“Popping” off the stack is synonymous with removing an
item
• Used in Operating system to implement method calls, and
in evaluating Expressions.
3
Stack Interface
public interface Stack<T>{
public T pop( );
public void push(T e);
public boolean empty( );
public boolean full( );
}
7
null
8
public Node () {
data = null;
next = null;
}
// Setters/Getters?
}
9
Example #1
15
Example #1
16
Example #1
17
Example #1
18
Example #2
19
Example #2
20
Example #2
21
Example #2
22
Example #3
23
tmp
Example #3
24
tmp
Example #3
25
tmp
T
public void push(T e){
Node<T> tmp = new Node<T>(e);
tmp.next = top;
top = tmp;
}
null
Example #3
26
T
public void push(T e){
Node<T> tmp = new Node<T>(e);
tmp.next = top;
top = tmp;
}
null
Example #3
27
public T pop(){
T e = top.data;
top = top.next;
return e;
}
}
28
T
public T pop(){
T e = top.data;
top = top.next;
return e;
}
null
}
Example #1
29
T
public T pop(){
T e = top.data;
top = top.next;
return e;
}
null
}
Example #1
30
public T pop(){
T e = top.data; T
top = top.next;
return e;
}
null
}
Example #1
31
public T pop(){
T e = top.data; T
top = top.next;
return e;
}
null
}
Example #1
32
public T pop(){
T e = top.data; T
top = top.next;
return e;
}
null
}
Example #1
33
public T pop(){
T e = top.data; T
top = top.next;
return e;
}
null
}
Example #2
34
public T pop(){ e
T e = top.data; T
top = top.next;
return e;
}
null
}
Example #2
35
public T pop(){ e
T e = top.data;
top = top.next;
return e; T
}
null
}
Example #2
36
public T pop(){ e
T e = top.data;
top = top.next;
return e; T
}
null
}
Example #2
37
public T pop(){
T e = top.data;
top = top.next;
return e; T
}
null
}
Example #2
38
public T pop(){
T e = top.data;
top = top.next;
return e; T
}
null
}
Example #3
39
public T pop(){
T e = top.data;
top = top.next; e
return e; T
}
null
}
Example #3
40
public T pop(){
T e = top.data;
top = top.next; e
return e;
}
null T
}
Example #3
41
public T pop(){
T e = top.data;
top = top.next; e
return e;
}
null T
}
Example #3
42
public T pop(){
T e = top.data;
top = top.next;
return e;
}
null T
}
Example #3
43
public T pop(){
T e = top.data;
top = top.next;
return e;
}
null T
}
Example #3
44
n-1
.
.
.
.
2 T
0
45
1 T 1
1 1
public T pop(){
return nodes[top--];
}
}
51
1
public T pop(){
0
return nodes[top--];
} T
} Example #1
52
1
public T pop(){ T
0
return nodes[top--];
}
} Example #1
53
1
public T pop(){ T
0
return nodes[top--];
e
}
} Example #1
54
1
public T pop(){ T
0
return nodes[top--];
}
} Example #2
55
1 T
public T pop(){
0
return nodes[top--];
}
} Example #2
56
1 T
public T pop(){ e
0
return nodes[top--];
}
} Example #2
57
1 T
public T pop(){
0
return nodes[top--];
}
} Example #3
58
1 T
S1 ← nodes[top]
public T pop(){ top--
return S1 0
return nodes[top--];
}
} Example #3
59
S1 ← nodes[top] 1
public T pop(){ top--
T
return S1 0
return nodes[top--];
}
} Example #3
60
1
public T pop(){ T
0
return nodes[top--];
}
} Example #4
61
S1 ← nodes[top] 1
public T pop(){ top--
T
return S1 0
return nodes[top--];
}
} Example #4
62
S1 ← nodes[top] 1
public T pop(){ top--
return S1 0
return nodes[top--];
} T
} Example #4
63
1
public T pop(){
0
return nodes[top--];
} T
} Example #4
Applications of Stacks
Some applications of stacks are:
◦ Balancing symbols.
◦ Computing or evaluating postfix expressions.
◦ Converting expressions from infix to postfix.
64
65
1. Balancing Symbols
• Expressions: mathematical (a + ((b-c)*d)) or
programs have delimiters.
begin {
S1 S1
S2 {
begin S2
S3 S3
begin }
…. S4
end }
end
end
66
1. Balancing Symbols
• Delimiters must be balanced.
• One of the common use of the stacks is to parse certain
kinds of expressions or string text.
• Write a program that verifies the delimiters in a line of text
or expression typed by the user.
• a*(b+c) //This expression is right
• b/[a*(b+c)] //This expression is right
• {a*(b+c]} //This expression is wrong
67
1. Balancing Symbols
1. Balancing Symbols
• Input : expression
• Output: True if and only if delimiters are balanced
• Let S be empty Stack
• Let n be number of characters
• for i=0 n-1
• If expression[i] is a Opening delimiter, then
• S.push(expression[i]).
• else If expression[i] is a closing delimiter, then
• If the S is empty
• return false unbalanced symbols
• symbol=S.pop().
• If symbol does not matches the closing delimiter
• return false unbalanced symbols.
• If S is empty
• return true balanced symbols.
• else
• return false unbalanced symbols
69
2. Postfix Expressions
• Evaluating Postfix Expressions:
• Infix expression: 4.99*1.06+5.99+6.99*1.06
• Value 18.69 correct parenthesis used.
• Value 19.37 incorrect no parenthesis used.
• In postfix form, above expression becomes:
4.99 1.06 * 5.99 + 6.99 1.06*+
Advantage: no brackets are needed and a stack can be used to
compute the expression.
70
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
• push the result back on the stack.
71
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
• push the result back on the stack.
72
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
6
• push the result back on the stack.
73
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
74
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read: 2
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
75
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
3
• When a number is read push it on the stack.
• When an operator is read: 2
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
76
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
3
• When a number is read push it on the stack.
• When an operator is read: 2
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
77
2. Postfix Expressions
• Example:
2+3=5
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
78
2. Postfix Expressions
• Example:
2+3=5
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read: 5
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
79
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
8
• When a number is read push it on the stack.
• When an operator is read: 5
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
80
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
8
• When a number is read push it on the stack.
• When an operator is read: 5
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
81
2. Postfix Expressions
• Example:
5 * 8 = 40
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 5
• carry out the operation on them
6
• push the result back on the stack.
82
2. Postfix Expressions
• Example:
5 * 8 = 40
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
4
• When an operator is read: 0
• pop two numbers from the stack 5
• carry out the operation on them
• push the result back on the stack. 6
83
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
4
• When an operator is read: 0
• pop two numbers from the stack 5
• carry out the operation on them
• push the result back on the stack. 6
84
2. Postfix Expressions
• Example:
5 + 40 = 45
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
6
• push the result back on the stack.
85
2. Postfix Expressions
• Example:
5 + 40 = 45
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 4
5
• carry out the operation on them
• push the result back on the stack. 6
86
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read: 3
• pop two numbers from the stack 4
5
• carry out the operation on them
• push the result back on the stack. 6
87
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read: 3
• pop two numbers from the stack 4
5
• carry out the operation on them
• push the result back on the stack. 6
88
2. Postfix Expressions
• Example:
45 + 3 = 48
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
6
• push the result back on the stack.
89
2. Postfix Expressions
• Example:
45 + 3 = 48
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 4
8
• carry out the operation on them
• push the result back on the stack. 6
90
2. Postfix Expressions
• Example:
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack 4
8
• carry out the operation on them
• push the result back on the stack. 6
91
2. Postfix Expressions
• Example:
6 * 48 = 288
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them
• push the result back on the stack.
92
2. Postfix Expressions
• Example:
6 * 48 = 288
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them 28
8
• push the result back on the stack.
93
2. Postfix Expressions
• Example:
End!
• infix: 6*(5+((2+3)*8)+3)
• postfix: 6 5 2 3 + 8 * + 3 + *.
• Algorithm to compute postfix expression:
• Read the postfix expression left to right.
• When a number is read push it on the stack.
• When an operator is read:
• pop two numbers from the stack
• carry out the operation on them 28
result 8
• push the result back on the stack.
94
Applications of Stacks
• Direct applications
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in the Java Virtual Machine
• Indirect applications
• Auxiliary data structure for algorithms
• Component of other data structures
Goodrich, Tamassia 2010 © 96
l.findFirst();
while(!l.empty()) {
s.push(l.retrieve());
l.remove();
}
while(!s.empty())
l.insert(s.pop());
}
}
Goodrich, Tamassia 2010 © 98
Parentheses Matching
• Each “(”, “{”, or “[” must be paired with a matching “)”, “}”,
or “]”
• correct: ( )(( )){([( )])}
• correct: ((( )(( )))){([( )])}
• incorrect: )(( )){([( )])}
• incorrect: ({[ ])}
• incorrect: (
Goodrich, Tamassia 2010 © 99
7 + ) )2 * 3( – 14( = 7 + 2 * 3 – 14
Operator precedence
* has precedence over +/–
Associativity
operators of the same precedence group
evaluated from left to right
Example: (x – y) + z rather than x – (y + z)
Idea: push each operator on the stack, but first pop and perform higher and
.equal precedence operations
102
Algorithm on an
Example Expression
7 + 2 * 3 – 4 ≤ 14
Operator ≤ has lower
–/+ precedence than
4 –
14 ≤
3 *
$ $
4 –
7 $
14 ≤ F
+ 2- + 5
+ 14 ≤ 14 ≤
2 2
3 * 3 * 6
4 – 4 – 4 – 2- +
14 ≤ 14 ≤ 14 ≤ 14 ≤