Download
adt stacks n.
Skip this Video
Loading SlideShow in 5 Seconds..
ADT Stacks PowerPoint Presentation

ADT Stacks

76 Vues Download Presentation
Télécharger la présentation

ADT Stacks

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. ADT Stacks

  2. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.” A stack is also called a Last In First Out (LIFO) data structure.

  3. Stack: Logical Level Stack Operations: void clear() void push (E it) E pop () E topValue () int length();

  4. Stack: Application Level • A runtime stack of activation records (ar) is maintained as a program executes to track function calls and scopes. • Each activation record contains • space for local variables and parameters • ptr to dynamic parent • ptr to static parent • return address

  5. Consider this codeoutline: Public class SomeMethods () { // ------------------------------ public static void B ( ) { } // ------------------------------ public static void A ( ) { B (); } // ------------------------------ public static void main (String[] args ) { A (); } }

  6. Consider the following: B A Push (main’s ar) Push (A’s ar) Push (B’s ar) Use info in Top ar to return control to A Pop Use info in Top ar to return control to main Pop Use info in Top ar to return control to OS Pop main main begins executing main calls method A method A calls method B method B returns method A returns main returns Runtime Stack

  7. Stack: Application Level Stacks can be used to analyze nested expressions with grouping symbols to determine if they are well-formed (all grouping symbols occur in matching pairs and are nested properly.) ( ( {xxx} ) x [ ] xx) is well-formed ( ( {xxx} x [ ] ) is ill-formed

  8. General Algorithm ( ( { x x x } ) x [ ] x x ) get next symbol set balanced flag to true while (there are more input symbols and expression still balanced) if (next symbol is opening symbol) Push symbol onto stack else if (next symbol is closing symbol) if (stack is empty) // check that length is 0 set balanced to false else pop the stack to get top opening symbol if (opening symbol does not match closing symbol) set balanced to false else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed ( [ ( { Popped { ( [ ( Stack

  9. Stack: Implementation Level Using an array: [maxSize - 1] . . . *Note that top indicates position where next pushed item will go [0] 0 10 listArray top* maxSize

  10. Stack: Implementation Level Using an array: push ( 70 ) [MAX_ITEMS - 1] . . . 70 [0] 1 10 top maxSize listArray

  11. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) [MAX_ITEMS - 1] . . . 28 70 [0] 2 10 listArray top maxSize

  12. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) [MAX_ITEMS - 1] . . . 88 28 70 [0] 3 10 listArray top maxSize

  13. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () [MAX_ITEMS - 1] . . . 88 28 70 [0] 2 10 listArray top maxSize

  14. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) [MAX_ITEMS - 1] . . . 95 28 70 [0] 3 10 listArray top maxSize

  15. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 2 10 listArray top maxSize

  16. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 1 10 listArray top maxSize

  17. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () pop () pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 0 10 listArray top maxSize

  18. Stack: Implementation Level Using a linked list: NULL top 0 size

  19. Stack: Implementation Level Using a linked list: push ( 70 ) 70 NULL top 1 size

  20. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) 28 70 NULL top 2 size

  21. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) 88 28 70 NULL top 3 size

  22. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () 28 70 NULL top 2 size

  23. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 95 28 70 NULL top 3

  24. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 28 70 NULL top 2 size

  25. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () 70 NULL top 1 size

  26. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () pop () NULL top 0 size