Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Data Structures and Algorithms

Stack

1
◼Introduction to the Stack data
structure
◼Designing a Stack class using
dynamic arrays
◼Linked Stacks
◼Some Applications of Stacks

2
▪ A simple data container consisting of a linear list
of elements
▪ Access is by position (order of insertion)
▪ All insertions and deletions are done at one end,
called top
▪ Last In First Out (LIFO) structure
▪ Two basic operations:
push: add to top
pop: remove from top
3
2. An illustration: A Stack of Plates

Stack Push Stack Pop


of a of a
plates plate plates plate
onto with a from
the new the
stack plate stack
on top

4
push

top ++top top

top top top--


pop

5
◼Run-time stack used in function calls
◼Page-visited history in a Web browser
◼Undo sequence in a text editor
◼Removal of recursion
◼Conversion of Infix to Postfix notation
◼Evaluation of Postfix expressions
◼Reversal of sequences
◼Checking for balanced symbols
6
◼ construct: construct an empty stack
◼ stackIsEmpty → bool : return True if stack
is empty
◼ stackIsFull → bool : return True if stack is
full
◼ push(el) : add element (el) at the top
◼ pop(el): retrieve and remove the top element
◼ stackTop(el): retrieve top element without
removing it
7
◼The stack may be implemented as a
dynamic array.
◼The capacity (MaxSize) will be input as
a parameter to the constructor (default
is 128)
◼The stack ADT will be implemented as a
template class to allow for different
element types.

8
// File: Stackt.h
// Stack template class definition.
// Dynamic array implementation

#ifndef STACKT_H
#define STACKT_H

template <class Type>

class Stackt
{
public:

Stackt (int nelements = 128); // Constructor


Stackt (const Stackt <Type> &); // Copy Constructor
~Stackt (); // Destructor

9
// Member Functions
void push(Type ); // Push
void pop(Type &); // Pop
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
bool stackIsFull() const; // Test for Full stack

private:
Type *stack; // pointer to dynamic array
int top, MaxSize;
};

#endif // STACKT_H
#include "Stackt.cpp"

10
◼A stack can be implemented as a linked
structure.
◼Requires more space than array
implementations, but more flexible in size.
◼Easy to implement because operations are
at the top (in this case the head node)

11
// The linked structure for a node can be
// specified as a Class in the private part of
// the main stack class.
class node // Hidden from user
{
public:
Type e; // stack element
node *next; // pointer to next node
}; // end of class node declaration

typedef node * NodePointer;

NodePointer top; // pointer to top

12
First Last

top
3 2
New
push(v):
NodePointer pnew = new
node ;
1 pnew->e = v;
pnew
pnew->next = top;
top = pnew;
13
First Last

top
3 2
New
push(v):
NodePointer pnew = new
node ;
1 pnew->e = v;
pnew
pnew->next = top;
top = pnew;
14
1
cursor
top

2
Type pop():
Type v = top->e;
cursor = top;
top = top->next;
delete cursor;
Return v;
15
// File: StackL.h
// Linked List Stack class definition
#ifndef STACKL_H
#define STACKL_H
template <class Type>
class StackL
{
public:

StackL(); // Constructor
~StackL(); // Destructor
void push(Type ); // Push
void pop(Type &);// Pop

16
void stackTop(Type &) const; // retrieve top
bool stackIsEmpty() const; // Test for Empty stack
private:
// Node Class
class node
{
public:
Type e; // stack element
node *next; // pointer to next node
}; // end of class node declaration

17
typedef node * NodePointer;
NodePointer top; // pointer to top
};
#endif // STACKL_H
#include "StackL.cpp"

18
◼Balancing Enclosure Symbols
◼Evaluation of Postfix Expressions
◼Backtracking
◼Conversion from Decimal to
Hexadecimal (Self study)
◼Converting Infix Expressions to Postfix
(Self study)

19
Given a text file containing a sequence of characters, we
want to check for balancing of the symbols ( ) , [ ] , { }.
Algorithm:
bool EnclosureBalance (filename)
{
Open file filename;
Initialize an empty stack of characters;
balanced = true;
for each character (ch) read until end of file :
{
If (ch is a left symbol) push ch on the stack;

20
else if (ch is a right symbol) then
if (stack is empty) balanced = false;
else
{
pop the stack;
if (popped symbol is not the corresponding
left symbol) balanced = false;
}
}
At the end of the file,
if (stack is not empty) balanced = false;
return balanced;
}

21
◼ Regular expressions are written in “infix”
notation, i.e., operator between two operands,
e.g.,
(A+B) * (C- (D+E))
◼ Parentheses are used to force precedence
◼ Reverse Polish Notation (RPN) or “postfix”
does without parentheses. e.g. the above
expression is:
AB+CDE+-*
◼ Postfix expressions like A B + are evaluated as
A+B
22
The idea is:
◼ Scan from left to right until an operator (+,-,*,/)
is encountered.
◼ Apply operator between the previous
operands.
◼ Replace the two previous operands by the
result.

This suggests to use a stack to store operands


and the results.
23
A B + C D E + - *
R1 C D E + - *
R1 C R2 - *
R1 C R2 - *
R1 R3 *
R1 R3 * = RESULT
24
(2+3) * (2- (4+1)) → 2 3 + 2 4 1 + - *
2 3 + 2 4 1 + - *
5 2 4 1 + - *
5 2 5 - *
5 2 5 - *
5 -3 *
5 -3 * = RESULT = -15

25
◼ Initialize a stack (S) of characters
◼ For each character from left to right
◼ Get next character
◼ If operand, push it on S
◼ If an operator:
– Pop two values (error if there are no two values)
– Apply operator
– Push result back onto (S)
◼ At the end, result is on top of (S) (the only
value, otherwise an error)

26
◼ A Maze can be modeled as nodes (decision points) and edges
(tracks). A Backtracking method can be used to find the way
tointhe exit starting from an entrance.

A
out F G

B
E

C
D

27
◼ We may choose to move in the order:
South – East – North – West
◼ A stack is used to record the tracks.
◼ When we move on a new track, we push it on
the stack.
◼ When we run out of tracks, we backtrack by
popping the last track from the stack.

Later in the course, we will do this using a


recursive algorithm using the system stack.
The algorithm is called Depth First Search
28

You might also like