1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structure Lecture-5
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
More on the STL vector list stack queue priority_queue.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
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.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
1 Writing a Good Program 8. Elementary Data Structure.
11 Introduction to Object Oriented Programming (Continued) Cats.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Programming Circular Linked List.
CS505 Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
LinkedList Class.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Object Oriented Programming COP3330 / CGS5409
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 4 Linked Lists.
Linked Lists.
Data Structures & Programming
Presentation transcript:

1 Linked Multiple Queues

2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues. Example: Process queues in an operating system.

3 Linked Multiple Queues We can take advantage of the fact that the number of items is fixed (or bounded). Provide an array with one element for each item to be queued plus one element for each queue (for a queue head). Each array element has a next and prev index. Always a member of exactly one queue. One queue is usually designated as the "Free Queue" Holds elements that are not in a real queue. Elements available to be assigned for use.

4 Linked Multiple Queues If we are queueing integers, 0 – N, we don’t need a data member. The position of the element defines the integer represented by that element. Example: Process IDs If we want to use this structure for other kinds of objects we can provide an array of objects or pointers to objects and map the integer to an object.

5 Getting Started Create a new empty C++ console application project. Add main.cpp Start with Hello, World Add class Linked_Multiqueue

6 Initial Program Add class Linked_Multiqueue _02_21_Linked_Multiqueues/

7 Class Linked_Multiqueue

8 Linked_Multiqueue.h #pragma once #include class Linked_Multiqueue { private: struct Node { int next; int prev; };

9 Linked_Multiqueue.h public: Linked_Multiqueue(int nr_queues, int nr_elements); ~Linked_Multiqueue(void); // Determine if the specified queue is empty. bool empty(int queue_id) const; // Return the integer at the head of the specified queue. int front(int queue_id) const; /* Move the element at the head of the "from" queue to * the tail of the "to" queue. */ void requeue (int from_queue_id, int to_queue_id); // Display all elements in the specified queue. void display(int queue_id, std::ostream & out) const;

10 Linked_Multiqueue.h private: // This method is to be used only by the constructor. void add_to_free_queue(int node_index);

11 Linked_Multiqueue.h private: /* Elements 0 through (nr_queues-1) are list headers for queues. * Elements nr_queues through (nr_queues+nr_elements-1) are queue * elements for integers 0 through N-1, where N is nr_elements. * The element for integer i is at (nr_queues+i). * * queues[queue_id].next is the first element of the queue. * (Oldest item and next item to be removed.) * queues[queue_id].prev is the last element of the queue. * (Most recently added item.) */ int number_of_queues; int number_of_elements; Node* queues; // Dynamically allocate array of Node objects };

12 Linked_Multiqueue.cpp #include #include "Linked_Multiqueue.h" // Constructor Linked_Multiqueue::Linked_Multiqueue(int nr_queues, int nr_elements) : number_of_queues(nr_queues), number_of_elements(nr_elements) { // Allocate array of Nodes queues = new Node[nr_queues + nr_elements]; assert (queues != 0); // Initialize all queues as empty. for (int i = 0; i < nr_queues; ++i) { queues[i].next = i; queues[i].prev = i; }

13 Constructor continued // Put all elements into the Free Queue (Queue 0). int first_node_index = nr_queues; int last_node_index = nr_queues + nr_elements - 1; for (int node_index = first_node_index; node_index <= last_node_index; ++node_index) { // Add this element to the end of queue 0; add_to_free_queue(node_index); }

14 add_to_free_queue() // Add the node at position node_index to the end of // the Free Queue. // This function is for use only by the constructor. void Linked_Multiqueue::add_to_free_queue(int node_index) { queues[node_index].prev = queues[free_queue].prev; queues[node_index].next = free_queue; int free_queue_tail_index = queues[free_queue].prev; queues[free_queue_tail_index].next = node_index; queues[free_queue].prev = node_index; }

15 empty() // Determine if the specified queue is empty. bool Linked_Multiqueue::empty(int queue_id) const { assert (queue_id >= 0); assert (queue_id < number_of_queues); return queues[queue_id].next == queue_id; }

16 display() // Display all elements in the specified queue. void Linked_Multiqueue::display(int queue_id, ostream & out) const { assert (queue_id >= 0); assert (queue_id < number_of_queues); if (empty(queue_id)) { out << "Queue " << queue_id << " is empty." << endl; } else { int i = queues[queue_id].next; // First element in this queue while (i >= number_of_queues) { out << i - number_of_queues << " "; i = queues[i].next; } out << endl; } out << endl }

17 Initial Test #include #include "Linked_Multiqueue.h" using namespace std; int main() { cout << "This is a test program for the linked multiqueue\n"; int nr_queues = 3; int nr_elements = 5; cout << "\nAllocating data structure.\n"; Linked_Multiqueue* lm1 = new Linked_Multiqueue(nr_queues, nr_elements); assert (lm1 != 0); cout << "Allocation successful\n"; for (int queue = 0; queue < nr_queues; ++queue) { cout << "Queue " << queue << endl; lm1->display(queue, cout); }

18 Initial Test Running

19 Destructor Linked_Multiqueue::~Linked_Multiqueue(void) { delete [] queues; }

20 front // Return the integer at the front of the specified queue. int Linked_Multiqueue::front(int queue_id) const { assert (queue_id >= 0); assert (queue_id < number_of_queues); return queues[queue_id].next - number_of_queues; }

21 requeue() /* Move the element at the head of the "from" queue to * the tail of the "to" queue. */ void Linked_Multiqueue::requeue (int from_queue_id, int to_queue_id) { assert (from_queue_id >= 0); assert (from_queue_id < number_of_queues); assert (to_queue_id >= 0); assert (to_queue_id < number_of_queues); assert (!empty(from_queue_id)); // First element in the "From" queue. int target = queues[from_queue_id].next;

22 requeue() continued // Remove target from its current queue int new_first = queues[target].next; queues[from_queue_id].next = new_first; queues[new_first].prev = from_queue_id; // Last element in the "To" queue int destination = queues[to_queue_id].prev; // Insert target at end of "To" queue. queues[destination].next = target; queues[target].prev = destination; queues[target].next = to_queue_id; queues[to_queue_id].prev = target; }

23 Test Requeue // Move all elements from their current queue // to the next higher numbered queue. for (int i = 0; i < nr_queues - 1; ++i) { cout << "\n\nMoving all elements from queue " << i << " to queue " << i+1 << endl; while (!lm1->empty(i)) { cout << "Moving head of queue " << i << " to queue " << i+1 << endl; lm1->requeue(i, i+1); cout << "Queue " << i << ":" << endl; lm1->display(i, cout); cout << "Queue " << i+1 << ":" << endl; lm1->display(i+1, cout); } cout << endl; }

24 Testing Requeue

25 More Testing // Move all elements of last queue back to first queue. cout << "\n\nMoving all elements from queue " << nr_queues-1 << " to queue " << 0 << endl; int i = nr_queues - 1; while (!lm1->empty(i)) { cout << "Moving head of queue " << i << " to queue " << 0 << endl; lm1->requeue(i, 0); cout << "Queue " << i << ":" << endl; lm1->display(i, cout); cout << "Queue " << 0 << ":" << endl; lm1->display(0, cout); } cout << endl;

26 Test Running

27 Test Delete cout << "\nDeleting the queues\n"; delete lm1; cout << "Test complete\n";

28 Testing Delete

29 What about copies? This class has no copy constructor or assignment operator. Will cause a crash if a user initializes a new object from and existing one or does an assignment. These are probably not operations that a user should do. How can we prevent it?