Download presentation
Presentation is loading. Please wait.
Published byYesenia Verdier Modified over 10 years ago
1
The Stack Data Structure
2
Classic structure
3
What is a Stack? 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
4
Why Use 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 doesnt have to remember last push to get it back or delete it.
5
Animations Push and Pop http://www.youtube.com/watch?v=ggogs3P73Ok http://www.youtube.com/watch?v=A_SobdSCY4Y
6
Stack Underlying Structure Array Linked List
7
Stack Interface Using an Array #include vector template class MyStack { public: MyStack(); bool isEmpty(); //can use vectors empty() int size();//can use vectors size() void push(Item e); void pop(); Item peek(); private: vector elems; };
8
Which End of the Array is Top? Push operations: Beginning of the Array?
9
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 entriesHARD Push operations: End of the Array?
10
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 entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array?
11
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 entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array? Possible, but must move any existing data up to the topHARD Pop operations: End of the Array?
12
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 entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array? Possible, but must move any existing data up to the topHARD Pop operations: End of the Array? Possible and no shuffling is needed when numUsed is trackedEASY
13
Which End of the Array is Top? Push operations: End of the Array! Possible and when space is available no shuffling neededEASY Pop operations: End of the Array! Possible and no shuffling is needed when numUsed is trackedEASY
14
Stack Interface Using a Linked List template class MyStack { public: MyStack(); bool isEmpty(); void push(Item e); void pop(); Item peek(); private: struct cellT{ Item val; cellT *next; }; cellT *head; };
15
Which End of the List is Top? Push operations: Beginning of the List?
16
Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List?
17
Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List?
18
Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List? We know where the head pointer isEasy Pop operations: End of the List?
19
Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List? We know where the head pointer isEasy Pop operations: End of the List? Possible, but would require traversing the list with a trailing cursorHARD Not made easier with a tail pointer (where is the last node?) Must traverse the list--HARD
20
Which End of the List is Top? Push operations: Beginning of the List! We know where the head pointer isEASY Pop operations: Beginning of the List! We know where the head pointer isEasy
21
Client Use of Stack using namespace std; int main() { MyStack s; s.push(1); s.push(2); s.push(3); while (!isEmpty()) cout << s.pop() << endl; return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.