1 / 32

Data Structure - Stack

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

Télécharger la présentation

Data Structure - Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Stacks • Stack: what is it? • ADT • Applications • Implementation(s) • Tower of Hanoi • Maze (Maze Routing) • Mathematical Expression Chung-Ang University Spring 2012

  3. What is a stack? • Stores a set of elements in a particular order • Stack principle: LAST IN FIRST OUT • = LIFO • It means: the last element inserted is the first one to be removed • Example • Which is the first element to pick up? Chung-Ang University Spring 2012

  4. Last In First Out top B A C B A D C B A E D C B A D C B A top top top top A Chung-Ang University Spring 2012

  5. Stack Applications • Real life • Pile of books • Plate trays • More applications related to computer science • Program execution stack • Evaluating expressions Chung-Ang University Spring 2012

  6. Method Invocation And Return publicvoid a() { …; b(); …} publicvoid b() { …; c(); …} publicvoid c() { …; d(); …} publicvoid d() { …; e(); …} publicvoid e() { …; c(); …} return address in d() return address in c() return address in e() return address in d() return address in c() return address in b() return address in a() Chung-Ang University Spring 2012

  7. objects: a finite ordered list with zero or more elements.methods: for all stack Stack, item  element, max_stack_size  positive integerStack 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 TRUEelse return FALSEStack push(stack, item) ::=if (IsFull(stack)) stack_fullelse insert item into top of stack and return Stack ADT Chung-Ang University Spring 2012

  8. Boolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUEelse return FALSEElement pop(stack) ::= if(IsEmpty(stack)) returnelse remove and return the item on the top of the stack. Stack ADT (cont’d) Chung-Ang University Spring 2012

  9. Array-based Stack Implementation • Allocate an array of some size (pre-defined) • Maximum N elements in stack • Bottom stack element stored at element 0 • last index in the array is the top • Increment top when one element is pushed, decrement after pop Chung-Ang University Spring 2012

  10. 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;BooleanisEmpty(Stack) ::= top< 0;BooleanisFull(Stack) ::= top >= MAX_STACK_SIZE-1; Stack Implementation: CreateS, isEmpty, isFull Chung-Ang University Spring 2012

  11. top a b c d e 0 1 2 3 4 void push(element item){ /* add an item to the global stack */ if (top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++top] = item;} Push Chung-Ang University Spring 2012

  12. StackFull() void StackFull() { fprintf(stderr, “Stack is full, cannot add element.”); exit(EXIT_FAILURE); } Chung-Ang University Spring 2012

  13. top a b c d e 0 1 2 3 4 element pop(){ /* return the top element from the stack */ if (top == -1) return stack_empty( ); /* returns and error key */ return stack[top--]; } Pop Chung-Ang University Spring 2012

  14. 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 Chung-Ang University Spring 2012

  15. StackFull()/Dynamic Array void StackFull() { REALLOC(stack, 2*capacity*sizeof(*stack); capacity *= 2; } Chung-Ang University Spring 2012

  16. 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. Chung-Ang University Spring 2012

  17. The Towers of HanoiA Stack-based Application • GIVEN: three poles • a set of discs on the first pole, discs of different sizes, the smallest discs at the top • GOAL: 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. Chung-Ang University Spring 2012

  18. Towers of Hanoi Chung-Ang University Spring 2012

  19. Towers of Hanoi Chung-Ang University Spring 2012

  20. Towers of Hanoi Chung-Ang University Spring 2012

  21. Towers of Hanoi Chung-Ang University Spring 2012

  22. Towers of Hanoi Chung-Ang University Spring 2012

  23. Towers of Hanoi Chung-Ang University Spring 2012

  24. Towers of Hanoi Chung-Ang University Spring 2012

  25. Towers of Hanoi Chung-Ang University Spring 2012

  26. 4 3 2 1 A B C Towers Of Hanoi/Brahma • 64 gold disks to be moved from tower A to tower C • each tower operates as a stack • cannot place big disk on top of a smaller one Chung-Ang University Spring 2012

  27. 1 A B C Recursive Solution • n > 0 gold disks to be moved from A to C using B • move top n-1 disks from A to B using C Chung-Ang University Spring 2012

  28. 1 B C Recursive Solution • move top disk from A to C A Chung-Ang University Spring 2012

  29. 1 B C Recursive Solution • move top n-1 disks from B to C using A A Chung-Ang University Spring 2012

  30. 1 B C Recursive Solution • moves(n) = 0 when n = 0 • moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0 A Chung-Ang University Spring 2012

  31. 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); } Chung-Ang University Spring 2012

  32. Is the End of the World Approaching? • Problem complexity 2n • 64 gold discs • moves(64) = 1.8 * 1019 (approximately) • Given 1 move a second  600,000,000,000 years until the end of the world  Chung-Ang University Spring 2012

More Related