Upload
keiran
View
80
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
The Stack Data Structure
Classic structure
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
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 doesn’t have to remember last push to get it back or delete it.
Animations
Push and Pophttp://www.youtube.com/watch?v=ggogs3P73Ok
http://www.youtube.com/watch?v=A_SobdSCY4Y
Stack Underlying Structure
ArrayLinked List
Stack Interface Using an Array
#include vectortemplate <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;
};
Which End of the Array is Top?
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—HARDPop 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—
HARDPop 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
Stack Interface Using a Linked List
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;
};
Which End of the List is Top?
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
Client Use of Stack
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;
}