Lec 10,11 Stacks
Lec 10,11 Stacks
Kiran Ijaz
August 20th, 2008
1
Stacks
“A Stack is a special kind of list in which all insertions
and deletions take place at one end, called the Top”
Other Names
Pushdown List
Last In First Out (LIFO)
2
Stacks
Examples:
Books on a floor
Dishes on a shelf
3
Common Operations on Stacks
1. MAKENULL(S): Make Stack S be an empty
stack.
2. TOP(S): Return the element at the top of stack
S.
3. POP(S): Remove the top element of the stack.
4. PUSH(S): Insert the element x at the top of the
stack.
5. EMPTY(S): Return true if S is an empty stack;
return false otherwise.
4
Static and Dynamic Stacks
There are two kinds of stack data structure -
5
Push and Pop operations of Stack
6
An Array Implementation of Stacks
First Implementation
Elements are stored in contiguous cells of an array.
New elements can be inserted to the top of the list.
Empty
maxlength
7
An Array Implementation of Stacks
1
3
2
2
1
top
1 First Element
2 Second Element
.
.
maxlength Last Element
10
A#ifndef
Stack Class
INTSTACK_H
#define INTSTACK_H
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int);
void push(int);
void pop(int &);
bool isFull(void);
bool isEmpty(void);
};
#endif
11
Implementation
//*******************
// Constructor *
//*******************
IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
12
Push
// Member function push pushes the argument
onto *
// the stack. *
void IntStack::push(int num)
{
if (isFull())
cout << "The stack is full.\n";
else
{
top++;
stackArray[top] = num;
}
}
13
// Member function pop pops the value at the top
// of the stack off, and copies it into the variable
// passed as an argument.
void IntStack::pop(int &num)
{
if (isEmpty())
cout << "The stack is empty.\n";
else
{
num = stackArray[top];
top--;
}
}
14
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************
bool IntStack::isFull(void)
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
15
//**********************************************
// Member funciton isEmpty returns true if the
//stack *
// is empty, or false otherwise.*
//***********************************************
bool IntStack::isEmpty(void)
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
16
// This program demonstrates the IntStack class.
#include <iostream.h>
#include "intstack.h“
void main(void)
{
IntStack stack(5);
int catchVar;
18
Program Output
Pushing 5
Pushing 10
Pushing 15
Pushing 20
Pushing 25
Popping...
25
20
15
10
5
19
About Program 1
20
• Figure 3 shows the state of the member
variables after all five calls to the push
function. Now the top of the stack is at
element 4, and the stack is full.
21
Notice that the pop function uses a reference
parameter, num.
24
A Linked-List Implementation of Stacks
Stack can expand or shrink with each PUSH or POP
operation.
PUSH and POP operate only on the header cell and
the first cell on the list.
Top
x y z .
25
Linked List Implementation of Stack
class Stack
{
struct node
{
int data;
node *next;
}*top;
public:
void Push(int newelement);
int Pop(void);
bool IsEmpty();
};
26
void Stack::Push(int newelement)
{
node *newptr;
newptr=new node;
newptr->data=newelement;
newptr->next=top;
top=newptr;
}
int Stack:Pop(void)
{
if (IsEmpty()) { cout<<“underflow error”; return;}
tempptr=top;
int returnvalue=top->data;
top=top->next;
delete tempptr;
return returnvalue;
}
27
void Stack::IsEmpty()
{
if (top==NULL) return true;
else return false;
}
28
Program 3
#include <iostream.h>
#include "dynintstack.h“
void main(void)
{
DynIntStack stack;
int catchVar;
32
Infix: ( (A+B)*C-(D-E)
Conversion to Postfix Expression
) $ (F+G)
( (AB+)*C-(DE-) ) $ (FG+)
( (AB+C*)-(DE-) ) $ (FG+)
(AB+C*DE--) $ (FG+)
AB+C*DE- -FG+$
33
Conversion to Prefix Expression
The precedence rules for converting an expression from infix to
prefix are identical. The only change from postfix is that the
operator is placed before the operands rather than after them.
34
Algorithm to Evaluate a Postfix Expression
Example:
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
opndstk = the empty stack sym opnd opnd2 valu opndstk
//scan the input string reading one b 1 e
element
6 6
//at a time into symb
2 6,2
while (not end of input) {
symb = next input character; 3 6,2,3
if (symb is an operand) + 2 3 5 6,5
push(opndstk, symb) - 6 5 1 1
else { 3 6 5 1 1,3
/* symb is an operator */ 8 6 5 1 1,3,8
opnd2 = pop(opndstk); 2 6 5 1 1,3,8,2
opnd1 = pop(opndstk);
/ 8 2 4 1,3,4
value = result of applying symb to
opnd1 and opnd2; + 3 4 7 1,7
push(opndstk, value); * 1 7 7 7
} /* end else */ 2 1 7 7 7,2
} /* end while */ $ 7 2 49 49
return (pop(opndstk)); 3 7 2 49 49,3
+ 49 3 52 52 35
Conversion of Infix Expression to postfix
A+B*C = ABC*+
(A+B)*C = AB+C*
There must be a precedence function.
prcd(op1, op2), where op1 and op2 are characters representing operators.
This function returns TRUE if op1 has precendence over op2 when op1 appears to
the left of op2 in an infix expression without parenthesis. prcd(op1,op2)
returns FALSE otherwise.
For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’) is FALSE.
prcd(‘$’,’$’) = FALSE
prcd( ‘(‘ , op) = FALSE for any operator op
prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’
prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘
prcd( ‘)‘ ,op ) = undefined for any operator op (an error)
36
Algorithm to Convert Infix to Postfix
opstk = the empty stack; Example-1: A+B*C
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string symb Postfix opstk
else { string
while (!empty(opstk) && A A
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk); + A +
add topsymb to the postfix string; B AB +
} /* end while */
push(opstk, symb); * AB +*
} /* end else */ C ABC +*
} /* end while */ ABC* +
/* output any remaining operators */
while (!empty(opstk) ) { ABC*+
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
37
Algorithm to Convert Infix to Postfix
opstk = the empty stack; Example-2: (A+B)*C
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string symb Postfix opstk
else { string
while (!empty(opstk) && ( (
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk); A A (
add topsymb to the postfix string;
+ A (+
} /* end while */
push(opstk, symb); B AB (+
} /* end else */ ) AB+
} /* end while */
/* output any remaining operators */ * AB+ *
while (!empty(opstk) ) { C AB+C *
topsymb = pop(opstk);
add topsymb to the postfix string; AB+C*
} /* end while */
38
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
Example-3: ( (A-(B+C) ) *D ) $ (E+F)
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
39
Algorithm to Convert Infix to Postfix
Example-3: ( (A-(B+C) ) *D ) $ (E+F)
opstk = the empty stack; symb Postfix string opstk
while (not end of input) { ( (
if (symb is an operand) A A ((
else { ( A ((-(