Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Linear Lists – Linked List Representation
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.
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.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
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.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
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-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
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.
Queue Overview Queue ADT Basic operations of queue
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
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.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
CS Data Structures Chapter 3 Stacks and Queues.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
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.
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’
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Linked List (Part I) Data structure.
Linked List Intro CSCE 121 J. Michael Moore.
CSC 143 Queues [Chapter 7].
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.
Doubly Linked List Implementation
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Circular Queues: Implemented using Arrays
Linked List Intro CSCE 121.
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0, a 1, …, a n-1 ) a0 is the front element, a n-1 is the tail, and a i is behind a i-1 for all i, 1 <= i < n

Queue Definition Because of insertion and deletion properties, Queue is very similar to: Line at the grocery store Cars in traffic Network packets …. Also called first-in first out lists

Array-Based Queue Definition template class Queue { public: Queue(int MaxQueueSize = DefaultSize); ~Queue(); bool IsFull(); bool IsEmpty(); void Add(const KeyType& item); KeyType* Delete(KeyType& item); private: void QueueFull(); // error handling void QueueEmpty();// error handling int head, tail; KeyType* queue; int MaxSize; };

Queue Implementation Constructor: template Queue ::Queue(int MaxQueueSize): MaxSize(MaxQueueSize) { queue = new KeyType[MaxSize]; head = tail = -1; }

Queue Implementation Destructor: template Queue ::~Queue() { delete [] queue; head = tail = -1; }

Queue Implementation IsFull() and IsEmpty(): template bool Queue ::IsFull() { return (tail == (MaxSize-1)); } template bool Queue ::IsEmpty() { return (head == tail); }

Queue Implementation Add() and Delete(): template void Queue ::Add (const KeyType& item) { if (IsFull()) {QueueFull(); return;} else { tail = tail + 1; queue[tail] = item; } } template KeyType* Queue ::Delete(KeyType& item) { if (IsEmpty()) {QueueEmpty(); return 0}; else { head = head + 1; item = queue[head]; return &item; } }

Example: OS Job Scheduling OS has to manage how jobs (programs) are executed on the processor – 2 typical techniques: -Priority based: Some ordering over of jobs based on importance (Professor X’s jobs should be allowed to run first over Professor Y). -Queue based: Equal priority, schedule in first in first out order.

Queue Based Job Processing FrontRearQ[0] Q[1] Q[2] Q[3]Comments Initial 0 J1Job 1 Enters 1 J1 J2Job 2 Enters 2 J1 J2 J3Job 3 Enters 02 J2 J3Job 1 Leaves 03 J2 J3 J4Job 4 Enters 13 J3 J4Job 2 Leaves

Job Processing When J4 enters the queue, rear is updated to 3. When rear is 3 in a 4-entry queue, run out of space. The array may not really be full though, if head is not -1. Head can be > -1 if items have been removed from queue. Possible Solution: When rear = (maxSize – 1) attempt to shift data forwards into empty spaces and then do Add.

Queue Shift private void shiftQueue(KeyType* queue, int & head, int & tail) { int difference = head – (-1); // head + 1 for (int j = head + 1; j < maxSize; j++) { queue[j-difference] = queue[j]; } head = -1; tail = tail – difference; }

Queue Shift Worst Case For Queue Shift: Full Queue Alternating Delete and Add statements FrontRearQ[0] Q[1] Q[2] Q[3]Comments 3 J1 J2 J3 J4Initial 03 J2 J3 J4Job 1 Leaves 3 J2 J3 J4 J5Job 5 Enters 03 J3 J4 J5Job 2 Enters 3 J3 J4 J5 J6Job 6 Leaves

Worst Case Queue Shift Worst Case: –Shift entire queue: Cost of O(n-1) –Do every time perform an add –Too expensive to be useful Worst case is not that unlikely, so this suggests finding an alternative implementation.

‘Circular’ Array Implementation Basic Idea: Allow the queue to wrap-around Implement with addition mod size: tail = (tail + 1) % queueSize; N-1 N-2 J1 J2 J3 J N-1 N-2 J2 J3 J1

Linked Queues Problems with implementing queues on top of arrays –Sizing problems (bounds, clumsy resizing, …) –Non-circular Array – Data movement problem Now that have the concepts of list nodes, can take advantage of to represent queues. Need to determine appropriate way of: –Representing front and rear –Facilitating node addition and deletion at the ends.

Linked Queues CAT Front Rear MATHAT FrontRear Add(Hat) Add(Mat) Add(Cat)Delete()

Linked Queues Class QueueNode{ friend class Queue; public: QueueNode(int d, QueueNode * l); private: int data; QueueNode *link; };

Linked Queues class Queue { public: Queue(); ~Queue(); void Add(const int); int* Delete(int&); bool isEmpty(); private: QueueNode* front; QueueNode* rear; void QueueEmpty(); }

Linked Queues Queue::Queue() { front = 0; rear = 0; } bool Queue::isEmpty() { return (front == 0); } Front Rear 0 0

Linked Queues void Queue::Add(const int y) { // Create a new node that contains data y // Has to go at end // Set current rear link to new node pointer // Set new rear pointer to new node pointer rear = rear->link = new QueueNode(y, 0); } CAT Front MAT HAT Rear

Linked Queues int * Queue::Delete(int & retValue) { // handle empty case if (isEmpty()) {return 0;} QueueNode* toDelete = front; retValue = toDelete.data; front = toDelete->link; delete toDelete; return &retValue; } CAT Front MAT HAT ReartoDelete HAT returnValue Front

Queue Destructor Queue destructor needs to remove all nodes from head to tail. CAT Front MAT HAT RearFront Temp 0 0 if (front) { QueueNode* temp; while (front != rear) { temp = front; front = front -> link; delete temp; } delete front; front = rear = 0; }

Front vs Delete Implementation as written has to remove the item from the queue to read data value. Some implementations provide two separate functions: –Front() which returns the data in the first element –Delete() which removes the first element from the queue, without returning a value.

Radix Sort and Queues What does radix sort have to do with queues? Each list (the master list (all items) and bins (per digit)) needs to be first in, first out ordered – perfect for a queue.