Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
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.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
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 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
Presentation transcript:

Chapter 5 ADTs Stack and Queue

Stacks of Coins and Bills

Stacks of Boxes and Books TOP OF THE STACK

Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack. A stack is a LIFO “last in, first out” structure.

Stack ADT Operations MakeEmpty -- Sets stack to an empty state. IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Throws exception if stack is full; otherwise adds newItem to the top of the stack. Pop -- Throws exception if stack is empty; otherwise removes the item at the top of the stack. ItemType Top -- Throws exception if stack is empty; otherwise returns a copy of the top item

ADT Stack Operations Transformers Push Pop Observers IsEmpty IsFull change state observe state

class StackType { public: StackType( ); bool IsFull () const; bool IsEmpty() const; void Push( ItemType item ); void Pop(); private: ItemType Top(); private: int top; ItemType items[MAX_ITEMS]; }; What are the pre and post conditions?

// File: StackType.cpp #include "StackType.h" #include StackType::StackType( ) { top = -1; } bool StackType::IsEmpty() const { return(top == -1); } bool StackType::IsFull() const { return (top = = MAX_ITEMS-1); }

void StackType::Push(ItemType newItem) { if( IsFull() ) throw FullStack(): top++; items[top] = newItem; } void StackType::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; } ItemType StackType::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; }

Class Interface Diagram (Memory reversed to better illustrate concept) StackType class StackType Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ] Top

charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)} Tracing Client Code Private data: top [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] letter ‘V’‘V’

Tracing Client Code letter ‘V’‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘S’ [ 1 ] ‘C’ items [ 0 ] ‘V’0 charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘K’‘K’ Private data: top 2 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘K’‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘K’‘K’ Private data: top 1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘C’‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘C’‘C’ Private data: top 0 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code letter ‘V’‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

End of Trace letter ‘V’‘V’ Private data: top -1 [ MAX_ITEMS-1 ]. [ 2 ] ‘K’ [ 1 ] ‘C’ items [ 0 ] ‘V’ charletter = ‘V’; StackType charStack; charStack.Push(letter); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(‘K’); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Another Stack Implementation One advantage of an ADT is that the implementation can be changed without the program using it knowing about it. The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.

ItemType is char class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’

class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ItemType is float

ItemType is string class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType cat dog

Tracing Client Code letter ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter ‘V’‘V’ Private data: topPtr NULL charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘V’‘V’ ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘S’ ‘ C’ ‘ V’ ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘S’ ‘ C’ ‘V’ ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘S’‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘S’‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘V’‘V’ ‘C’‘C’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘V’‘V’ ‘C’‘C’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

Tracing Client Code letter Private data: topPtr ‘K’‘K’ ‘V’‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

// DYNAMICALLY LINKED IMPLEMENTATION OF STACK Struct NodeType; //Forward declaration class StackType { public: //Identical to previous implementation private: NodeType* topPtr; };. Struct NodeType { ItemType info; NodeType* next; };

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem location

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem location

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem location ‘B’‘B’

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem location ‘B’‘B’

Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’‘B’ newItem location ‘B’‘B’

void StackType::Push ( ItemType newItem ) // Adds newItem to the top of the stack. { if (IsFull()) throw FullStack(); NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; } Implementing Push Do we need IsFull?

Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item

Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’‘B’

Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’‘B’

Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’‘B’

Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘X’ ‘C’ ‘L’ tempPtr ‘B’‘B’

void StackType::Pop()// Remove top item from Stack. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr ->next; delete tempPtr; } ItemType StackType::Top() // Returns a copy of the top item in the stack. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; } Implementing Pop / Top

Implementing IsFull bool StackType::IsFull() const // Returns true if there is no room for another // ItemType on the free store; false otherwise { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; }

Example of a Queue

What is a Queue? Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). A queue is a FIFO “first in, first out” structure.

Queue ADT Operations MakeEmpty -- Sets queue to an empty state. IsEmpty -- Determines whether the queue is currently empty. IsFull -- Determines whether the queue is currently full. Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item.

ADT Queue Operations Transformers MakeEmpty Enqueue Dequeue Observers IsEmpty IsFull change state observe state

typedef char ItemType; class QueType { public: QueType(int max); // max is the size of the queue. QueType(); // Default size of 500. ~QueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item); Public Interface of QueType

SIMPLE ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘ C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED

What is the private part of class QueType for this design? What troubles do we encounter?

DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED

What is the private part of class QueType for this design? Does this design have the same problems as the previous design?

class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘C’‘Z’ ‘T’

What is the private part of class QueType for this design?

CountedQueType inherits from QueType // DERIVE CLASS CountedQueType FROM BASE CLASS QueType class CountedQueType : QueType; { public: CountedQueType( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int GetLength( ) const; // Returns number of items on the counted queue. private: int length; };

What additional fields does CountedQueType need? What functions must be changed?

Comparing Queue Implementations 80-byte string Array: (80 bytes * 101 slots) + (2 bytes * 2 indexes) = 8084 bytes Linked queue: 8 bytes for 2 queue pointers, plus (80 bytes + 4 bytes) = 84 bytes / node Array requires same amount of memory, regardless of slots used. When elements > 96, queue requires more memory. 2-byte integer Array: (2 bytes * 101 slots) + (2 bytes * 2 indexes) = 206 bytes Lined queue: 8 bytes for 2 queue pointers, plus 2 bytes + 4 bytes = 6 bytes / node When elements > 33, queue requires more memory than array

Big-O Comparison of Queue Operations Dynamic ArrayLinked List Class Constructor O(1) MakeEmpty O(1)O(N) IsFull O(1) IsEmpty O(1) Enqueue O(1) Dequeue O(1) Destructor O(1)O(N)