Data Structures for Media Queues. Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
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.
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.
CHAPTER 7 Queues.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
CS 1114: Implementing Search
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Introduction to Data Structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Linked List (Part I) Data structure.
CSC 143 Queues [Chapter 7].
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
CSCS-200 Data Structure and Algorithms
Data Structures & Programming
Presentation transcript:

Data Structures for Media Queues

Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked Allocation Applications Applications Priority Queues Priority Queues Outline

Queue is a list with the restriction that insertions are performed at one end and deletions are performed at the other end of the list Queue is a list with the restriction that insertions are performed at one end and deletions are performed at the other end of the list Also known as: First-in-first-out (FIFO) list Also known as: First-in-first-out (FIFO) list Queue

Value: A sequence of items that belong to some data type ITEM_TYPE Operations on q: 1. Boolean IsEmpty() Postcondition: If the queue is empty, return true, otherwise return false 2. Boolean IsFull() Postcondition: If the queue is full, return true, otherwise return false 3. ITEM_TYPE Dequeue() /*take out the front one and return its value*/ Precondition: q is not empty Postcondition: The front item in q is removed from the sequence and returned 4. Void Enqueue(ITEM_TYPE e) /*to append one item to the rear of the queue*/ Precondition: q is not ______ Postcondition: e is added to the sequence as the rear one ADT of Queue

Implementation of Queue Sequential Allocation (Using Array) #define TOTAL_SLOTS 100 __gc class MyQueue { private: int front; int rear; int items[TOTAL_SLOTS]; }; Slot#0Slot#1Slot#2…Slot#98Slot#99 Slot#0 Item A Slot#1 Item B Slot#2 Item C Slot#3 Empty … Empty Slot#99 Empty Suppose some items are appended into the queue: //the index of the front slot that contains the front item //the index of the first empty slot at the rear of queue front rear

#define TOTAL_SLOTS 100 __gc class MyQueue { private: int front; int rear; int items[TOTAL_SLOTS]; }; Slot#0 Item A Slot#1 Item B Slot#2 Item C Slot#3 Empty … Empty Slot#99 Empty front rear If the queue is empty, we’ll have___________. Suppose we remove 2 items: Slot#0 Empty Slot#1 Empty Slot#2 Item C Slot#3 Empty … Empty Slot#99 Empty front rear Then we remove the remaining one item: Slot#0 Empty Slot#1 Empty Slot#2 Empty Slot#3 Empty … Empty Slot#99 Empty rear front Implementation of Queue Sequential Allocation (Using Array)

Suppose 2 items are removed and 96 items added: front rear Slot#0 Empty Slot#1 Empty Slot#2 Item C Slot#3 Item D Slot#4 Item E …Slot#98 Item XX Slot#99 Empty Then, we add (append) item YY: front rear Slot#0 Empty Slot#1 Empty Slot#2 Item C Slot#3 Item D Slot#4 Item E …Slot#98 Item XX Slot#99 Item YY Then, we add (append) one more item ZZ: front rear Slot#0 Item ZZ Slot#1 Empty Slot#2 Item C Slot#3 Item D Slot#4 Item E …Slot#98 Item XX Slot#99 Item YY However, we can’t add further item. Reason: we should not let rear = front if the queue is not empty. (The queue is empty when rear = front) Hence, this implementation allows only “______________________” items. Slot#0 Item A Slot#1 Item B Slot#2 Item C Slot#3 Empty … Empty Slot#99 Empty front rear #define TOTAL_SLOTS 100 __gc class MyQueue { private: int front; int rear; int items[TOTAL_SLOTS]; };

Implementation of Queue Sequential Allocation (Using Array) #define TOTAL_SLOTS 100 __gc class MyQueue { private: int front; int rear; int items[TOTAL_SLOTS]; public: bool isEmpty(); bool isFull(); enqueue(int ); int dequeue(); }; //the index of the front slot that contains the front item //the index of the first empty slot at the rear of queue

Implementation of Queue Sequential Allocation (Using Array) bool MyQueue::isEmpty() { return (front==rear); } bool MyQueue::isFull() { return((rear+1)%TOTAL_SLOTS==front); } void MyQueue::enqueue(int data) { if(!isFull()) { items[rear]=data; rear=(rear+1) } int MyQueue::dequeue( ) { int ret_val; if(!isEmpty()) { ret_val=items[front]; front=(front+1)%TOTAL_SLOTS; return ret_val; } %TOTAL_SLOTS;

NULL Implementation of Queue Using Linked List frontrear Queue can also be implemented with linked list. A pointer front points to the first node of the queue. A pointer rear points to the last node of the queue. If the queue is empty, then front=rear=NULL. When will the queue be full?

Linked Implementation of Queue // MyQueue.h #pragma once #include “ stdlib.h ” #using #using using namespaces System; namespace LinkedListLibrary { public __gc class MyQueue {public: MyQueue( ); bool IsEmpty(); void Enqueue(int ); int Dequeue(); private: ListNode* front; ListNode* rear; int size; };} // MyQueue.cpp #include “ MyQueue.h ” MyQueue::MyQueue(){size=0;front=NULL;rear=NULL;} bool MyStack::IsEmpty() { return (front==NULL); }

To insert an item (Enqueue) We have 2 cases: The queue is empty or not. Step 1: Allocate a new slot, p, to store the item. Step 2: Connect p to the queue (2 cases). Step 3: Update the rear pointer to point to p. Linked Implementation of Queue NULL frontrear Item X Item A … New frontrear Item X Item A … New NULL Case 2: The queue is not emptyCase 1: The queue is empty NULL rear New front New front=NULL rear=NULL

To insert an item (Enqueue) We have 2 cases: The queue is empty or not. Step 1: Allocate a new slot, p, to store the item. Step 2: Connect p to the queue (2 cases). Step 3: Update the pRear pointer to point to p. Linked Implementation of Queue // MyQueue.cpp #include “ MyQueue.h ” void MyQueue::Enqueue(int data) { ListNode *p=new ListNode(data); if (IsEmpty()) front=p;elserear->next=p;rear=p;}

To delete an item (the front item) and return it We have 3 cases: The queue has 0 item, 1 item or more than one item. Linked Implementation of Queue Case 2: The queue has 1 item Case 1: The queue has 0 item  Output error NULL rear Item A front front=NULL rear=NULL Case 3: The queue has more than one item NULL rear Item A front rear Item X Item B … NULLItem A Value of Item A front rear Item X Item B … Item A NULL Value of Item A

To delete an item (the front item) and return it We have 3 cases: The queue has 0 item, 1 item or more than one item. Linked Implementation of Queue // MyQueue.cpp #include “ MyQueue.h ” int MyQueue::Dequeue() { int ret_value; if (!IsEmpty()) {ret_value=front->getData();front=front->next;if(front==NULL)rear=NULL;} return ret_value; }

In Game, when factory produce units In Game, when factory produce units See online movies See online movies The way printer works The way printer works Round Robin Schedule Round Robin Schedule Establish a queue for current jobs Establish a queue for current jobs Whenever a time slot is used up Whenever a time slot is used up Insert the current job into the queue Insert the current job into the queue Begin executing the job fetched from the queue Begin executing the job fetched from the queue Application 1 Phenomena on the computer

job 1 : 4 time slots; job 2 : 3 time slots job 1 : 4 time slots; job 2 : 3 time slots job 3 : 1 time slot; job 4 : 2 time slots job 3 : 1 time slot; job 4 : 2 time slots Round Robin Schedule 1(4 left)2(3 left)3(1 left)4(2 left)2(3 left)3(1 left)4(2 left)1(3 left)3(1 left)4(2 left)1(3 left)2(2 left)4(2 left)1(3 left)2(2 left)1(3 left)2(2 left)4(1 left)2(2 left)4(1 left)1(2 left)2(1left)4(1 left)1(2 left)2(1left)1(2 left)2(1left)1(1 left)

Application 2 Reversing a Stack Reversing a stack Stack *s; Queue *p; …while(!s->IsEmpty()) {x = s->pop(); p->Enqueue(x);} while (!p->IsEmpty()) {x = p->Dequeue(); s->push(x);} D C B A empty A B C D stack DCBA queue Pop from stack and insert into a queue Delete from queue and Push onto stack

In Game, when factory produce units In Game, when factory produce units Suppose a factory can produce the following three units: Suppose a factory can produce the following three units: Attacker Attacker Defender Defender Worker Worker When you are giving commands, you do not have so many time to worry about the order of production. It should be AI ’ s work to arrange that for you When you are giving commands, you do not have so many time to worry about the order of production. It should be AI ’ s work to arrange that for you Queue enough? Least important Most important Moderately important

Priority Queue The elements in a stack or a FIFO queue are ordered based on the sequence in which they have been inserted. The elements in a stack or a FIFO queue are ordered based on the sequence in which they have been inserted. In a priority queue, the sequence in which elements are removed is based on the priority of the elements. In a priority queue, the sequence in which elements are removed is based on the priority of the elements. A Priority=1 B Priority=2 C Priority=3 D Priority=3 Ordered Priority Queue (highest priority) (lowest priority) B Priority=2 C Priority=3 A Priority=1 D Priority=3 Unordered Priority Queue The first element to be removed. Priority Queue

Priority Queue - Array Implementation To implement a priority queue using an array such that the elements are ordered based on the priority. To implement a priority queue using an array such that the elements are ordered based on the priority. Time complexity of the operations : (assume the sorting order is from highest priority to lowest) Insertion:Find the location of insertion. O(__) Shift the elements after the location O(__) where n = number of elements in the queue Insert the element to the found location O(__) Altogether: O(__) Deletion:The highest priority element is at the front, ie. Remove the front element (Shift the remaining) takes O(__) time The efficiency of insertion is important Priority Queue

Priority Queue - Array Implementation To implement a priority queue using an array such that elements are unordered. To implement a priority queue using an array such that elements are unordered. Time complexity of the operations : Time complexity of the operations : Insertion:Insert the element at the rear position. O(1) Deletion:Find the highest priority element to be removed. O(n) Copy the value of the element to return it later. O(1) Shift the following elements so as to fill the hole. O(n) or replace the hole with the rear element O(1) or replace the hole with the rear element O(1) Altogether: O(n) The efficiency of deletion is important Consider that, on the average, Ordered Priority Queue: since it is sorted, every insertion needs to search half the array for the insertion position, and half elements are to be shifted. Unordered Priority Queue: every deletion needs to search all n elements to find the highest priority element to delete. Priority Queue

Priority Queue - List Implementation To implement a priority queue as an ordered list. To implement a priority queue as an ordered list. Time complexity of the operations : (assume the sorting order is from highest priority to lowest) Insertion: Find the location of insertion. O(n) No need to shift elements after the location. Link the element at the found location. O(1) Altogether: O(n) Deletion: The highest priority element is at the front. ie. Remove the front element takes O(1) time The efficiency of insertion is important. More efficient than array implementation. Priority Queue

Priority Queue - List Implementation To implement a priority queue as an unordered list. To implement a priority queue as an unordered list. Time complexity of the operations : Insertion: Simply insert the item at the rear. O(1) Deletion: Traverse the entire list to find the maximum priority element. O(n). Copy the value of the element to return it later. O(1) No need to shift any element. Delete the node. O(1) Altogether: O(n) The efficiency of deletion is important Ordered list vs Unordered list Priority Queue We will come back to this after we learned trees