250 likes | 449 Views
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
E N D
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?
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?
Push and Pop http://www.youtube.com/watch?v=ggogs3P73Ok http://www.youtube.com/watch?v=A_SobdSCY4Y Animations
Array • Linked List Stack Underlying Structure
#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
Push operations: Beginning of the Array? Which End of the Array is Top?
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?
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?
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?
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?
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?
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
Push operations: Beginning of the List? Which End of the List is Top?
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?
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?
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?
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?
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?
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