Top Banner
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5
48

Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Jan 14, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Stacks

CS 302 – Data StructuresSections 5.1, 5.2, 6.1, 6.5

Page 2: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Warning

• Although data structure concepts between this and last semesters will be the same, there might be implementation differences.

• Your answers in questions in quizzes and exams should be based on this semester’s (CS302) implementations!

Page 3: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Definitions

• ImageType.h

• ImageType.cpp

• Driver.cpp

class specification

class implementation

application (client code)

Page 4: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Question

• In object oriented programming, class implementation details are hidden from applications that use the class.

• Why?

Page 5: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

What is a stack?• It is an ordered group of homogeneous items.

• Items are added to and removed from the top of the stack

LIFO property: Last In, First Out

• The last item added would be the first to be removed

TOP OF THE STACK TOP OF THE STACK

Page 6: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Stack Implementations

Array-based

Linked-list-based

Page 7: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stackstemplate<class ItemType>class StackType { public: StackType(int); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top, maxStack;

ItemType *items;};

dynamically allocated array

Page 8: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stacks (cont’d)

template<class ItemType>StackType<ItemType>::StackType(int size){

top = -1; maxStack = size; items = new ItemType[max];}

template<class ItemType>StackType<ItemType>::~StackType(){

delete [ ] items;}

O(1)

O(1)

Page 9: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stacks (cont’d)

template<class ItemType>void StackType<ItemType>::MakeEmpty(){ top = -1;} O(1)

Page 10: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stacks (cont.)

template<class ItemType>bool StackType<ItemType>::IsEmpty() const{

return (top == -1);}

template<class ItemType>bool StackType<ItemType>::IsFull() const{

return (top == maxStack-1);} 

O(1)

O(1)

Page 11: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Push (ItemType newItem)

• Function: Adds newItem to the top of the stack.

• Preconditions: Stack has been initialized and is not full.

• Postconditions: newItem is at the top of the stack.

Page 12: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Stack overflow

• The condition resulting from trying to push an element onto a full stack.

if(!stack.IsFull())

stack.Push(item);

Page 13: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stacks (cont.)

template<class ItemType>void StackType<ItemType>::Push(ItemType newItem){

top++; items[top] = newItem;} O(1)

Page 14: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Pop (ItemType& item)

• Function: Removes topItem from stack and returns it in item.

• Preconditions: Stack has been initialized and is not empty.

• Postconditions: Top element has been removed from stack and item is a copy of the removed element.

Page 15: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Stack underflow

• The condition resulting from trying to pop an empty stack.

if(!stack.IsEmpty())

stack.Pop(item);

Page 16: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-based Stacks (cont.)

template<class ItemType>void StackType<ItemType>::Pop(ItemType& item){ item = items[top]; top--;}

O(1)

Page 17: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Templates

• Templates allow the compiler to generate multiple versions of a class type by allowing parameterized types.

• Compiler generates distinct class types and gives its own internal name to each of the types.

Page 18: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Compiling Templates

• Cannot anymore compile StackType.cpp separately from the application of the class (e.g., driver.cpp)

• Compiler needs to know the data type of the stack to instantiate the class!

• Where can the compiler find this information?

Page 19: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Compiling Templates (cont’d) // Client codeStackType<int> myStack;StackType<float> yourStack;StackType<StrType> anotherStack; myStack.Push(35);yourStack.Push(584.39); 

Page 20: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Compiling Templates (cont’d)

• Must compile StackType.cpp and client code (e.g., driver.cpp) together!

(1) Use “include” directive to include StackType.cpp StackType.cpp at the end of StackType.h StackType.h

(2) “include” StackType.hStackType.h in client’s code

(3) Compile client code

Page 21: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Linked-list-based Stacks template<class ItemType>

struct NodeType<ItemType> { ItemType info; NodeType<ItemType>* next;};

Page 22: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Linked-list-based Stacks (cont’d)template<class ItemType>struct NodeType<ItemType>; 

template<class ItemType>class StackType { public: StackType(); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: NodeType<ItemType>* topPtr;};

Page 23: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Linked-list-based Stacks (cont’d)template<class ItemType>StackType<ItemType>::StackType()StackType(){

topPtr = NULL;} 

template<class ItemType>void StackType<ItemType>::MakeEmpty()MakeEmpty(){

NodeType<ItemType>* tempPtr; 

while(topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }}

O(N)

O(1)

Page 24: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Linked-list-based Stacks (cont’d)

template<class ItemType>bool StackType<ItemType>::IsEmpty()IsEmpty() const{ return(topPtr == NULL);} 

template<class ItemType>StackType<ItemType>::~StackType()StackType(){ MakeEmpty();}

O(1)

O(N)

Page 25: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Linked-list-based Stacks (cont’d) 

template<class ItemType>bool StackType<ItemType>::IsFull()IsFull() const{ NodeType<ItemType>* location;  location = new NodeType<ItemType>; // test if(location == NULL) return true; else { delete location; return false; }}

O(1)

Page 26: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Push (ItemType newItem)

• Function: Adds newItem to the top of the stack.

• Preconditions: Stack has been initialized and is not full.

• Postconditions: newItem is at the top of the stack.

Page 27: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Pushing on a non-empty stack

Page 28: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Pushing on a non-empty stack (cont.)

• The order of changing the pointers is important!

Page 29: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Special Case: pushing on an empty stack

Page 30: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Function Push template <class ItemType>void StackType<ItemType>::PushPush(ItemType

item){

NodeType<ItemType>* location; 

location = new NodeType<ItemType>; location->info = newItem; location->next = topPtr; topPtr = location;}

O(1)

Page 31: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Pop (ItemType& item)

• Function: Removes topItem from stack and returns it in item.

• Preconditions: Stack has been initialized and is not empty.

• Postconditions: Top element has been removed from stack and item is a copy of the removed element.

Page 32: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Popping the top element

Page 33: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Popping the top element(cont.)

Need a temporary pointer !

Page 34: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Special case: popping the last element on the stack

tempPtr

Page 35: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Function Poptemplate <class ItemType>void StackType<ItemType>::PopPop(ItemType& item){ NodeType<ItemType>* tempPtr;  item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;}

O(1)

Page 36: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Comparing stack implementations Big-O Comparison of Stack Operations

Operation Array Implementation

Linked Implementation

Constructor O(1) O(1)

MakeEmpty O(1) O(N)

IsFull O(1) O(1)

IsEmpty O(1) O(1)

Push O(1) O(1)

Pop O(1) O(1)

Destructor O(1) O(N)

Page 37: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Array-vs Linked-list-based Stack Implementations

• Array-based implementation is simple but:– The size of the stack must be determined when

a stack object is declared.– Space is wasted if we use less elements.– We cannot "enqueue" more elements than the

array can hold.

• Linked-list-based implementation alleviates these problems but time requirements might increase.

Page 38: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Example using stacks: evaluate postfix expressions

• Postfix notation is another way of writing arithmetic expressions.

• In postfix notation, the operator is written after the two operands.

infix: 2+5 postfix: 2 5 +

• Why using postfix notation? 

Precedence rules and parentheses are not required!

Page 39: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Example: postfix expressions(cont.)

Expressions are evaluated from left to right.

Page 40: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Postfix expressions: Algorithm using stacks (cont.)

Page 41: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercise 15: Write the body for a client function that replaces each copy of an item in a stack with another item. Use the following specification.

ReplaceItem(StackType& stack, ItemType oldItem, ItemType

newItem)

Function: Replaces all occurrences of oldItem with newItem.Precondition: stack has been initialized.Postconditions: Each occurrence of oldItem in stack has been replaced by newItem. Order of other elements remains unchanged.

Warning: you may not assume any knowledge of how the stack is implemented!

Page 42: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

{

ItemType item;

StackType tempStack;

while (!Stack.IsEmpty()) {

Stack.Pop(item);

if (item==oldItem)

tempStack.Push(newItem);

else

tempStack.Push(item);

}

while (!tempStack.IsEmpty()) {

tempStack.Pop(item);

Stack.Push(item);

}

}

1

2

3

3

5

1

1

5

3

Stack

Stack

tempStack

oldItem = 2

newItem = 5

Page 43: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

{

ItemType item;

StackType tempStack;

while (!Stack.IsEmpty()) {

Stack.Pop(item);

if (item==oldItem)

tempStack.Push(newItem);

else

tempStack.Push(item);

}

while (!tempStack.IsEmpty()) {

tempStack.Pop(item);

Stack.Push(item);

}

}

O(N)

What are the time requirements using big-O?

Page 44: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercises 19, 20

Page 45: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercises 19, 20

Page 46: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercise 20

small large

Page 47: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercise 20 (cont’d)

etc.

Page 48: Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.

Exercise 20 (cont’d)