Queues CS 302 – Data Structures Sections 5.3 and 5.4.

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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
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.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
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.
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.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Implementing an Unsorted List as a Linked Structure CS 308 – 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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks 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.
Implementing a Sorted List as a Linked Structure CS 308 – 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.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
C++ Classes and Data Structures Jeffrey S. Childs
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.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
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.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
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
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.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
Queues.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Queues CS 302 – Data Structures Sections 5.3 and 5.4

What is a queue? It is an ordered group of homogeneous items. Queues have two ends: –Items are added at one end. –Items are removed from the other end. FIFO property: First In, First Out –The item added first is also removed first

Queue Implementations Array-based Linked-list-based

Array-based Implementation template class QueueType { public: QueueType(int); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: int front, rear; ItemType* items; int maxQue; };

Implementation Issues Optimize memory usage. Conditions for a full or empty queue. Initialize front and rear. circular array linear array

Optimize memory usage

Full/Empty queue conditions

“Make front point to the element preceding the front element in the queue!” NOW!

Initialize front and rear

Array-based Implementation (cont.) template QueueType ::QueueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; } O(1)

Array-based Implementation (cont.) template QueueType ::~QueueType() { delete [] items; } O(1)

Array-based Implementation (cont.) template void QueueType ::MakeEmpty() { front = maxQue - 1; rear = maxQue - 1; } O(1)

Array-based Implementation (cont.) template bool QueueType ::IsEmpty() const { return (rear == front); } template bool QueueType ::IsFull() const { return ( (rear + 1) % maxQue == front); } O(1)

Enqueue (ItemType newItem) Function: Adds newItem to the rear of the queue. Preconditions: Queue has been initialized and is not full. Postconditions: newItem is at rear of queue.

Queue overflow The condition resulting from trying to add an element onto a full queue. if(!q.IsFull()) q.Enqueue(item);

Array-based Implementation (cont.) template void QueueType ::Enqueue (ItemType newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; } O(1)

Dequeue (ItemType& item) Function: Removes front item from queue and returns it in item. Preconditions: Queue has been initialized and is not empty. Postconditions: Front element has been removed from queue and item is a copy of removed element.

Queue underflow The condition resulting from trying to remove an element from an empty queue. if(!q.IsEmpty()) q.Dequeue(item);

Array-based Queue Implementation (cont.) template void QueueType ::Dequeue (ItemType& item) { front = (front + 1) % maxQue; item = items[front]; } O(1)

Linked-list-based Implementation Allocate memory for each new element dynamically Link the queue elements together Use two pointers, qFront and qRear, to mark the front and rear of the queue

A “circular” linked queue design (see textbook for details)

Linked-list-based Implementation // forward declaration of NodeType (i.e., like function prototype) template struct NodeType; template class QueueType { public: QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: NodeType * qFront; NodeType * qRear; };

Enqueue (ItemType newItem) Function: Adds newItem to the rear of the queue. Preconditions: Queue has been initialized and is not full. Postconditions: newItem is at rear of queue.

Enqueue (non-empty queue)

Special Case: empty queue Need to make qFront point to the new node New Node newNode qFront = NULL qRear = NULL

Enqueue template Enqueue void QueueType ::Enqueue(ItemType newItem) { NodeType * newNode; newNode = new NodeType ; newNode->info = newItem; newNode->next = NULL; if(qRear == NULL) // special case qFront = newNode; else qRear->next = newNode; qRear = newNode; } O(1)

Dequeue (ItemType& item) Function: Removes front item from queue and returns it in item. Preconditions: Queue has been initialized and is not empty. Postconditions: Front element has been removed from queue and item is a copy of removed element.

Dequeue (queue contains more than one elements)

Special Case: queue contains a single element We need to reset qRear to NULL also Node qFront qRear After dequeue: qFront = NULL qRear = NULL

Dequeue template Dequeue void QueueType ::Dequeue(ItemType& item) { NodeType * tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFront->next; if(qFront == NULL) // special case qRear = NULL; delete tempPtr; } O(1)

qRear, qFront - revisited Are the relative positions of qFront and qRear important? What about this?

qRear, qFront - revisited Are the relative positions of qFront and qRear important? Hard to dequeue!

Other Queue functions template QueueType() QueueType ::QueueType() { qFront = NULL; qRear = NULL; } template MakeEmpty() void QueueType ::MakeEmpty() { NodeType * tempPtr; while(qFront != NULL) { tempPtr = qFront; qFront = qFront->next; delete tempPtr; } qRear=NULL; } O(N) O(1)

Other Queue functions (cont.) template QueueType() QueueType ::~QueueType() { MakeEmpty(); } O(N)

Other Queue functions (cont.) template IsEmpty() bool QueueType ::IsEmpty() const { return(qFront == NULL); } template IsFull() bool QueueType ::IsFull() const { NodeType * ptr; ptr = new NodeType ; if(ptr == NULL) return true; else { delete ptr; return false; } O(1)

Comparing queue implementations Big-O Comparison of Queue Operations OperationArray Implementation Linked Implementation Class constructorO(1) MakeEmptyO(1)O(N) IsFullO(1) IsEmptyO(1) EnqueueO(1) DequeueO(1) DestructorO(1)O(N)

memory Compare queue implementations in terms of memory usage Example 1: Assume a queue of strings –Array-based implementation Assume a queue (size: 100) of strings (80 bytes each) Assume indices take 2 bytes Total memory: (80 bytes x 101 slots) + (2 bytes x 2 indices) = 8084 bytes –Linked-list-based implementation Assume pointers take 4 bytes Memory per node: 80 bytes + 4 bytes = 84 bytes

Comparing queue implementations (cont.)

Compare queue implementations in terms of memory usage Example 2: Assume a queue of short integers –Array-based implementation Assume a queue (size: 100) of short integers (2 bytes each) Assume indices take 2 bytes Total memory: (2 bytes x 101 slots) + (2 bytes x 2 indexes) = 206 bytes –Linked-list-based implementation Assume pointers take 4 bytes Memory per node: 2 bytes + 4 bytes = 6 bytes (cont.)

Comparing queue implementations (cont.)

Array- vs Linked-list-based Queue Implementations Array-based implementation is simple but: –The size of the queue must be determined when the queue object is declared. –Space is wasted if we use less elements. –Cannot "enqueue" more elements than the array can hold. Linked-list-based queue alleviates these problems but time requirements might increase.

Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. Able was I ere I saw Elba

Example: recognizing palindromes (1)Read the line of text into both a stack and a queue. (2) Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

Example: recognizing palindromes #include #include "stack.h" #include "queue.h“ int main() { StackType s; QueType q; char ch; char sItem, qItem; int mismatches = 0; cout << "Enter string: " << endl; while(cin.peek() != '\n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); }

while( (!q.IsEmpty()) && (!s.IsEmpty()) ) { s.Pop(sItem); q.Dequeue(qItem); if(sItem != qItem) ++mismatches; } if (mismatches == 0) cout << "That is a palindrome" << endl; else cout << That is not a palindrome" << endl; return 0; } Example: recognizing palindromes

Exercise 37: Implement a client function that returns the number of items in a queue. The queue is unchanged. int Length(QueueType& queue) Function: Determines the number of items in the queue. Precondition: queue has been initialized. Postconditions: queue is unchanged You may not assume any knowledge of how the queue is implemented.

int Length(QueType& queue) { QueType tempQ; ItemType item; int length = 0; while (!queue.IsEmpty()) { queue.Dequeue(item); tempQ.Enqueue(item); length++; } while (!tempQ.IsEmpty()) { tempQ.Dequeue(item); queue.Enqueue(item); } return length; } What are the time requirements using big-O? O(N)

int Length(QueType& queue) { QueType tempQ; ItemType item; int length = 0; while (!queue.IsEmpty()) { queue.Dequeue(item); tempQ.Enqueue(item); length++; } while (!tempQ.IsEmpty()) { tempQ.Dequeue(item); queue.Enqueue(item); } return length; } How would you implement it as member function? What would be the time requirements in this case using big-O? r f f r rear - frontmaxQue – (front – rear) O(1) Case 1: array-based

int Length(QueType& queue) { QueType tempQ; ItemType item; int length = 0; while (!queue.IsEmpty()) { queue.Dequeue(item); tempQ.Enqueue(item); length++; } while (!tempQ.IsEmpty()) { tempQ.Dequeue(item); queue.Enqueue(item); } return length; } How would you implement it as member function? What would be the time requirements in this case using big-O? O(N) Case 2: linked-list-based