1 / 18

Introduction to Data Structure

Introduction to Data Structure. Chapter 7 Ming Li Department of Computer Science California State University, Fresno Fall 2006. Stacks. A stack is a sequence of items that are accessible at only one end of the sequence. Pushing/Popping a Stack.

mosesg
Télécharger la présentation

Introduction to Data Structure

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. Introduction to Data Structure Chapter 7 Ming Li Department of Computer Science California State University, Fresno Fall 2006

  2. Stacks • A stack is a sequence of items that are accessible at only one end of the sequence.

  3. Pushing/Popping a Stack • Because a pop removes the item last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

  4. Stack Operations stack(); Create an empty stack. bool empty(); Check whether the stack is empty. Return true if it is empty and false otherwise. int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition: The stack is not empty. const T& top() const; Constant version of top().

  5. Stack Operations void pop(); Remove the item from the top of the stack. Precondition: The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top.

  6. Using a Stack to Create a Hex Number

  7. Uncoupling Stack Elements

  8. Uncoupling Stack Elements

  9. Uncoupling Stack Elements

  10. Uncoupling Stack Elements

  11. Uncoupling Stack Elements

  12. Uncoupling Stack Elements

  13. Stack - Copying while(!s.empty()) s.pop(); s.pop(); int i=0; while(i++<7) s.push(a[7-i]); s.push(a[7]);

  14. Stack - Reversal while(!s.empty()) s.pop(); s.pop(); int i=0; while(i++<7) s.push(a[i]); s.push(a[0]);

  15. Stack – Traversal & Search while(!s.top() !=6) s.pop(); if (s.pop() != 6) s.pop();

  16. Stack – Insertion if (s.pop() < 6) { s1.push(s.top()); s.pop(); } while(!s.top() < 6) { s1.push(s.top()); s.pop(); } s.push(6); while(!s1.empty()) { int x = s1.top(); s.push(x); } while(!s.top() < 6) { s1.push(s.top()); s.pop(); } s.push(6); while(!s.top() < 6) { s1.push(s.top()); s.pop(); }

  17. Stack Problem • How to access min() with O(1), i.e., constant time? • Postfix evaluation • Memory management

  18. System Stack for Recursion

More Related