+ All Categories
Home > Documents > The Stack Data Structure

The Stack Data Structure

Date post: 22-Feb-2016
Category:
Upload: keiran
View: 78 times
Download: 0 times
Share this document with a friend
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
Popular Tags:
21
The Stack Data Structure
Transcript
Page 1: The Stack Data Structure

The Stack Data Structure

Page 2: The Stack Data Structure

Classic structure

Page 3: The Stack Data 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

Page 4: The Stack Data Structure

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.

Page 6: The Stack Data Structure

Stack Underlying Structure

ArrayLinked List

Page 7: The Stack Data Structure

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;

};

Page 8: The Stack Data Structure

Which End of the Array is Top?

Push operations: Beginning of the Array?

Page 9: The Stack Data Structure

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?

Page 10: The Stack Data Structure

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?

Page 11: The Stack Data Structure

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?

Page 12: The Stack Data Structure

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

Page 13: The Stack Data Structure

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

Page 14: The Stack Data Structure

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;

};

Page 15: The Stack Data Structure

Which End of the List is Top?

Push operations: Beginning of the List?

Page 16: The Stack Data Structure

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?

Page 17: The Stack Data Structure

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?

Page 18: The Stack Data Structure

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?

Page 19: The Stack Data Structure

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

Page 20: The Stack Data Structure

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

Page 21: The Stack Data Structure

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;

}


Recommended