5 Stacks 1 By Juthawut Chantharamalee Data Structure
บทท 5 สแตก (Stacks) )สวนท 1( By Juthawut Chantharamalee วชาโครงสรางขอมล (Data Structure) รหส 4122202 1
Top Top Stack of coins รปท Stack of books Computer stack 5. 1 สแตก (Stacks) วชาโครงสรางขอมล (Data Structure) รหส 4122202 3
Data Top Push Operation Top Stack รปท Stack 5. 2 การ Push ขอมลลงในสแตก วชาโครงสรางขอมล (Data Structure) รหส 4122202 6
Data Top Stack Top Operation Stack รปท Stack 5. 4 การอานขอมลดวย วชาโครงสรางขอมล Top Stack Top (Data Structure) รหส 4122202 10
green Push Step 1 green Stack blue Step 2 Top green blue green Push Stack Step 3 Top blue green blue Pop green Push Stack รปท green Top red green Stack 5. 5 ฟงกชนดำเนนงานพนฐานของสแตก วชาโครงสรางขอมล Top Stack red Top Stack Step 4 Top (Data Structure) รหส 4122202 (1 ( 11
red Step 5 Top red green Stack Top Stack red Step 6 Top red green Pop Top green Stack green Step 7 Top green Pop Stack รปท Stack 5. 5 ฟงกชนดำเนนงานพนฐานของสแตก วชาโครงสรางขอมล (Data Structure) รหส 4122202 (2 ( 12
count head stack count <integer> top <node pointer> end stack (a) โครงสรางของโหนดสวนหวของสแตก Structure) data รปท link node data <data type> link <node pointer> end node (b) โครงสรางโหนดขอมลสแตก Structure) 5. 8 โครงสรางขอมลของสแตก Structure) วชาโครงสรางขอมล (Stack Head (Stack Node (Stack Data (Data Structure) รหส 4122202 21
Algorithm Create. Stack Creates and initializes metadata structure. Pre nothing Post structure created and initialized Return stack head 1 allocate memory for stack head 2 set count to 0 3 set top to null 4 return stack head end Create. Stack อลกอรทมท วชาโครงสรางขอมล 5. 1 Create Stack (Data Structure) รหส 4122202 25
pnew green data link stack รปท 0 count top 1 count top (a) Before (b) After 5. 10 ตวอยาง green Push ขอมลลงในสแตกยงไมมขอมล วชาโครงสรางขอมล (Data Structure) รหส 4122202 27
pnew red data pnew top stack 2 count top blue green stack blue data top 3 count top green data top blue data link green data link (a) Before (b) After รปท 5. 11 ตวอยางการ ขอมลลงในสแตกตอจากขอมลทมอยเดม วชาโครงสรางขอมล red data red blue green Push (Data Structure) รหส 4122202 29
Algorithm push. Stack (stack, data) Insert (push) one item into the stack. Pre stack passed by reference data contain data to be pushed in stack Post data have been pushed in stack 1 allocate memory for stack head 2 Store data in new data 3 make current top node the second node 4 make new data the top 5 increment stack count end push. Stack อลกอรทมท วชาโครงสรางขอมล 5. 2 Push Stack (Data Structure) รหส 4122202 30
stack dlt. Ptr 3 count top stack red data top dlt. Ptr 2 count top blue data link green data link (a) Before รปท Recycled 5. 12 ตวอยางการ วชาโครงสรางขอมล (b) After Pop ขอมล red ออกจากสแตก (Data Structure) รหส 4122202 32
Algorithm push. Stack (stack, data. Out) This algorithm pops the item on the top of the stack and returns it to the user. Pre stack passed by reference data. Out is reference variable to receive data Post data have been return to calling algorithm Return true if successful; false if underflow 1 if (stack empty) 1 set success to false 2 else 1 set data. Out to data in top node 2 make second node the top node 3 decrement stack count 4 set success to true 3 end if 4 return success end pop. Stack อลกอรทมท วชาโครงสรางขอมล 5. 3 Pop Stack (Data Structure) รหส 4122202 33
Algorithm stack. Top (stack, data. Out) This algorithm retrieves the data from the top of the stack without changing stack Pre stack is metadata structure to a valid stack data. Out is reference variable to receive data Post data have been return to calling algorithm Return true if data returned; false if underflow 1 if (stack empty) 1 set success to false 2 else 1 set data. Out to data in top node 2 set success to true 3 end if 4 return success end stack. Top อลกอรทมท วชาโครงสรางขอมล the 5. 4 Stack Top (Data Structure) รหส 4122202 35
Algorithm empty. Stack (stack) Determines if stack is empty and return a Boolean. Pre stack is metadata structure to a valid stack Post returns stack status Return true if stack empty, false if stack contains data 1 if (stack count 0) 1 return true 2 else 1 return false 3 end if end empty. Stack อลกอรทมท วชาโครงสรางขอมล 5. 4 Empty Stack (Data Structure) รหส 4122202 37
Algorithm full. Stack (stack) Determines if stack is full and return a Boolean. Pre stack is metadata structure to a valid stack Post returns stack status Return true if stack full, false if memory available 1 if (memory not available) 1 return true 2 else 1 return false 3 end if end full. Stack อลกอรทมท วชาโครงสรางขอมล 5. 4 Empty Stack (Data Structure) รหส 4122202 39
Stack Count ฟงกชน Stack Count จะรเทรนคาตวเลขจำนวนสมาชกในขณะนนของสแต กไปยงผทเรยกใช ซงขนตอนวธของ Stack แสดงไดดงอลกอรทมท Algorithm. Count stack. Count (stack) 5. 7 Determines if number of elements currently in stack. Pre stack is metadata structure to a valid stack Post returns stack count Return integer count of number of elements in stack 1 return (stack count) end stack. Count อลกอรทมท วชาโครงสรางขอมล 5. 7 Stack Count (Data Structure) รหส 4122202 40
Algorithm destroy. Stack (stack) This algorithm releases all nodes back to the dynamic memory. Pre stack is pass by reference Post stack empty and all nodes delete 1 if (stack not empty) 1 loop (stack not empty) 1 delete top node 2 end loop 2 end if 3 delete stack head end destroy. Stack อลกอรทมท วชาโครงสรางขอมล 5. 8 Destroy Stack (Data Structure) รหส 4122202 42
The End Lesson 5 (Session 1) วชาโครงสรางขอมล (Data Structure) รหส 4122201 43
- Slides: 43