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.

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

QUEUE Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth. Chanakya.
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?
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.
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,
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.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Queues.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
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
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Circular Queues: Implemented using Arrays
Data Structures – Week #4
Linked List Intro CSCE 121.
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

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 take place at other end (head) 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

Queue Implementation Ideas Container type class for holding data Array versus Linked List? Who’s better? Head index pointer Position right before first element in queue Tail index pointer Position of last element in queue

Array-Based Queue Definition template template class Queue {public: Queue(int MaxQueueSize = DefaultSize); ~Queue(); bool IsFull(); 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 template Queue ::Queue(int MaxQueueSize): MaxSize(MaxQueueSize) { queue = new KeyType[MaxSize]; head = tail = -1; }

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

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

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

Example: 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 FrontRear Q[0] Q[1] Q[2] Q[3] CommentsInitial0 J1 J1 Job 1 Enters 1 J1 J2 J1 J2 Job 2 Enters 2 J1 J2 J3 J1 J2 J3 Job 3 Enters 02 J2 J3 J2 J3 Job 1 Leaves 03 J2 J3 J4 J2 J3 J4 Job 4 Enters 13 J3 J4 J3 J4 Job 2 Leaves MaxSize = 4

Job Processing When J4 enters the queue, rear is updated to 3. When J4 enters the queue, rear is updated to 3. When rear is 3 in a 4-entry queue, run out of space. 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 The array may not really be full though, if head is not Head can be > -1 if items have been removed from queue. 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 FrontRear Q[0] Q[1] Q[2] Q[3] Comments3 J1 J2 J3 J4 J1 J2 J3 J4Initial 03 J2 J3 J4 J2 J3 J4 Job 1 Leaves 3 J2 J3 J4 J5 J2 J3 J4 J5 Job 5 Enters 03 J3 J4 J5 J3 J4 J5 Job 2 Enters 3 J3 J4 J5 J6 J3 J4 J5 J6 Job 6 Leaves

Worst Case Queue Shift Worst Case: Worst Case: Shift entire queue: Cost of O(n) Shift entire queue: Cost of O(n) Do every time perform an add Do every time perform an add Too expensive to be useful 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 Problems with implementing queues on top of arrays Sizing problems (bounds, clumsy resizing, …) Sizing problems (bounds, clumsy resizing, …) Non-circular Array – Data movement problem Non-circular Array – Data movement problem Now that have the concepts of list nodes, can take advantage of to represent queues. Now that have the concepts of list nodes, can take advantage of to represent queues. Need to determine appropriate way of: Need to determine appropriate way of: Representing front and rear Representing front and rear Facilitating node addition and deletion at the ends. 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()) { QueueEmpty(); 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. Implementation as written has to remove the item from the queue to read data value. Some implementations provide two separate functions: Some implementations provide two separate functions: Front() which returns the data in the first element Front() which returns the data in the first element Delete() which removes the first element from the queue, without returning a value. Delete() which removes the first element from the queue, without returning a value.