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

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Chapter 7 Stacks Dr. Youssef Harrath
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Data Structures – Week #3
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 5 ADTs Stack and Queue.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Stacks.
Stacks CS-240 Dick Steflik.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Data Structures – Week #3
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

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!

Definitions ImageType.h ImageType.cpp Driver.cpp class specification class implementation application (client code)

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

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

Stack Implementations Array-based Linked-list-based

Array-based Stacks template 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

Array-based Stacks (cont’d) template StackType ::StackType(int size) { top = -1; maxStack = size; items = new ItemType[max]; } template StackType ::~StackType() { delete [ ] items; } O(1)

Array-based Stacks (cont’d) template void StackType ::MakeEmpty() { top = -1; } O(1)

Array-based Stacks (cont.) template bool StackType ::IsEmpty() const { return (top == -1); } template bool StackType ::IsFull() const { return (top == maxStack-1); } O(1)

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.

Stack overflow The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item);

Array-based Stacks (cont.) template void StackType ::Push(ItemType newItem) { top++; items[top] = newItem; } O(1)

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.

Stack underflow The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item);

Array-based Stacks (cont.) template void StackType ::Pop(ItemType& item) { item = items[top]; top--; } O(1)

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.

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?

Compiling Templates (cont’d) // Client code StackType myStack; StackType yourStack; StackType anotherStack; myStack.Push(35); yourStack.Push(584.39);

Compiling Templates (cont’d) Must compile StackType.cpp and client code (e.g., driver.cpp) together! StackType.cpp StackType.h (1)Use “include” directive to include StackType.cpp at the end of StackType.h StackType.h (2) “include” StackType.h in client’s code (3) Compile client code

Linked-list-based Stacks template struct NodeType { ItemType info; NodeType * next; };

Linked-list-based Stacks (cont’d) template struct NodeType ; template class StackType { public: StackType(); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: NodeType * topPtr; };

Linked-list-based Stacks (cont’d) template StackType() StackType ::StackType() { topPtr = NULL; } template MakeEmpty() void StackType ::MakeEmpty() { NodeType * tempPtr; while(topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } O(N) O(1)

Linked-list-based Stacks (cont’d) template IsEmpty() bool StackType ::IsEmpty() const { return(topPtr == NULL); } template StackType() StackType ::~StackType() { MakeEmpty(); } O(1) O(N)

Linked-list-based Stacks (cont’d) template IsFull() bool StackType ::IsFull() const { NodeType * location; location = new NodeType ; // test if(location == NULL) return true; else { delete location; return false; } O(1)

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.

Pushing on a non-empty stack

Pushing on a non-empty stack (cont.) The order of changing the pointers is important!

Special Case: pushing on an empty stack

Function Push template Push void StackType ::Push(ItemType item) { NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; } O(1)

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.

Popping the top element

Popping the top element (cont.) Need a temporary pointer !

Special case: popping the last element on the stack tempPtr

Function Pop template Pop void StackType ::Pop(ItemType& item) { NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } O(1)

Comparing stack implementations Big-O Comparison of Stack Operations OperationArray Implementation Linked Implementation ConstructorO(1) MakeEmptyO(1)O(N) IsFullO(1) IsEmptyO(1) PushO(1) PopO(1) DestructorO(1)O(N)

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.

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: Why using postfix notation? Precedence rules and parentheses are not required !

Example: postfix expressions (cont.) Expressions are evaluated from left to right.

Postfix expressions: Algorithm using stacks (cont.)

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!

{ 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); } Stack tempStack oldItem = 2 newItem = 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?

Exercises 19, 20

Exercise 20 small large

Exercise 20 (cont’d) etc.

Exercise 20 (cont’d)