Lecture-10 & 11 (B)_(Stack_Array)
Lecture-10 & 11 (B)_(Stack_Array)
▪ A list
▪ Data items can be added and deleted
▪ Maintains Last In First Out (LIFO) order
Specification of StackType
Structure: Elements are added to and removed from the top of the stack.
Definitions (provided by user):
MAX_ITEMS Maximum number of items that might be on the stack.
ItemType Data type of the items on the stack.
Operations (provided by the ADT):
MakeEmpty
Function Sets stack to an empty state.
Postcondition Stack is empty.
Boolean IsEmpty
Function Determines whether the stack is empty.
Precondition Stack has been initialized.
Postcondition Returns true if stack is empty and false otherwise.
Boolean IsFull
Function Determines whether the stack is full.
Precondition Stack has been initialized.
Postcondition Returns true if stack2 is full and false otherwise.
Specification of StackType
Push(ItemType newItem)
Function Adds newItem to the top of the stack.
Precondition Stack has been initialized.
Postcondition If (stack is full), exception FullStack is thrown, else newItem is at
the top of the stack.
Pop()
Function Removes top item from the stack.
Precondition Stack has been initialized.
Postcondition If (stack is empty), exception EmptyStack is thrown, else top
element has been removed from stack.
ItemType Top()
Function Returns a copy of the top item on the stack.
Precondition Stack has been initialized.
Postcondition If (stack is empty), exception EmptyStack is thrown, else a copy of
the top element is returned.
3
stacktype.h
#include "ItemType.h"
class FullStack
// Exception class thrown by Push when stack is full.
{};
class EmptyStack
// Exception class thrown by Pop and Top when stack is emtpy.
{};
class StackType
{
public:
StackType();
void MakeEmpty();
bool IsFull();
bool IsEmpty();
void Push(ItemType item);
void Pop();
ItemType Top();
private:
int top;
ItemType items[MAX_ITEMS];
};
4
stacktype.cpp
#include "StackType.h" void StackType::Push(ItemType newItem)
#include <iostream> {
StackType::StackType() if( IsFull() )
{ throw FullStack();
top = -1;
top++;
}
items[top] = newItem;
void StackType::MakeEmpty() }
{
top = -1; void StackType::Pop()
} {
if( IsEmpty() )
bool StackType::IsEmpty() throw EmptyStack();
{ top--;
return (top == -1);
}
}
5
stacktype.cpp
#include "StackType.h" void StackType::Push(ItemType newItem)
#include <iostream> {
StackType::StackType() if( IsFull() )
{ throw FullStack();
top = -1;
} O(1) top++;
items[top] = newItem;
O(1)
void StackType::MakeEmpty() }
{
top = -1; void StackType::Pop()
}
O(1) {
if( IsEmpty() )
bool StackType::IsEmpty()
{
throw EmptyStack();
top--;
O(1)
return (top == -1);
}
}
O(1)
bool StackType::IsFull() ItemType StackType::Top()
{ {
return (top == MAX_ITEMS-1); if (IsEmpty())
} throw EmptyStack();
return items[top]; O(1)
}
O(1)
6
ADT Stack Operations
Transformers
• Push
• Pop change state
• MakeEmpty
Observers
• IsEmpty
• IsFull
observe state
• Top
7
Application of Stack
()
(())()(()())()
(())()((()
(())))((()
8
Application of Stack
Algorithm for matching parentheses string
1. Initialise an empty stack
2. Read next item in the string
a) If item is an opening parentheses, push it into the stack
b) Else, if item is a closing parentheses, pop from stack
3. If there are more items to process, go to step 2
4. Pop the answer off the stack.
Application of Stack
2-3*4+5 234*-5+ -5
(2 - 3) * (4 + 5) 23-45+* -9
11
Application of Stack
12
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack
Postfix
13
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack (
Postfix
14
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack (
Postfix 4
15
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack ( +
Postfix 4
16
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack ( +
Postfix 4 8
17
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack
Postfix 4 8 +
18
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack *
Postfix 4 8 +
19
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack * (
Postfix 4 8 +
20
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack * (
Postfix 4 8 + 6
21
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack * ( -
Postfix 4 8 + 6
22
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack * ( -
Postfix 4 8 + 6 5
23
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack *
Postfix 4 8 + 6 5 -
24
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack /
Postfix 4 8 + 6 5 - *
25
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / (
Postfix 4 8 + 6 5 - *
26
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( (
Postfix 4 8 + 6 5 - *
27
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( (
Postfix 4 8 + 6 5 - * 3
28
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( ( -
Postfix 4 8 + 6 5 - * 3
29
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( ( -
Postfix 4 8 + 6 5 - * 3 2
30
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / (
Postfix 4 8 + 6 5 - * 3 2 -
31
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( *
Postfix 4 8 + 6 5 - * 3 2 -
32
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( * (
Postfix 4 8 + 6 5 - * 3 2 -
33
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( * (
Postfix 4 8 + 6 5 - * 3 2 - 2
34
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( * ( +
Postfix 4 8 + 6 5 - * 3 2 - 2
35
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( * ( +
Postfix 4 8 + 6 5 - * 3 2 - 2 2
36
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack / ( *
Postfix 4 8 + 6 5 - * 3 2 - 2 2 +
37
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack /
Postfix 4 8 + 6 5 - * 3 2 - 2 2 + *
38
Application of Stack
Infix ( 4 + 8 ) * ( 6 - 5 ) / ( ( 3 - 2 ) * ( 2 + 2 ) )
Stack
Postfix 4 8 + 6 5 - * 3 2 - 2 2 + * /
39
Application of Stack
40
Application of Stack
Algorithm for evaluating a postfix expression
1. Initialise an empty stack
2. Read next item in the expression
a) If item is an operand, push it into the stack
b) Else, if item is an operator, pop top two items off the stack, apply
the operator, and push the answer back into the stack
3. If there are more items to process, go to step 2
4. Pop the answer off the stack.
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack
42
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 4
43
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 4 8
44
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12
45
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 6
46
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 6 5
47
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1
48
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12
49
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 3
50
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 3 2
51
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1
52
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 2
53
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 2 2
54
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 1 4
55
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 12 4
56
Application of Stack
string 4 8 + 6 5 - * 3 2 - 2 2 + * /
stack 3
57
Application of Stack
stack 3
58