stacks implementation and examples

71

Click here to load reader

Upload: greatqadirgee4u

Post on 16-Apr-2017

33.706 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Stacks Implementation and Examples

CIS-122 : Data Structures Lecture- # 2On Stacks

Conducted bySyed Muhammad Haroon , SE

Pakistan Institute of Engineering & Applied SciencesDepartment of Computer & Information Sciences

Page 2: Stacks Implementation and Examples

Outline

• Revision of Lecture # 01 Definition Representation of Stack Operations on Stack

• Lecture # 02 Applications of Stack

• Arithmetic Expression• Recursion : Factorial• Quick Sort• Tower of Hanoi

Assignment• Stack Machines• Record Management

Page 3: Stacks Implementation and Examples

REVISION OF LECTURE #1

Page 4: Stacks Implementation and Examples

What is a stack?

• Ordered group of homogeneous items.• Added to and removed from the top of the stack• LIFO: Last In, First Out.

Page 5: Stacks Implementation and Examples

BASIC STACK OPERATIONS

• Initialize the Stack. • Pop (delete an item) • Push (insert an item) • Status(Empty, Full, No of Item, Item at Top)• Clear the Stack • Determine Stack Size

Page 6: Stacks Implementation and Examples

Representation of Stacks• Arrays

Fixed Size Stack Item, top

• Link List

Dynamic Stack Node: data, link Stack Header

Page 7: Stacks Implementation and Examples

7

Array Representation of stacks

• To implement a stack, items are inserted and removed at the same end (called the top)

• To use an array to implement a stack, you need both the array itself and an integer The integer tells you either:

• Which location is currently the top of the stack, or• How many elements are in the stack

Page 8: Stacks Implementation and Examples

Pushing and popping

• To add (push) an element, either: Increment top and store the element in stk[top], or Store the element in stk[count] and increment count

• To remove (pop) an element, either: Get the element from stk[top] and decrement top, or Decrement count and get the element in stk[count]

Page 9: Stacks Implementation and Examples

9

Linked-list implementation of stacks

• Since all the action happens at the top of a stack, a singly-linked list (SLL) is a fine way to implement it

• The header of the list points to the top of the stack

44 97 23 17

myStack:

• Pushing is inserting an element at the front of the list• Popping is removing an element from the front of the list

Page 10: Stacks Implementation and Examples

10

Linked-list implementation details

• With a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem)

• Underflow can happen, and should be handled the same way as for an array implementation

• When a node is popped from a list, and the node references an object, the reference (the pointer in the node) does not need to be set to null Unlike an array implementation, it really is removed--you can no longer

get to it from the linked list Hence, garbage collection can occur as appropriate

Page 11: Stacks Implementation and Examples

EVALUATION OF ARITHMETIC EXPRESSION

Page 12: Stacks Implementation and Examples

Building an Arithmetic Expression Evaluator

Infix and Postfix Expressions:

Infix Expression Equivalent Postfix Expression

3*4+5 34*5+

Assume 1-digit integer operands and the binary operators + - * / only

3*(4+5)/2 345+*2/

Infix Expression Properties: Usual precedence and associativity of operators Parentheses used to subvert precedence

Postfix Expression Properties: Both operands of binary operators precede operator Parentheses no longer needed

(3+4)/(5-2) 34+52-/7-(2*3+5)*(8-4/2) 723*5+842/-*-3-2+1 32-1+

Page 13: Stacks Implementation and Examples

Building an Arithmetic Expression Evaluator

Postfix Expression String Processing Assume 1-digit integer operands, the binary operators + - * / only, and the string to be evaluated is properly formed

An Example: 3*(4+5)/2 345+*2/ 13 Remaining Postfix String int Stack (top) Rule Used

345+*2/ empty45+*2/ 3 15+*2/ 3 4 1+*2/ 3 4 5 1*2/ 3 9 2

2/ 27 2

Rules for processing the postfix string: Starting from the left hand end, inspect each character of the string

/ 27 2 1null 13 2

1. if it’s an operand – push it on the stack

2. if it’s an operator – remove the top 2 operands from the stack, perform the indicated operation, and push the result on the stack

value of expression at top of stack

Page 14: Stacks Implementation and Examples

Building an Arithmetic Expression Evaluator

Infix to Postfix ConversionAssume 1-digit integer operands, the binary operators + - * / only, and the string to be converted is properly formed

Rules for converting the infix string: Starting from the left hand end, inspect each character of the string

1. if it’s an operand – append it to the postfix string

2. if it’s a ‘(‘ – push it on the stack

3. if it’s an operator – if the stack is empty, push it on the stack else pop operators of greater or equal precedence and append them to the postfix string, stopping when a ‘(‘ is reached, an operator of lower precedence is reached, or the stack is empty; then push the operator on the stack

4. if it’s a ‘)’ – pop operators off the stack, appending them to the postfix string, until a ‘(‘ is encountered and pop the ‘(‘ off the stack

5. when the end of the infix string is reached – pop any remaining operators off the stack and append them to the postfix string

Page 15: Stacks Implementation and Examples

Infix to Postfix Conversion (continued)

An Example: 7-(2*3+5)*(8-4/2) 723*5+842/-*- Remaining Infix String char Stack Postfix String Rule Used

7-(2*3+5)*(8-4/2) empty null-(2*3+5)*(8-4/2) empty 7 1(2*3+5)*(8-4/2) - 7 32*3+5)*(8-4/2) -( 7 2*3+5)*(8-4/2) -( 72 13+5)*(8-4/2) -(* 72 3+5)*(8-4/2) -(* 723 35)*(8-4/2) -(+ 723* 3)*(8-4/2) -(+ 723*5 1*(8-4/2) - 723*5+ 4(8-4/2) -* 723*5+ 38-4/2) -*( 723*5+ 2-4/2) -*( 723*5+8 14/2) -*(- 723*5+8 3/2) -*(- 723*5+84 12) -*(-/ 723*5+84 3) -*(-/ 723*5+842 1null empty 723*5+842/-*- 4&5

Page 16: Stacks Implementation and Examples

RECURSION OR FACTORIAL CALCULATION

Page 17: Stacks Implementation and Examples

17

Recursion

• A recursive definition is when something is defined partly in terms of itself

• Here’s the mathematical definition of factorial:

• Here’s the programming definition of factorial: static int factorial(int n) {

if (n <= 1) return 1; else return n * factorial(n - 1);}

factorial(n) = 1, if n <= 1n * factorial(n – 1) otherwise

Page 18: Stacks Implementation and Examples

18

Supporting recursion

static int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n - 1);}

• If you call x = factorial(3), this enters the factorial method with n=3 on the stack

• | factorial calls itself, putting n=2 on the stack• | | factorial calls itself, putting n=1 on the stack• | | factorial returns 1• | factorial has n=2, computes and returns 2*1 = 2• factorial has n=3, computes and returns 3*2 = 6

Page 19: Stacks Implementation and Examples

19

Factorial (animation 1)

• x = factorial(3)

• static int factorial(int n) { //n=3 int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1

r is put on stack with value 1

All references to r use this r

All references to n use this n

3 is put on stack as n

Now we recur with 2...

Page 20: Stacks Implementation and Examples

20

Factorial (animation 2)

• r = n * factorial(n - 1);

• static int factorial(int n) {//n=2 int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1

r is put on stack with value 1

Now using this r

And this n

Now we recur with 1...

n=2

r=1

2 is put on stack as n

Page 21: Stacks Implementation and Examples

21

Factorial (animation 3)

• r = n * factorial(n - 1);

• static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1

r is put on stack with value 1Now using this r

And this n

n=2

r=1

n=1

r=1

1 is put on stack as n

Now we pop r and n off the stack and return 1 as factorial(1)

Page 22: Stacks Implementation and Examples

22

n=1

r=1

Factorial (animation 4)

• r = n * factorial(n - 1);

• static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1

Now using this rAnd this n

n=2

r=1

r=1

n=1fac=1

Now we pop r and n off the stack and return 1 as factorial(1)

Page 23: Stacks Implementation and Examples

23

Factorial (animation 5)

• r = n * factorial(n - 1);

• static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1

Now using this rAnd this n

2 * 1 is 2;Pop r and n;

Return 2

r=1

n=21

fac=2

Page 24: Stacks Implementation and Examples

24

Factorial (animation 6)

• x = factorial(3)

• static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; }}

n=3

r=1Now using this rAnd this n

3 * 2 is 6;Pop r and n;

Return 6

2r=1

n=3fac=6

Page 25: Stacks Implementation and Examples

25

Stack frames

• Rather than pop variables off the stack one at a time, they are usually organized into stack frames

• Each frame provides a set of variables and their values

• This allows variables to be popped off all at once

• There are several different ways stack frames can be implemented

n=3

r=1

n=2

r=1

n=1

r=1

Page 26: Stacks Implementation and Examples

TOWER OF HANOI

Page 27: Stacks Implementation and Examples

Towers of Hanoi

• The Towers of Hanoi is a puzzle made up of three vertical pegs and several disks that slide on the pegs

• The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly smaller ones on top

• The goal is to move all of the disks from one peg to another under the following rules: We can move only one disk at a time

We cannot move a larger disk on top of a smaller one

Page 28: Stacks Implementation and Examples

Towers of Hanoi

Original Configuration Move 1

Move 3Move 2

Page 29: Stacks Implementation and Examples

Towers of Hanoi

Move 4 Move 5

Move 6 Move 7 (done)

Page 30: Stacks Implementation and Examples

QUICK SORT

Page 31: Stacks Implementation and Examples

©Duane Szafron 1999 31

Quicksort – (1) Partition

50 60 40 90 10 80 70

0 1 2 3 4 5 6

Pivot

40 10 50

0 1 2

60 90 80 70

0 1 2 3

Page 32: Stacks Implementation and Examples

©Duane Szafron 1999 32

Quicksort – (2) recursively sort small

10 40 50

0 1 2

50 60 40 90 10 80 70

0 1 2 3 4 5 6

quicksort(small)

40 10 50

0 1 2

60 90 80 70

0 1 2 3

Page 33: Stacks Implementation and Examples

©Duane Szafron 1999 33

Quicksort – (3) recursively sort large

quicksort(large) 10 40 50

0 1 2

60 70 80 90

0 1 2 3

50 60 40 90 10 80 70

0 1 2 3 4 5 6

40 10 50

0 1 2

60 90 80 70

0 1 2 3

Page 34: Stacks Implementation and Examples

©Duane Szafron 1999 34

Quicksort – (4) concatenate

10 40 50 60 70 80 90

0 1 2 3 4 5 6

concatenate

Final resultOriginal array

10 40 50

0 1 2

60 70 80 90

0 1 2 3

50 60 40 90 10 80 70

0 1 2 3 4 5 6

40 10 50

0 1 2

60 90 80 70

0 1 2 3

Page 35: Stacks Implementation and Examples

©Duane Szafron 1999 35

In-place Partition Algorithm (1)

• Our goal is to move one element, the pivot, to its correct final position so that all elements to the left of it are smaller than it and all elements to the right of it are larger than it.

• We will call this operation partition().• We select the left element as the pivot.

60 30 10 20 40 90 70 80 50

0 1 2 3 4 5 6 7 8

rl p

Page 36: Stacks Implementation and Examples

©Duane Szafron 1999 36

In-place Partition Algorithm (2)

• Find the rightmost element that is smaller than the pivot element.

60 30 10 20 40 90 70 80 50

0 1 2 3 4 5 6 7 8

Exchange the elements and increment the left.

l

50 30 10 20 40 90 70 80 60

0 1 2 3 4 5 6 7 8

rl

p

p

rr

Page 37: Stacks Implementation and Examples

©Duane Szafron 1999 37

In-place Partition Algorithm (3)

• Find the leftmost element that is larger than the pivot element.

Exchange the elements and decrement the right.

50 30 10 20 40 60 70 80 90

0 1 2 3 4 5 6 7 8

l p

50 30 10 20 40 90 70 80 60

0 1 2 3 4 5 6 7 8

rl

r

pl

Page 38: Stacks Implementation and Examples

©Duane Szafron 1999 38

In-place Partition Algorithm (4)

• Find the rightmost element that is smaller than the pivot element.

Since the right passes the left, there is no element and the pivot is the final location.

50 30 10 20 40 60 70 80 90

0 1 2 3 4 5 6 7 8

r l p r

Page 39: Stacks Implementation and Examples

ARITHMETIC EVALUATION

Page 40: Stacks Implementation and Examples

Postfix Expression

6 5 2 3 + 8 * + 3 + *

Stackpostfix calculation

=

Page 41: Stacks Implementation and Examples

6

Postfix Expression

Stack

5 2 3 + 8 * + 3 + *

postfix calculation

=

Page 42: Stacks Implementation and Examples

6

Postfix Expression

Stack

2 3 + 8 * + 3 + *

5

postfix calculation

Page 43: Stacks Implementation and Examples

6

5

Postfix Expression

Stack

3 + 8 * + 3 + *

2

postfix calculation

=

Page 44: Stacks Implementation and Examples

6

5

Postfix Expression

Stackpostfix calculation

+ 8 * + 3 + *

2

3 =

Page 45: Stacks Implementation and Examples

Postfix Expression

Stackpostfix calculation

8 * + 3 + *

+ =

6

5

23

Page 46: Stacks Implementation and Examples

Postfix Expression

Stackpostfix calculation

(

-6

5

2 3

8 * + 3 + *

+ 3 =

Page 47: Stacks Implementation and Examples

postfix calculations

Postfix Expression

Stack

(

-6

5

8 * + 3 + *

2 + 3 =

Page 48: Stacks Implementation and Examples

d – ( e + f )

a b + c -

*

Stackpostfix calculations

Postfix Expression

Stack

(

-6

5

8 * + 3 + *

2 + 3 = 5

Page 49: Stacks Implementation and Examples

postfix calculations

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

8 * + 3 + *

5 =

Page 50: Stacks Implementation and Examples

postfix calculations

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

* + 3 + *

58

=

Page 51: Stacks Implementation and Examples

e + f )

a b + c – d *

-

(

Stackpostfix calculations

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

+ 3 + *

58

* =

Page 52: Stacks Implementation and Examples

+ f )

a b + c – d * e

-

(

Stackpostfix calculations

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

+ 3 + *

5 * 8 =

Page 53: Stacks Implementation and Examples

f )

a b + c – d * e

-

(

+

Stackpostfix calculations

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

+ 3 + *

5 * 8 =

Page 54: Stacks Implementation and Examples

)

a b + c – d * e f

-

(

+

Stackpostfix calculations

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

+ 3 + *

5 * 8 = 40

Page 55: Stacks Implementation and Examples

postfix calculations

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

+ 3 + *

40 =

Page 56: Stacks Implementation and Examples

a b + c – d * e f + -

Stackpostfix calculations

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

3 + *

40 + =

Page 57: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

5

3 + *

+ 40 =

Page 58: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

3 + *

5 + 40 =

Page 59: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

3 + *

5 + 40 = 45

Page 60: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

45

3 + *

=

Page 61: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

45

+ *

3 =

Page 62: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

45

*

3 + =

Page 63: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

45

*

+ 3 =

Page 64: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

*

45 + 3 =

Page 65: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

*

45 + 3 = 48

Page 66: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

*

=

48

Page 67: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

* =

48

Page 68: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

* 48 =

Page 69: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-

6 * 48 =

Page 70: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-

6 * 48 = 288

Page 71: Stacks Implementation and Examples

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-6

18

3 + *

a b + c – d * e f +

-

Stack

( e + f )

a b + c – d *

-

Stack

– ( e + f )

a b + c - d

*

Stack

Postfix Expression

Stack

(

-288

=