Title: Chapter 4: Stacks
1Chapter 4 Stacks
2Stack
- A stack is a special case of linear list in which
insertion and deletion operation are restricted
to occur only at one end, which is referred to as
the stacks top. - Stacks are also known as LIFO (last in, first
out) lists. - Example, a pile of trays in McDonald.
Top
Top
Top
Top
3Stack
- Any picture that we draw of a stack is really
only a snapshot in time. The top element changes
as the stack grows and shrinks the bottom remain
fixed. - Most programming language do not have a built-in
stack data structure. The simplest way to
represent stacks is to house them in arrays and
write the programming code to enforce the stack
rule.
4Stack Operations
- Basic operations that are legal for data type
stack - Create(stack)
- Top(stack)
- Noel(stack)
- Isempty(stack)
- Push(element,stack)
- Pop(stack)
5Stack Operations
- Create(S) operator returns an empty stack with
name S. - Noel(Create(S)) is 0
- Top(Create(S)) is null
- Isempty(S) determines whether or not a stack is
empty. - Isempty(S) is true if stack S is empty.
6Stack Operations
- Push(E,S) operator adds element E to the top of
stack S. - Top(Push(E,S)) is ???
- Isempty(Push(E,S)) is ???
- Pop(S) operator removes an element from the top
of stack S. - Pop(S) decrements Noel(S) and changes Top(S).
E
7Stack Operations
- Example (The following operators are operated
sequentially) - Create(S) gt Noel(S)???, Top(S)???
- Push(A,S) gt Noel(S)???, Top(S)???
- Push(B,S) gt Noel(S)???, Top(S)???
- Push(C,S) gt Noel(S)???, Top(S)???
- Pop(S) gt Noel(S)???, Top(S)???
- Push(D,S) gt Noel(S)???, Top(S)???
- Push(E,S) gt Noel(S)???, Top(S)???
- Pop(S) gt Noel(S)???, Top(S)???
8Example Applications of Stacks
- Example Matching Parentheses
- Consider the syntax verification problem of
ensuring that for each left parenthesis there is
a corresponding right parenthesis. - A stack can be used to facilitate the matching
procedure. - Whenever we encounter a left parenthesis, we push
it onto a stack. - Whenever we hit a right parenthesis, we check the
state of the stack. - If it is empty, then we have found a right
parenthesis that does not close a left
parenthesis and have an error. - If the stack is not empty, we have fond a pair
and merely pop the stack. - If the stack is not empty at the end of the
string, then there is at least one unclosed left
parenthesis.
9Matching Parentheses
Example (AB)C (AB)C)
Position NEXT-CHAR at end of string
End of string?
Stack empty?
yes
yes
Valid syntax
no
Invalid syntax unclosed left parenthesis
no
done
Found ( ?
yes
Push ( onto stack
no
Found ) ?
Stack empty?
Invalid syntax unclosed left parenthesis
yes
yes
no
no
Pop stack
done
Increment NEXT-CHAR to next character in string
10Postfix Notation
- The compiler needs to be able to translate from
our usual form of representation of arithmetic
statements (called infix notation to a form that
can more easily be used for generation of object
code. - For binary operators, such as addition,
subtraction, multiplication, division, and
exponentiation, the operator in infix notation
appears between the two operands, e.g., AB. - Stack is used to transform this notation to what
is known as postfix notation, in which both
operands appear before the operator, e.g., AB
11Converting expressions from infix to postfix
notation
Start POSITION at beginning of INFIX string
End of INFIX string?
Pop the stack, unloading to output
yes
DONE
no
Found ( ?
yes
Push ( onto stack
no
Pop the stack to output until find (
Found ) ?
Pop ( from stack
yes
no
Determine precedence of operator and precedence
of top element on stack
Found operator ?
yes
no
Operator precedence lt top elements precedence ?
Pop stack to output
Move character to output
yes
Increment POSITION To next character In INFIX
string
no
Push operator onto stack
12Postfix Notation Rules
- If the symbol is (, it is pushed onto the
operator stack. - If the symbol is ), it pops everything from the
operator stack down to the first (. The
operators go to output as they are popped off the
stack. The ( is popped but does not go to
output. - If the symbol is an operator, then if the
operator on the top of the operator stack is of
the same of higher precedence, that operator is
popped and goes to output, continuing in this
fashion until the first left parenthesis or an
operator of lower precedence is met in the
operator stack. When that situation occurs, then
the current operator is pushed onto the stack. - If the symbol is an operand, it goes directly to
output. - The terminating symbol (semicolon), pops
everything off the stack. - Operator precedence levels
- Highest Exponentiation (?)
- Middle Multiplication (), Division(/)
- Lowest Addition (), Subtraction(-)
13Postfix Notation
- Infix arithmetic expression
- ((AB)C/D E ? F)/G
- Postfix arithmetic expression
- AB C D/EF ? G/.
- Try A ? B C D E / F / (G H) ???
14Mapping to Storage
- Mapping stack to storage uses the same approach
as mapping one-dimensional arrays which mapped to
storage in a physically sequential manner. The
stack is assigned the base location which remains
fixed and it is allowed to grow into adjacent
locations.
15Mapping to Storage
- Sharing Space
- Assume now that we have two stacks that need to
co-exist in memory. If stack S1 will have a
maximum of M elements and stack S2 will have a
maximum of N elements. - This is not particularly efficient, especially if
stacks S1 and S2 are never full at the same time.
16Mapping to Storage
- S1 and S2 will never run into each other, yet
neither of them will ever overflow together they
can accommodate up to N elements. Stack S1 grows
to the right and stack S2 grows to the left.