Stacks Notes
Stacks Notes
Stacks
• Subcase of sequential data structures - limited access data structures
• Arrays : Random access
• Lists : Sequential access
• Last In First Our (LIFO) List
• Operations
• Push, Pop and peek
• isempty
• Applications
• Reverse a word
• Solve Maze game
• space for parameters and local variables is created internally using a stack.
• compiler's syntax check for matching braces is implemented by using stack.
• support for recursion
Pre and post conditions
// move all elements of S into tmp -- they will be in the reverse order
while (!stack_empty(S))
push(tmp, pop(S));
return copy;
}
What does the following code fragment print when n is 50? In general,
what does it do when presented with a positive integer n?
• Unbalanced Parentheses
((((((())
()))
(()()(()
Parentheses check : Illustration
(a*(b + c)-(d/(e+(a-c)+d)*(c+e)))
(
( ( ( (
( ( ( ( ( ( ( (
( ( ( ( ( ( ( ( ( ( (
Valid Parentheses
Parentheses check : Illustration
(a*(d/e+(a-c))+d))*(c+e)))
(
( ( (
( ( ( ( (
)
In Valid Parentheses
Parentheses check : Illustration
{a*(b + c)-[d/(e+(a-c)+d]*(c+e)]}
(
( ( (
( [ [ [ [
{ { { { { { {
Invalid Parentheses
• In using a stack to check delimiter matching, three different kinds of
errors can occur. Give example expressions that would create each
kind of error, and indicate the stack status in each case
Algorithm ParenthesesCheck(infix)
1. Create stack s
2. balanced = True
3. index = 0
4. while index < len(infix) and balanced == True
i. symbol = infix[index]
ii. if symbol == "("
push(S, symbol)
else if symbol == ‘)’
if isEmpty(S)
balanced = False
else
pop(S)
iii. index = index + 1
5. if balanced and isEmpty(S)
return True
else
return False
Infix to Postfix notation
• Read in the tokens one at a time
• If a token is an operand, write it into the output
• If a token is an operator,
• push it to the stack, if the stack is empty.
• If the stack is not empty, pop entries with higher or equal priority and then push that token
to the stack.
• If a token is a left parentheses '(',
• push it to the stack
• If a token is a right parentheses ')',
• pop entries until '(‘ and add to output ; ignore ‘)’
• When you finish reading the string, you pop up all tokens which are left there.
[Arithmetic precedence is in increasing order: '+', '-', '*', '/‘]
Conversion of Infix expression to Postfix
While characters remain in the infix string
1. read the next character in the infix string
2. if the character is an operand,
append the character to the postfix expression
3. if the character is an operator @
while the stack is not empty and an operator of greater or equal priority is on the stack
pop the stack and append the operator to the postfix
push @
4. if the character is a left parenthesis ‘(‘
push the parenthesis onto the stack
5. if the character is a right parenthesis ‘)’
while the top of the stack is not a left parenthesis ‘(‘
pop the stack and append the operator to postfix expression
pop the stack ‘(‘ and discard the returned left parenthesis
Pop any remaining items on the stack and append to postfix.
Infix to Postfix notation - Example
Infix Expression a+(b+c*a+d)/c
Postfix Expression a
ab
abc
abca
abca*+
abca*+d
*
abca*+d+
+ + + abca*+d+c
( ( ( ( ( /
abca*+d+c/+
+ + + + + + + +
Evaluating a Postfix Expression
while characters remain in the postfix string
1. read a character
2. if the character is an operand, push it onto a stack
3. if the character is an operator
i. pop the second operand
ii. pop the first operand
iii. perform the operation
iv. push the result
Pop the final value from the stack