Lec 10,11 Stacks
-
Upload
raja-mustafa -
Category
Documents
-
view
227 -
download
0
Transcript of Lec 10,11 Stacks
-
8/14/2019 Lec 10,11 Stacks
1/40
Lecture-08
Kiran Ijaz
August 20th, 2008
1
-
8/14/2019 Lec 10,11 Stacks
2/40
StacksA Stack is a special kind of list in which all insertions
and deletions take place at one end, called the Top
2
Other Names
Pushdown List
Last In First Out (LIFO)
-
8/14/2019 Lec 10,11 Stacks
3/40
StacksExamples:
Books on a floor
Dishes on a shelf
3
-
8/14/2019 Lec 10,11 Stacks
4/40
Common Operations on
Stacks1. MAKENULL(S): Make Stack S be an empty
stack.
2. TOP(S
): Return the element at the top ofstack S.
3. POP(S): Remove the top element of thestack.
4. PUSH(S): Insert the elementxat the top ofthe stack.
5. EMPTY(S): Return true if S is an emptystack; return false otherwise.
4
-
8/14/2019 Lec 10,11 Stacks
5/40
Static and Dynamic
StacksThere are two kinds of stack data structure -
a) static, i.e. they have a fixed size, and areimplemented asarrays.
b) dynamic, i.e. they grow in size as needed,
and implemented as linked lists
5
-
8/14/2019 Lec 10,11 Stacks
6/40
Push and Pop operations of
Stack
6
-
8/14/2019 Lec 10,11 Stacks
7/40
An Array Implementation ofStacks
First Implementation Elements are stored in contiguous cells of an array.
New elements can be inserted to the top of the list.
7
Last Element
Second Element
First Element
List
Emptymaxlength
top
-
8/14/2019 Lec 10,11 Stacks
8/40
An Array Implementation ofStacks
8
Problem with this implementation
Every PUSH and POP requires moving the entirearray up and down.
1
1
2
1
2
3
1
2
-
8/14/2019 Lec 10,11 Stacks
9/40
An Array Implementation ofStacks
Since, in a stack the insertion and deletiontake place only at the top, so
9
A better Implementation: Anchor the bottom of the stack at the
bottom of the array Let the stack grow towards the top of the
array Top indicates the current position of the
first stack element.
-
8/14/2019 Lec 10,11 Stacks
10/40
An Array Implementation ofStacks
10
A better Implementation:
First Element
Last Elementmaxlength
top
Second Element
1
2.
.
-
8/14/2019 Lec 10,11 Stacks
11/40
A Stack Class#ifndef 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
-
8/14/2019 Lec 10,11 Stacks
12/40
Implementation//*******************// Constructor *//*******************
IntStack::IntStack(int size){
stackArray = new int[size];stackSize = size;
top = -1;}
12
-
8/14/2019 Lec 10,11 Stacks
13/40
Push// Member function push pushes theargument onto *// the stack. *void IntStack::push(int num){
if (isFull())cout
-
8/14/2019 Lec 10,11 Stacks
14/40
// 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
-
8/14/2019 Lec 10,11 Stacks
15/40
15
//***************************************************
// 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;
}
-
8/14/2019 Lec 10,11 Stacks
16/40
16
//**********************************************
// 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;}
-
8/14/2019 Lec 10,11 Stacks
17/40
17
// This program demonstrates the IntStack class.
#include
#include "intstack.h
void main(void)
{
IntStack stack(5);
int catchVar;
cout
-
8/14/2019 Lec 10,11 Stacks
18/40
18
cout
-
8/14/2019 Lec 10,11 Stacks
19/40
19
Program Output
Pushing 5Pushing 10
Pushing 15
Pushing 20
Pushing 25Popping...
25
20
15
10
5
-
8/14/2019 Lec 10,11 Stacks
20/40
20
About Program 1
In the program, the constructor is called
with the argument 5. This sets up the
member variables as shown in Figure 1.Since top is set to 1, the stack is empty
-
8/14/2019 Lec 10,11 Stacks
21/40
21
Figure 3 shows the state of the member
variables after all five calls to thepushfunction. Now the top of the stack is at
element 4, and the stack is full.
-
8/14/2019 Lec 10,11 Stacks
22/40
22
Notice that the pop function uses a reference
parameter, num.
The value that is popped off the stack is copied into
num so it can be used later in the program.
Figure 4 (on the next slide) depicts the state ofthe class members, and the num parameter, just
after the first value is popped off the stack.
-
8/14/2019 Lec 10,11 Stacks
23/40
23
Implementing other Stack Operations
More complex operations can be built on the basic stack class we
have just described, e.g. a class called MathStack.
MathStackhas 2 member functions :- add( ) sub( )
-
8/14/2019 Lec 10,11 Stacks
24/40
Stack TemplatesThe stack class so far work with integers only.A stack template can be used to work withany data type.
24
-
8/14/2019 Lec 10,11 Stacks
25/40
A Linked-List Implementation ofStacks
Stack can expandor shrinkwith each PUSH orPOP operation.
PUSH and POP operate only on the header celland the first cell on the list.
25
x y
.z
Top
-
8/14/2019 Lec 10,11 Stacks
26/40
Linked List Implementation of
Stackclass Stack{
struct node{
int data;node *next;
}*top;public:
void Push(int newelement);int Pop(void);bool IsEmpty();
};
26
-
8/14/2019 Lec 10,11 Stacks
27/40
void Stack::Push(int newelement){
node *newptr;newptr=new node;newptr->data=newelement;newptr->next=top;top=newptr;
}
int Stack:Pop(void){
if (IsEmpty()) { coutnext;delete tempptr;return returnvalue;
}
27
-
8/14/2019 Lec 10,11 Stacks
28/40
void Stack::IsEmpty()
{
if (top==NULL) return true;else return false;
}
28
-
8/14/2019 Lec 10,11 Stacks
29/40
29
Program 3
// This program demonstrates the dynamic stack
// class DynIntClass.
#include
#include "dynintstack.h
void main(void){
DynIntStack stack;
int catchVar;
cout
-
8/14/2019 Lec 10,11 Stacks
30/40
30
cout
-
8/14/2019 Lec 10,11 Stacks
31/40
31
-
8/14/2019 Lec 10,11 Stacks
32/40
INFIX, POSTFIX and PREFIXInfix: A+B-C
Postfix: AB+C-Prefix: -+ABC
Order of Precedence of OperatorsExponentiationMultiplication/Division
Addition/Subtraction
32
-
8/14/2019 Lec 10,11 Stacks
33/40
Infix: ( (A+B)*C-(D-E) ) $
(F+G)Conversion to Postfix Expression( (AB+)*C-(DE-) ) $ (FG+)
( (AB+C*)-(DE-) ) $ (FG+)(AB+C*DE--) $ (FG+)AB+C*DE- -FG+$
Exercise: Convert the following to Postfix( A + B ) * ( C D)A $ B * C D + E / F / (G + H)
33
-
8/14/2019 Lec 10,11 Stacks
34/40
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 ratherthan after them.
Evaluating a Postfix Expression
Each operator in a postfix string refers to the previoustwo operands in the string.
34
gor m o va ua e a os x
-
8/14/2019 Lec 10,11 Stacks
35/40
gor m o va ua e a os xExpression
opndstk = the empty stack//scan the input string reading oneelement
//at a time into symbwhile (not end of input) {
symb = next input character;
if (symb is an operand)push(opndstk, symb)else {/* symb is an operator */opnd2 = pop(opndstk);opnd1 = pop(opndstk);
value = result of applying symbto opnd1 and opnd2;push(opndstk, value);} /* end else */
} /* end while */return (pop(opndstk));
symb opnd1 opnd2 value opndstk
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
$ 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52 35
Example:
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
-
8/14/2019 Lec 10,11 Stacks
36/40
Conversion of Infix Expression
to postfixA+B*C = ABC*+(A+B)*C = AB+C*
There must be a precedence function.prcd(op1, op2), where op1 and op2 are characters representingoperators.
This function returns TRUE if op1 has precendence over op2 whenop1 appears to the left of op2 in an infix expression withoutparenthesis. prcd(op1,op2) returns FALSE otherwise.
For example prcd(*,+) and prcd(+,+) are TRUE whereasprcd(+,*) is FALSE.
prcd($,$) = FALSE
prcd( ( , op) = FALSE for any operator opprcd( 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
-
8/14/2019 Lec 10,11 Stacks
37/40
Algorithm to Convert Infix to Postfixopstk = the empty stack;while (not end of input) {
symb = next input character;if (symb is an operand)add symb to the postfix stringelse {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 */
symb Postfixstring
opstk
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC* +
ABC*+
37
Example-1: A+B*C
-
8/14/2019 Lec 10,11 Stacks
38/40
Algorithm to Convert Infix to Postfixopstk = the empty stack;while (not end of input) {
symb = next input character;if (symb is an operand)add symb to the postfix stringelse {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 */
symb Postfix string opstk
( (
A A (
+ A ( +
B AB ( +
) AB+
* AB+ *
C AB+C *
AB+C*
38
Example-2: (A+B)*C
-
8/14/2019 Lec 10,11 Stacks
39/40
Algorithm to Convert Infix to Postfixopstk = the empty stack;while (not end of input) {
symb = next input character;if (symb is an operand)add symb to the postfix stringelse {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
Example-3: ( (A-(B+C) ) *D ) $ (E+F)
-
8/14/2019 Lec 10,11 Stacks
40/40
Algorithm to Convert Infix to Postfix
opstk = the empty stack;while (not end of input) {
symb = next input character;if (symb is an operand)add symb to the postfix stringelse {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 */
symb Postfix string opstk
( (
( ((
A A ((
- A ((-
( A ((-(
B AB ((-(
+ AB ((-(+
C ABC ((-(+
) ABC+ ((-
) ABC+- (
* ABC+- (*
D ABC+-D (*
) ABC+-D*
$ ABC+-D* $
( ABC+-D* $(
E ABC+-D*E $(
+ ABC+-D*E $(+
F ABC+-D*EF $(+
) ABC D*EF $
Example-3: ( (A-(B+C) ) *D ) $ (E+F)