0% found this document useful (0 votes)
0 views

Lecture-10 & 11 (B)_(Stack_Array)

The document provides a detailed specification of a stack data structure, including its operations and implementation in C++. It describes the Last In First Out (LIFO) principle, along with methods such as Push, Pop, and Top, as well as algorithms for matching parentheses and evaluating arithmetic expressions. Additionally, it includes exception handling for full and empty stacks.

Uploaded by

Nafis Shahriar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture-10 & 11 (B)_(Stack_Array)

The document provides a detailed specification of a stack data structure, including its operations and implementation in C++. It describes the Last In First Out (LIFO) principle, along with methods such as Push, Pop, and Top, as well as algorithms for matching parentheses and evaluating arithmetic expressions. Additionally, it includes exception handling for full and empty stacks.

Uploaded by

Nafis Shahriar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Stack

▪ 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);
}
}

bool StackType::IsFull() ItemType StackType::Top()


{ {
return (top == MAX_ITEMS-1); if (IsEmpty())
} throw EmptyStack();
return items[top];
}

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

()
(())()(()())()
(())()((()
(())))((()

Which of the strings of parentheses are balanced?

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

Evaluating arithmatic expressions

Infix Postfix Evaluation

2-3*4+5 234*-5+ -5

(2 - 3) * (4 + 5) 23-45+* -9

2- (3 * 4 +5) 234*5+- -15

Why ? No parentheses necessary !


Application of Stack
Algorithm for Infix to Postfix
1. Examine the next element in the input.
2. If it is operand, output it.
3. If it is opening parenthesis, push it on stack.
4. If it is an operator, then
• Pop until the top of the stack has an element of lower
precedence
• Then push it
5. If it is a closing parenthesis, pop operators from stack and output them
until an opening parenthesis is encountered. pop and discard the opening
parenthesis.
6. If there is more input go to step 1
7. If there is no more input, pop the remaining operators to output.

11
Application of Stack

Suppose we want to convert


(4+8)*(6-5)/((3-2)*(2+2))into Postfix form

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

Now let’s evaluate this expression.


4 8 + 6 5 - * 3 2 – 2 2 + * /

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

Only item in stack: indicates valid expression

stack 3

More than one item in stack or unsuccessful pop:


indicates invalid expression

58

You might also like