0% found this document useful (0 votes)
58 views29 pages

Stacks

Uploaded by

frustatedmonk2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views29 pages

Stacks

Uploaded by

frustatedmonk2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stacks

What is a stack?
• It is an ordered group of homogeneous items of elements.
• Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
• The last element to be added is the first to be removed
(LIFO: Last In, First Out).
Stack Specification
• Definitions: (provided by the user)
• MAX_ITEMS: Max number of items that might be on the
stack
• ItemType: Data type of the items on the stack
• Operations
• MakeEmpty
• Boolean IsEmpty
• Boolean IsFull
• Push (ItemType newItem)
• Pop ()
Push (ItemType newItem)

•Function: Adds newItem to the top of


the stack.
Pop (ItemType& item)

• Function: Removes topItem from stack and returns it in item.


Array Representation of stacks

.
Insertion in a Stack
PUSH(STACK,TOP,MAXSTK,ITEM) /initialize TOP=-1, when the stack is empty)
1. If TOP=MAXSTK-1, then: Print OVERFLOW and Exit.
2. Set TOP:=TOP+1
3. Set STACK[TOP]:=ITEM
4. Return
Deletion from a Stack

POP(STACK,TOP,ITEM)
1. If TOP = -1, then, Print: UNDERFLOW and Return.
2. Set ITEM:=STACK[TOP]
3. Set TOP:=TOP-1
4. Return
Stack Implementation
#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 items[MAX_ITEMS];
};
Stack Implementation (cont.)

StackType::StackType()
{
top = -1;
}

void StackType::MakeEmpty()
{
top = -1;
}

bool StackType::IsEmpty() const


{
return (top == -1);
}
Stack Implementation (cont.)
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}

void StackType::Push(ItemType newItem)


{
top++;
items[top] = newItem;
}

void StackType::Pop(ItemType& item)


{
item = items[top];
top--;
}
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);
Applications of Stacks

• What are some applications of stacks?


• Program execution
• Parsing
• Evaluating postfix expressions
Applications of Stacks

Evaluation of Arithmetic Expressions.


• Notations
• Infix notation
A+B
• Prefix Notation (Polish Notation)
+AB
• Postfix Notation (Reverse Polish Notation)
AB+
Conversion from Infix to Prefix

• (A +B) * C = [+ A B] * C = * + A B C
• A + (B * C) = A + [* B C] = + A * B C
• (A + B)/(C – D) = [+ A B ] / [ - C D ] = / + A B – C D
Converting Infix to Postfix Expression

POLISH(Q,P)
1. Push “(“ onto STACK and add “)” to the end of Q
2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q
until the STACK is empty.
3. If an operand is encountered, add it to P.
4. If a left paranthesis is encountered, push it onto STACK.
5. If an operator @ is encountered, then
a) Repeatedly pop from STACK and add to P each operator(on the top
of STACK) which has the same precedence as or higher precedence
than @. Then push @ onto the stack.
6. If a right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each operator(on the
top of STACK) until a left parenthesis is encountered.
b) Remove the left parenthesis [Do not add left parenthesis to P].
7. Exit
Postfix to Value

.
FIGURE 5-9 The steps in converting the infix expression
a / b * (c + (d - e)) to postfix form

Copyright ©2012 by Pearson Education, Inc. All rights reserved


Evaluating Postfix Expressions

FIGURE 5-10 The stack during the evaluation of the postfix


expression a b / when a is 2 and b is 4
Copyright ©2012 by Pearson Education, Inc. All rights reserved
FIGURE 5-11 The stack during the evaluation of the postfix expression a
b + c / when a is 2, b is 4, and c is 3

Copyright ©2012 by Pearson Education, Inc. All rights reserved


Reversing a Word

• We can use a stack to reverse the letters in a word.


• How?

21
Reversing a Word

• Read each letter in the word and push it onto the stack
• When you reach the end of the word, pop the letters off the stack
and print them out.

22
Tower of Hanoi

Tower of Hanoi, is a mathematical puzzle which consists of three towers


(pegs) and more than one rings is as depicted
These rings are of different sizes and stacked upon in an
ascending order, i.e. the smaller one sits over the larger one.
There are other variations of the puzzle where the number of
disks increase, but the tower count remains the same.
Rules
The mission is to move all the disks to some another tower
without violating the sequence of arrangement. A few rules to be
followed for Tower of Hanoi are −
•Only one disk can be moved among the towers at any given
time.
•Only the "top" disk can be removed.
•No large disk can sit over a small disk.
• Tower of Hanoi puzzle with n disks can be solved in
minimum 2n−1 steps. This presentation shows that a puzzle
with 3 disks has taken 23 - 1 = 7 steps.
Tower of Hanoi

You might also like