+ All Categories
Home > Documents > Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Date post: 28-Dec-2015
Category:
Upload: dina-ellis
View: 229 times
Download: 2 times
Share this document with a friend
Popular Tags:
24
Main Index Conten ts 1 1 Main Index Conten ts Week 4 – Stacks
Transcript
Page 1: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Week 4– Stacks

Page 2: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents2

Vector List

Access an element at position/index i

Direct access, V[i]Has to iterate from a known position, may use distance function

Insert or remove an element at positions

other than end

Cause to relocate elements

No relocation, just rebuild the link

Sequential search an element with specific

value, return the position/index

Sequentially search Sequentially search

Page 3: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents33 Main IndexMain Index

ContentsContents

Container Types

Sequence Containers

Adapter Containers

Associative Containers

Vector Stack Set, Multiset

Deque Queue Map, Mutltimap

List Priority Queue

Page 4: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents4

Stacks A stack is a sequence of items that are

accessible at only one end of the sequence.

Last in, first out, (LIFO)

Page 5: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents5

Pushing/Popping a Stack Because a pop removes the item that last

added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

Page 6: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents66 Main IndexMain Index ContentsContents

CLASS stack Operations <stack>

stack();Create an empty stack

bool empty(); Check whether the stack is empty. Return true if it

is empty and false otherwise.

Page 7: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents77 Main IndexMain Index ContentsContents

CLASS stack Operations <stack>

void pop();Remove the item from the top of the stack.

Precondition: The stack is not empty.Postcondition:

Either the stack is empty or

the stack has a new topmost

item from a previous push.

void push(const T& item);Insert the argument item at the top of the stack.

Postcondition: The stack has a new item at

the top.

Page 8: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents88 Main IndexMain Index ContentsContents

CLASS stack Operations <stack>

int size() const;Return the number of items on the stack.

T& top() const;Return a reference to the value of the item at the

top of the stack.Precondition: The stack is not empty.

Page 9: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

What would be the output on screen?

Page 10: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Since stack is specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted

only from one end of the container.

Stack does not have iterators.

How do we remove/insert an element at positions other than top?

How do we search for an element with specific value?

Page 11: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1111 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

T rain B efo re U n co u p lin g EA B C D E

Page 12: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1212 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

U n co u p le E . M o v e t o s id e t rackA B C D

E

Page 13: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1313 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

U n co u p le D . M o v e t o s id e t rackA B C

D

E

Page 14: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1414 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

U n co u p le C M o v e as id eA B C

D

E

Page 15: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1515 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

A t t ach D t o en d o f t rainA B D C

E

Page 16: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents1616 Main IndexMain Index ContentsContents

Uncoupling Stack Elements

A t t ach E t o en d o f t rainA B D E C

Page 17: Main Index Contents 11 Main Index Contents Week 4 – Stacks.
Page 18: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Applications of Stack

Backtracking in a Maze:

• We start from one point, there are several paths. • Suppose we choose a random path. After following a certain path, we

realize that the path we have chosen is wrong. • So we need to find a way by which we can return to the beginning of

that path. • This is done by pushing that points that we have reached into a stack. • We can pop the last point from the stack and thus return to the last

point and continue our quest to find the right path.

Page 19: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

19

Underlying storage structure

Container classes

The storage structure should be selected so thatthe container operations can be implemented efficiently.

Page 20: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Stack implementationstacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements (pushed/popped from the "back" , which is known as the top of the stack).

The underlying container may be any of the standard container class templates (vector, list, deque). As lone as the container could support the following operations:

• empty• size• back• push_back• pop_back

Page 21: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Stack implementationtemplate <typename T>class miniStack{public:

miniStack();void push(const T& item);void pop();T& top();const T& top() const;bool empty();int size() const;

private:vector<T> stackVector;

};

Page 22: Main Index Contents 11 Main Index Contents Week 4 – Stacks.
Page 23: Main Index Contents 11 Main Index Contents Week 4 – Stacks.
Page 24: Main Index Contents 11 Main Index Contents Week 4 – Stacks.

Main IndexMain Index ContentsContents

Reading

Chapter 3.6

Next week: Queue, Tree and etc…

No HW due this weekend!

HW3 is due next Friday. (Submission before Thursday will receive 20 additional points)

24


Recommended