Stacks - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Stacks

Description:

It is an ordered group of homogeneous items. ... In postfix notation, the operator is written after the two operands. infix: 2 5 postfix: 2 5 ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 41
Provided by: ECC69
Category:
Tags: infix | stacks

less

Transcript and Presenter's Notes

Title: Stacks


1
Stacks Templates
  • CS 302 Data Structures
  • Sections 5.1 6.1

2
What 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

3
Stack 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

4
Stack Implementation
  • StackTypeStackType()
  • top -1
  • void StackTypeMakeEmpty()
  • top -1

O(1)
O(1)
5
Stack Implementation (cont.)
  • bool StackTypeIsEmpty() const
  • return (top -1)
  • bool StackTypeIsFull() const
  • return (top MAX_ITEMS-1)
  •  

O(1)
O(1)
6
Push (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.

7
Stack Implementation (cont.)
  •  
  • void StackTypePush(ItemType newItem)
  • top
  • itemstop newItem
  •  

O(1)
8
Pop (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.

9
Stack 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)

11
Tracing 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
12
Tracing 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
13
Tracing 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
14
Tracing 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
15
Tracing 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
16
Tracing 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
17
Tracing 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
18
Tracing 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
19
Tracing 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
20
Tracing 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
21
Tracing 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
22
Tracing 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
23
Tracing 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
24
Tracing 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
25
Tracing 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
26
Implementing 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 !!

27
Implementing 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

28
Example 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.

29
Function templates
  • The definitions of the member functions must be
    rewritten as function templates.
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType()
  • top -1
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtMakeEmpty()
  • top -1

30
Function 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

31
Function templates (cont.)
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtPop(ItemType item)
  • item itemstop
  • top--

32
Compiling 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

33
Implementing 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
34
Implementing stacks using dynamic array
allocation (cont.)
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType(int max)
  • maxStack max
  • top -1
  • items new ItemTypemax
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType()
  • delete items

35
Example 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!!

36
Example postfix expressions(cont.)
37
Postfix expressions Algorithm using stacks
(cont.)
38
Postfix 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).

40
Stack
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
Write a Comment
User Comments (0)
About PowerShow.com