Title: Stacks
1Stacks Templates
- CS 302 Data Structures
- Sections 5.1 6.1
2What is a stack?
- It is an ordered group of homogeneous items.
- Items are added to and removed from the top of
the stack - LIFO property Last In, First Out
- The last item added would be the first to be
removed
3Stack Specification
- include "ItemType.h"
- // Must be provided by the user of the class
- // Contains definitions for MAX_ITEMS and
ItemType - Â
- class StackType
- public
- StackType()
- void MakeEmpty()
- bool IsEmpty() const
- bool IsFull() const
- void Push(ItemType)
- void Pop(ItemType)
- private
- int top
- ItemType itemsMAX_ITEMS
4Stack Implementation
- StackTypeStackType()
-
- top -1
-
- void StackTypeMakeEmpty()
-
- top -1
-
O(1)
O(1)
5Stack Implementation (cont.)
- bool StackTypeIsEmpty() const
-
- return (top -1)
-
- bool StackTypeIsFull() const
-
- return (top MAX_ITEMS-1)
-
- Â
O(1)
O(1)
6Push (ItemType newItem)
- Function Adds newItem to the top of the stack.
- Preconditions Stack has been initialized and is
not full. - Postconditions newItem is at the top of the
stack.
7Stack Implementation (cont.)
- Â
- void StackTypePush(ItemType newItem)
-
- top
- itemstop newItem
-
- Â
O(1)
8Pop (ItemType item)
- Function Removes topItem from stack and returns
it in item. - Preconditions Stack has been initialized and is
not empty. - Postconditions Top element has been removed from
stack and item is a copy of the removed element.
9Stack Implementation (cont.)
- Â
- void StackTypePop(ItemType item)
-
- item itemstop
- top--
O(1)
10- Stack overflow
- The condition resulting from trying to push an
element onto a full stack. - if(!stack.IsFull())
- stack.Push(item)
- Stack underflow
- The condition resulting from trying to pop an
empty stack. - if(!stack.IsEmpty())
- stack.Pop(item)
11Tracing Client Code
letter
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter) charStack.Push(K) whil
e (!charStack.IsEmpty( )) charStack.Pop(letter)
Private data top
MAX_ITEMS-1 .
.
. 2
1 items 0
12Tracing Client Code
letter
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
Private data top -1
MAX_ITEMS-1 .
.
. 2
1 items 0
13Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter
)
Private data top 0
MAX_ITEMS-1 .
.
. 2
1 items 0 V
14Tracing Client Code
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter ) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 1
MAX_ITEMS-1 .
.
. 2
1 C items
0 V
15Tracing Client Code
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
16Tracing Client Code
V
letter
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter
)
Private data top 2
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
17Tracing Client Code
S
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 1
MAX_ITEMS-1 .
.
. 2 S
1 C
items 0 V
18Tracing Client Code
S
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop(letter ) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 2
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
19Tracing Client Code
S
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter
)
letter
Private data top 2
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
20Tracing Client Code
K
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
21Tracing Client Code
K
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( ) charStack.Push(K) while
(!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
22Tracing Client Code
C
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 0
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
23Tracing Client Code
C
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( ) charStack.Push(K) while
(!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top 0
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
24Tracing Client Code
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( letter) charStack.Push(K) whi
le (!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top -1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
25Tracing Client Code
V
char letter V StackType charStack charStac
k.Push(letter) charStack.Push(C) charStack.P
ush(S) if ( !charStack.IsEmpty( ))
charStack.Pop( ) charStack.Push(K) while
(!charStack.IsEmpty( )) charStack.Pop(letter)
letter
Private data top -1
MAX_ITEMS-1 .
.
. 2 K
1 C
items 0 V
26Implementing stacks using templates
- Templates allow the compiler to generate multiple
versions of a class type or a function by
allowing parameterized types. - Essentially, we can pass the data type as a
parameter to a class !!
27Implementing stacks using templates
(cont.)
- templateltclass ItemTypegt
- class StackType
- public
- StackType()
- void MakeEmpty()
- bool IsEmpty() const
- bool IsFull() const
- void Push(ItemType)
- void Pop(ItemType)
- private
- int top
- ItemType itemsMAX_ITEMS
-
28Example using templates
- // Client code
- StackTypeltintgt myStack
- StackTypeltfloatgt yourStack
- StackTypeltStrTypegt anotherStack
-
- myStack.Push(35)
- yourStack.Push(584.39)
- Â
- Passing a parameter to a template has an effect
at compile time. - The compiler generates distinct class types and
gives its own internal name to each of the types.
29Function templates
- The definitions of the member functions must be
rewritten as function templates. - templateltclass ItemTypegt
- StackTypeltItemTypegtStackType()
-
- top -1
-
- templateltclass ItemTypegt
- void StackTypeltItemTypegtMakeEmpty()
-
- top -1
-
30Function templates (cont.)
- Â templateltclass ItemTypegt
- bool StackTypeltItemTypegtIsEmpty() const
-
- return (top -1)
-
- templateltclass ItemTypegt
- bool StackTypeltItemTypegtIsFull() const
-
- return (top MAX_ITEMS-1)
-
- Â
- templateltclass ItemTypegt
- void StackTypeltItemTypegtPush(ItemType newItem)
-
- top
- itemstop newItem
31Function templates (cont.)
- templateltclass ItemTypegt
- void StackTypeltItemTypegtPop(ItemType item)
-
- item itemstop
- top--
32Compiling templates
- We cannot compile StackType.cpp separately from
the application (i.e., client code)! - Compiler needs to know the data type of the stack
to instantiate the class! - But ... the data type is provided in the clients
code! - To address this problem, compile StackType.cpp
and client code together! - (1) Append StackType.cpp at the end of
StackType.h - or include StackType.cpp at the end of
StackType.h - (2) include StackType.h in clients code
- (3) Compile client code
33Implementing stacks using dynamic array
allocation
- templateltclass ItemTypegt
- class StackType
- public
- StackType(int)
- StackType()
- void MakeEmpty()
- bool IsEmpty() const
- bool IsFull() const
- void Push(ItemType)
- void Pop(ItemType)
private int top int maxStack
ItemType items
34Implementing stacks using dynamic array
allocation (cont.)
- templateltclass ItemTypegt
- StackTypeltItemTypegtStackType(int max)
-
- maxStack max
- top -1
- items new ItemTypemax
-
- templateltclass ItemTypegt
- StackTypeltItemTypegtStackType()
-
- delete items
35Example using stacks evaluate postfix
expressions
- Postfix notation is another way of writing
arithmetic expressions. - Â
- In postfix notation, the operator is written
after the two operands. - Â
- infix 25 postfix 2 5
- Expressions are evaluated from left to right.
- Â
- Precedence rules and parentheses are never
needed!!
36Example postfix expressions(cont.)
37Postfix expressions Algorithm using stacks
(cont.)
38Postfix expressionsAlgorithm using stacks
- WHILE more input items exist
- Get an item
- IF item is an operand
- stack.Push(item)
- ELSE
- stack.Pop(operand2)
- stack.Pop(operand1)
- Compute result
- stack.Push(result)
- stack.Pop(result)
39- Exercise 15 (page 330) Write the body for a
client function that replaces each copy of an
item in a stack with another item. Use the
following specification. - ReplaceItem(StackType stack, ItemType oldItem,
ItemType newItem) - Function Replaces all occurrences of oldItem
with newItem. - Precondition stack has been initialized.
- Postconditions Each occurrence of oldItem in
stack has been replaced by newItem. Order of
other elements remains unchanged. - (You may not assume any knowledge of how the
stack is implemented).
40Stack
tempStack
-
- ItemType item
- StackType tempStack
- while (!Stack.IsEmpty())
- Stack.Pop(item)
- if (itemoldItem)
- tempStack.Push(newItem)
- else
- tempStack.Push(item)
-
- while (!tempStack.IsEmpty())
- tempStack.Pop(item)
- Stack.Push(item)
-
-
Stack
oldItem 2 newItem 5