260 likes | 354 Views
ADT Stacks. 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 L ast I n F irst O ut ( LIFO ) data structure. Stack: Logical Level. Stack Operations: void clear()
E N D
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.
Stack: Logical Level Stack Operations: void clear() void push (E it) E pop () E topValue () int length();
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
Consider this codeoutline: Public class SomeMethods () { // ------------------------------ public static void B ( ) { } // ------------------------------ public static void A ( ) { B (); } // ------------------------------ public static void main (String[] args ) { A (); } }
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
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
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
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
Stack: Implementation Level Using an array: push ( 70 ) [MAX_ITEMS - 1] . . . 70 [0] 1 10 top maxSize listArray
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) [MAX_ITEMS - 1] . . . 28 70 [0] 2 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) [MAX_ITEMS - 1] . . . 88 28 70 [0] 3 10 listArray top maxSize
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
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
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
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
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
Stack: Implementation Level Using a linked list: NULL top 0 size
Stack: Implementation Level Using a linked list: push ( 70 ) 70 NULL top 1 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) 88 28 70 NULL top 3 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 95 28 70 NULL top 3
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () 70 NULL top 1 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () pop () NULL top 0 size