1 / 24

Data Structures for Media Queues

Data Structures for Media Queues. Outline. Queue Abstract Data Type Sequential Allocation Linked Allocation Applications Priority Queues. Queue. 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

cdubay
Download Presentation

Data Structures for Media Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures for MediaQueues

  2. Outline • Queue Abstract Data Type • Sequential Allocation • Linked Allocation • Applications • Priority Queues

  3. Queue • 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

  4. ADT of 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

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

  6. front rear Slot#0Empty Slot#0Empty Slot#1Empty Slot#1Empty Slot#2Empty Slot#2 Item C Slot#3Empty Slot#3Empty … Empty … Empty Slot#99 Empty Slot#99 Empty rear front Implementation of QueueSequential Allocation (Using Array) #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#3Empty … Empty Slot#99 Empty rear front Suppose we remove 2 items: Then we remove the remaining one item: If the queue is empty, we’ll have___________.

  7. rear front rear front Slot#0 Empty Slot#1 Empty Slot#2 Item C Slot#3Item D Slot#4Item E … Slot#98Item XX Slot#99Empty rear front Slot#0 Item ZZ Slot#0 Empty Slot#1 Empty Slot#1 Empty Slot#2 Item C Slot#2 Item C Slot#3Item D Slot#3Item D Slot#4Item E Slot#4Item E … … Slot#98Item XX Slot#98Item XX Slot#99Item YY Slot#99Item YY #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#3Empty … Empty Slot#99 Empty rear front Suppose 2 items are removed and 96 items added: Then, we add (append) item YY: Then, we add (append) one more item ZZ: 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.

  8. Implementation of QueueSequential 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

  9. Implementation of QueueSequential 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;

  10. Implementation of QueueUsing Linked List NULL front rear • 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?

  11. Linked Implementation of Queue • // MyQueue.h • #pragma once • #include “stdlib.h” • #using <mscorlib.dll> • 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); }

  12. Linked Implementation of Queue 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. Case 1: The queue is empty Case 2: The queue is not empty front=NULL rear=NULL Item X New Item A NULL New … front rear New NULL Item X Item A New NULL … front rear front rear

  13. Linked Implementation of Queue 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. // MyQueue.cpp #include “MyQueue.h” void MyQueue::Enqueue(int data) { ListNode *p=new ListNode(data); if (IsEmpty()) front=p; else rear->next=p; rear=p; }

  14. Linked Implementation of Queue 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. Case 1: The queue has 0 item  Output error Value of Item A Case 2: The queue has 1 item front=NULL rear=NULL Item A NULL Item A NULL front rear front rear Case 3: The queue has more than one item Value of Item A Item A Item X NULL Item A Item X Item B Item B NULL … … front front rear rear

  15. Linked Implementation of Queue 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. // 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; }

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

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

  18. stack D C Pop from stack and insert into a queue B A queue D C B A A B C Delete from queue and Push onto stack D 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); } empty

  19. Queue enough? • In Game, when factory produce units • Suppose a factory can produce the following three units: • Attacker • Defender • 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 Least important Most important Moderately important

  20. Ordered Priority Queue A Priority=1 B Priority=2 C Priority=3 D Priority=3 (highest priority) (lowest priority) The first element to be removed. Unordered Priority Queue B Priority=2 C Priority=3 A Priority=1 D Priority=3 Priority Queue Priority Queue • 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.

  21. Priority Queue Priority Queue - Array Implementation • To implement a priority queue using an array such that the elements are orderedbased 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

  22. Priority Queue Priority Queue - Array Implementation • To implement a priority queue using an array such that elements are unordered. 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) 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.

  23. Priority Queue Priority Queue - List Implementation • To implement a priority queue as an orderedlist. 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.

  24. Priority Queue Priority Queue - List Implementation • To implement a priority queue as an unorderedlist. 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 <Comparison is similar to array implementations.> We will come back to this after we learned trees

More Related