1 / 21

The Stack Data Structure

The Stack Data Structure. Classic structure. An abstract data type in which accesses are made at only one end Last In First Out (LIFO) Typical Functions Constructor: set data to valid state Push: add data to TOP of stack Pop: delete data at TOP of stack

keiran
Download Presentation

The Stack 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. The Stack Data Structure

  2. Classic structure

  3. An abstract data type in which accesses are made at only one end • Last In First Out (LIFO) • Typical Functions • Constructor: set data to valid state • Push: add data to TOP of stack • Pop: delete data at TOP of stack • Peek: view or return data at TOP of stack • Typical Data • Size: total size of stack • IsEmpty: is there any data? • Top: where is the top of the stack? • Linear collection What is a Stack?

  4. Usage • Constructor creates an empty stack • Call push function to add objects, pop function to remove • Limited-access container • Can only add/remove from top of stack • Why??? • Useful for • Reversing a sequence • Managing a series of undoable actions • Tracking history (web browsing, undo operations) • Prevents making mistakes to protected data • The client doesn’t have to remember last push to get it back or delete it. Why Use a Stack?

  5. Push and Pop http://www.youtube.com/watch?v=ggogs3P73Ok http://www.youtube.com/watch?v=A_SobdSCY4Y Animations

  6. Array • Linked List Stack Underlying Structure

  7. #include vector template <class Item> class MyStack { public: MyStack(); bool isEmpty(); //can use vector’s empty() int size(); //can use vector’s size() void push(Item e); void pop(); Item peek(); private: vector<Item> elems; }; Stack Interface Using an Array

  8. Push operations: Beginning of the Array? Which End of the Array is Top?

  9. Push operations: Beginning of the Array? • Possible, but must move any existing data over to make room for new entries—HARD • Push operations: End of the Array? Which End of the Array is Top?

  10. Push operations: Beginning of the Array? • Possible, but must move any existing data over to make room for new entries—HARD • Push operations: End of the Array? • Possible and when space is available no shuffling needed—EASY • Pop operations: Beginning of the Array? Which End of the Array is Top?

  11. Push operations: Beginning of the Array? • Possible, but must move any existing data over to make room for new entries—HARD • Push operations: End of the Array? • Possible and when space is available no shuffling needed—EASY • Pop operations: Beginning of the Array? • Possible, but must move any existing data up to the top—HARD • Pop operations: End of the Array? Which End of the Array is Top?

  12. Push operations: Beginning of the Array? • Possible, but must move any existing data over to make room for new entries—HARD • Push operations: End of the Array? • Possible and when space is available no shuffling needed—EASY • Pop operations: Beginning of the Array? • Possible, but must move any existing data up to the top—HARD • Pop operations: End of the Array? • Possible and no shuffling is needed when numUsed is tracked—EASY Which End of the Array is Top?

  13. Push operations: End of the Array! • Possible and when space is available no shuffling needed—EASY • Pop operations: End of the Array! • Possible and no shuffling is needed when numUsed is tracked—EASY Which End of the Array is Top?

  14. template <class Item> class MyStack { public: MyStack(); bool isEmpty(); void push(Item e); void pop(); Item peek(); private: struct cellT{ Item val; cellT *next; }; cellT *head; }; Stack Interface Using a Linked List

  15. Push operations: Beginning of the List? Which End of the List is Top?

  16. Push operations: Beginning of the List? • We know where the head pointer is—EASY • Push operations: End of the List? Which End of the List is Top?

  17. Push operations: Beginning of the List? • We know where the head pointer is—EASY • Push operations: End of the List? • Possible, but would require traversing the list—HARD • With a tail pointer—Easy • Pop operations: Beginning of the List? Which End of the List is Top?

  18. Push operations: Beginning of the List? • We know where the head pointer is—EASY • Push operations: End of the List? • Possible, but would require traversing the list—HARD • With a tail pointer—Easy • Pop operations: Beginning of the List? • We know where the head pointer is—Easy • Pop operations: End of the List? Which End of the List is Top?

  19. Push operations: Beginning of the List? • We know where the head pointer is—EASY • Push operations: End of the List? • Possible, but would require traversing the list—HARD • With a tail pointer—Easy • Pop operations: Beginning of the List? • We know where the head pointer is—Easy • Pop operations: End of the List? • Possible, but would require traversing the list with a trailing cursor—HARD • Not made easier with a tail pointer (where is the last node?) Must traverse the list--HARD Which End of the List is Top?

  20. Push operations: Beginning of the List! • We know where the head pointer is—EASY • Pop operations: Beginning of the List! • We know where the head pointer is—Easy Which End of the List is Top?

  21. using namespace std; int main() { MyStack<int> s; s.push(1); s.push(2); s.push(3); while (!isEmpty()) cout << s.pop() << endl; return 0; } Client Use of Stack

More Related