cs10003: programming & data...

1

Upload: others

Post on 11-Sep-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

CS10003: Programming & Data Structures

Dept. of Computer Science & EngineeringIndian Institute of Technology Kharagpur

Autumn 2020

Page 2: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

2

Stacks

Page 3: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Stacks and Basic Operations

Property: Last-In First-Out (LIFO) Data Structure

Typical Operations: isEmpty: determines if the stack has no elements isFull: determines if the stack is full

(in case of a bounded sized stack) top: returns the top element in the stack push: inserts an element into the stack pop: removes the top element from the stack

Push

Pop

A B C D

D C B A

Top D

CBA

3

Page 4: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Stacks and Basic Operations

Implementation Aspects:

Using Array Pre-declared size of elements

Using Linked List top element is the head of the list push is like inserting at the front of the list pop is like deleting from the front of the list

Push

Pop

A B C D

D C B A

Top D

CBA

4

Page 5: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

5

Basic Operations over StacksInitialization:typedef struct stkArr{int data[MAX];int top;

} stack;

stack *s;s->top = -1;

Emptiness Check:int isEmpty (stack *s){if(s->top == -1)return 1;

else return 0;}

Overflow Check:int isFull (stack *s){if (s->top >= MAX–1)return 1;

else return 0;}

Seek Top Element:int top(stack *s){if(!isEmpty(s))return s->data[s->top];

}

Push Element:void push(stack *s, int elm){if(!isFull(s))s->data[++(s->top)] = elm;

}

Pop Element:void pop(stack *s){if(!isEmpty(s))--(s->top);

}

• Array based Implementation uses pre-defined fixed sized (MAX) stack, whereas

• Linked-List requires little higher space (to keep extra pointer to next node) for storing each element

Each operation takes constant

time!

Page 6: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Applications: Parenthesis Matching Problem If Only ‘(’ and ‘)’ are allowed Can be found without using Stacks

Keep a variable count and Increment or Decrement countwhen ‘(’ or ‘)’ is encountered (ignore all other characters)

Parenthesis Unbalanced if – count becomes less than zero at any intermediate point count is non-zero at end (Balanced only when count=0 at end)

Example:( ( ( ) ( ) ) ( ( ( ) ) ( ) ) )1 2 3 2 3 2 1 2 3 4 3 2 3 2 1 0( ( ) ( ) ( ( ( ) ) ) ) ( ( )1 2 1 2 1 1 2 3 2 1 0 -1 0 1 0

6

Page 7: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Parenthesis Matching Problem: Revisited If ‘( )’, ‘{ }’ and ‘[ ]’ all are allowed Three separate count variables (for each type of parenthesis)

will NOT do Check this Example: {2*(3+5}-[8-2)/3] Wrongly report Balanced if the above procedure is followed

Solution: (use Stack) Push every opening parenthesis ‘(’, ‘{’ or ‘[’ into a stack For every closing parenthesis ‘)’, ‘}’ or ‘]’, pop the top element of the

stack and match for ‘(’, ‘{’ or ‘[’, respectively If any mismatch, flag Unbalanced Otherwise report Balanced at end

7

Page 8: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Applications: Arithmetic Expression Evaluation Arithmetic Expressions

Infix (operator within operands):c*(a + b)–(d + e)/f

Prefix (operator before operands):– * c + a b / + d e f

Postfix (operator after operands):c a b + * d e + f / –

Example:expr: (2+3)*4–5 23+4*5–

(Infix) (Postfix)

Standard Format

Evaluate Postfix Expression:void postfixEval(Expression expr){initialize stack S;x get next token from expr;while(x is NOT end of expr){

if(x is operand) push(S, x);else{ // x is operator

if(!empty(S)){x1 top(S); pop(S);

}else exit with error;if(!empty(S)){x2 top(S); pop(S);

}else exit with error;result x1 <x> x2;push(S, result);

}x get next token from expr;

}output(top(S)); pop(S);if(!empty(S)) exit with error;

}

2

2

23

3

5

+

20

*

15

54

4

205

5

result 8

Page 9: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Applications: Expression Conversion Infix to Postfix Conversion

Operands in same order Operators are rearranged Operators after operands Brackets are deleted

Operators are popped out from stack whenever ISP ≥ ICP condition holds!

Evaluate Postfix Expression:void infix_postfix(Expression expr){

initialize stack S; push(S,'#');x get next token from expr;while(x is NOT end of expr){

if(x is operand) output(x);else if(x is ')'){while((y=pop(S)) != '('){output(y);

else{while(ISP(y=pop(S)) >= ICP(x))output(y);

}x get next token from expr;

}while((y=pop(S)) != '#')

output(y);}

Symbol In-Stack Priority (ISP)

In Coming Priority

(ICP))^ 3 4* / 2 2

+ – 1 1( 0 4

Example ExpressionsInfix:(((A/(B^C))+(D*E))–A*C)Postfix: A B C ^ / D E * + A C * -

9

Page 10: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Example: Revisited Infix: (((A/(B^C))+(D*E))–A*C)

Postfix: A B C ^ / D E * + A C * -

10

Symbol ISP ICP)^ 3 4* / 2 2

+ – 1 1( 0 4

(((

((( (

((/

A(((/(

(((/(^

B(((/

C ^

((

/

+((

D((+

*

*(

-(

*-(

A(

(

+( (

E((+

+ C * -

)

))

) )

Page 11: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Recursions use Stacks Implicitly! Fibonacci Number Computation (using Recursion)

Recurrence: Fib(n) = Fib(n-1) + Fib(n-2), if n > 1 and Fib(0) = Fib(1) = 1

fib (5)

fib (3) fib (4)

fib (1)

fib (2)fib (1) fib (2)

fib (0)

fib (3)

fib (1)

fib (1) fib (2)

fib (0)

fib (0) fib (1)

5 43

421

42

410

41

4 32

310

0 0 0 1 1 2 3 3 3

31

3 21

2 10

14 5 5 6 6 7 8

Fibonacci Recursion Stack

Fibonacci Recursion Tree

Nested Recursive Function Calls use

Stacks to Store Return Addresses

11

Page 12: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

12

Recursions use Stacks Implicitly! Tower of Hanoi (TOH)void towers (int n, char from, char to, char aux) {

/* Base Condition */ if (n==1) {

printf ("Disk 1 : %c -> %c \n", from, to);return ;

}/* Recursive Condition */ towers (n-1, from, aux, to);printf ("Disk %d : %c -> %c\n", n, from, to);towers (n-1, aux, to, from);

}

3,A,B,C

2,A,C,BA to B

2,C,B,A

1,A,B,CA to C

1,B,C,AA to B

2,C,B,A

A to BA to C

1,B,C,AA to B

2,C,B,A

A to C1,B,C,AA to B

2,C,B,A

1,B,C,AA to B

2,C,B,A

B to CA to B

2,C,B,AA to B

2,C,B,A 2,C,B,A

1,C,A,BC to B

1,A,B,C

TOH Recursion Stack

A B C

A B C

A B C

A B C

Page 13: CS10003: Programming & Data Structurescse.iitkgp.ac.in/.../PDS/Autumn2020/slides/Lec-34-Stack.pdf · 2021. 2. 21. · 1,C,A,B C to B 1,A,B,C TOH Recursion Stack A B C A B C A B C

Thank You!