Data Structure - Stack

32
Data Structure - Stack Sang Yong Han http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2012 1

description

Data Structure - Stack. Sang Yong Han http://ec.cse.cau.ac.kr/. Stacks. Stack: what is it? ADT Applications Implementation(s) Tower of Hanoi Maze (Maze Routing) Mathematical Expression. What is a stack?. Stores a set of elements in a particular order - PowerPoint PPT Presentation

Transcript of Data Structure - Stack

Page 1: Data Structure - Stack

Data Structure - Stack

Sang Yong Han

http://ec.cse.cau.ac.kr/

Chung-Ang University Spring 2012 1

Page 2: Data Structure - Stack

Stacks

Stack: what is it?ADTApplicationsImplementation(s)

Tower of HanoiMaze (Maze Routing)Mathematical Expression

2Chung-Ang University Spring 2012

Page 3: Data Structure - Stack

What is a stack?

3Chung-Ang University Spring 2012

Page 4: Data Structure - Stack

Last In First Out

BA

DCBA

CBA

DCBA

EDCBAtop

toptop

toptop

A

4Chung-Ang University Spring 2012

Page 5: Data Structure - Stack

Stack Applications

Real lifePile of booksPlate trays

More applications related to computer science

Program execution stackEvaluating expressions

5Chung-Ang University Spring 2012

Page 6: Data Structure - Stack

Method Invocation And Return

public void a(){ …; b(); …}public void b(){ …; c(); …}public void c(){ …; d(); …}public void d(){ …; e(); …}public void e(){ …; c(); …}

return address in a()

return address in b()

return address in c()

return address in d()

return address in e()

return address in c()

return address in d()

6Chung-Ang University Spring 2012

Page 7: Data Structure - Stack

objects: a finite ordered list with zero or more elements. methods: for all stack Stack, item element, max_stack_size positive integer Stack createS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean isFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack push(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return

Stack ADT

7Chung-Ang University Spring 2012

Page 8: Data Structure - Stack

Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSEElement pop(stack) ::= if(IsEmpty(stack)) return else remove and return the item on the top of the stack.

Stack ADT (cont’d)

8Chung-Ang University Spring 2012

Page 9: Data Structure - Stack

Array-based Stack Implementation

Allocate an array of some size (pre-defined)

Maximum N elements in stack

Bottom stack element stored at element 0last index in the array is the topIncrement top when one element is pushed, decrement after pop

9Chung-Ang University Spring 2012

Page 10: Data Structure - Stack

Stack createS(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[MAX_STACK_SIZE]; int top = -1;

Boolean isEmpty(Stack) ::= top< 0;

Boolean isFull(Stack) ::= top >= MAX_STACK_SIZE-1;

Stack Implementation: CreateS, isEmpty, isFull

10Chung-Ang University Spring 2012

Page 11: Data Structure - Stack

void push(element item){ /* add an item to the global stack */ if (top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++top] = item;}

Push

0 1 2 3 4

a b c d e

top

11Chung-Ang University Spring 2012

Page 12: Data Structure - Stack

StackFull()

void StackFull() { fprintf(stderr, “Stack is full, cannot

add element.”); exit(EXIT_FAILURE);}

12Chung-Ang University Spring 2012

Page 13: Data Structure - Stack

element pop(){ /* return the top element from the stack */ if (top == -1) return stack_empty( ); /* returns and error key */ return stack[top--]; }

Pop

0 1 2 3 4

a b c d e

top

13Chung-Ang University Spring 2012

Page 14: Data Structure - Stack

StackFull()/Dynamic Array

Use a variable called capacity in place of MAX_STACK_SIZE

Initialize this variable to (say) 1 When stack is full, double the capacity

using REALLOC This is called array doubling

14Chung-Ang University Spring 2012

Page 15: Data Structure - Stack

StackFull()/Dynamic Array

void StackFull() { REALLOC(stack,

2*capacity*sizeof(*stack); capacity *= 2;}

15Chung-Ang University Spring 2012

Page 16: Data Structure - Stack

A LegendThe Towers of Hanoi

In the great temple of Brahma in Benares, on a brass plate under the dome that marks the center of the world, there are 64 disks of pure gold that the priests carry one at a time between these diamond needles according to Brahma's immutable law: No disk may be placed on a smaller disk. In the begging of the world all 64 disks formed the Tower of Brahma on one needle. Now, however, the process of transfer of the tower from one needle to another is in mid course. When the last disk is finally in place, once again forming the Tower of Brahma but on a different needle, then will come the end of the world and all will turn to dust.

16Chung-Ang University Spring 2012

Page 17: Data Structure - Stack

The Towers of HanoiA Stack-based Application

GIVEN: three polesa set of discs on the first pole, discs of different sizes, the smallest discs at the topGOAL: move all the discs from the left pole to the right one. CONDITIONS: only one disc may be moved at a time. A disc can be placed either on an empty pole or on top of a larger disc.

17Chung-Ang University Spring 2012

Page 18: Data Structure - Stack

Towers of Hanoi

18Chung-Ang University Spring 2012

Page 19: Data Structure - Stack

Towers of Hanoi

19Chung-Ang University Spring 2012

Page 20: Data Structure - Stack

Towers of Hanoi

20Chung-Ang University Spring 2012

Page 21: Data Structure - Stack

Towers of Hanoi

21Chung-Ang University Spring 2012

Page 22: Data Structure - Stack

Towers of Hanoi

22Chung-Ang University Spring 2012

Page 23: Data Structure - Stack

Towers of Hanoi

23Chung-Ang University Spring 2012

Page 24: Data Structure - Stack

Towers of Hanoi

24Chung-Ang University Spring 2012

Page 25: Data Structure - Stack

Towers of Hanoi

25Chung-Ang University Spring 2012

Page 26: Data Structure - Stack

Towers Of Hanoi/Brahma

A B C

1234

64 gold disks to be moved from tower A to tower Ceach tower operates as a stackcannot place big disk on top of a smaller one

26Chung-Ang University Spring 2012

Page 27: Data Structure - Stack

Recursive Solution

A B C

1

n > 0 gold disks to be moved from A to C using Bmove top n-1 disks from A to B using C

27Chung-Ang University Spring 2012

Page 28: Data Structure - Stack

Recursive Solution

A B C

1

move top disk from A to C

28Chung-Ang University Spring 2012

Page 29: Data Structure - Stack

Recursive Solution

A B C

1

move top n-1 disks from B to C using A

29Chung-Ang University Spring 2012

Page 30: Data Structure - Stack

Recursive Solution

A B C

1

moves(n) = 0 when n = 0moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0

30Chung-Ang University Spring 2012

Page 31: Data Structure - Stack

Towers of Hanoi – Recursive Solution

void hanoi (int discs,Stack fromPole, Stack toPole, Stack aux) {

Disc d;if( discs >= 1) {

hanoi(discs-1, fromPole, aux, toPole);d = fromPole.pop();toPole.push(d);hanoi(discs-1,aux, toPole, fromPole);

}

31Chung-Ang University Spring 2012

Page 32: Data Structure - Stack

Is the End of the World Approaching?

Problem complexity 2n 64 gold discsmoves(64) = 1.8 * 1019 (approximately)Given 1 move a second

600,000,000,000 years until the end of the world

32Chung-Ang University Spring 2012