Data Structures for Media Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Data Structures for Media Queues

Description:

Data Structures for Media Queues – PowerPoint PPT presentation

Number of Views:140
Avg rating:3.0/5.0
Slides: 25
Provided by: zha119
Category:

less

Transcript and Presenter's Notes

Title: Data Structures for Media Queues


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
Implementation of QueueSequential Allocation
(Using Array)
define TOTAL_SLOTS 100 __gc class
MyQueue private int front int
rear int itemsTOTAL_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
Implementation of QueueSequential Allocation
(Using Array)
define TOTAL_SLOTS 100 __gc class MyQueue
private int front int
rear int itemsTOTAL_SLOTS
Slot0 Item A
Slot1 Item B
Slot2 Item C
Slot3Empty
Empty
Slot99 Empty
front
rear
Suppose we remove 2 items
Then we remove the remaining one item
If the queue is empty, well have___________.
7
define TOTAL_SLOTS 100 __gc class MyQueue
private int front int
rear int itemsTOTAL_SLOTS
Slot0 Item A
Slot1 Item B
Slot2 Item C
Slot3Empty
Empty
Slot99 Empty
front
rear
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 cant 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 itemsTOTAL_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 MyQueueisEmpty() return
(frontrear) bool MyQueueisFull() return(
(rear1)TOTAL_SLOTSfront)
void MyQueueenqueue(int data) if(!isFull())
itemsreardata rear(rear1)
int MyQueuedequeue( ) int
ret_val if(!isEmpty()) ret_valitemsfront
front(front1)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 frontrearNULL.
  • When will the queue be full?

11
Linked Implementation of Queue
// MyQueue.h pragma once include
stdlib.h using ltmscorlib.dllgt 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 MyQueueMyQu
eue() size0 frontNULL rearNULL bool
MyStackIsEmpty() return (frontNULL)
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 2 The queue is not empty
Case 1 The queue is empty
frontNULL rearNULL
Item X
Item A
New
NULL
New

front
rear
NULL
New
NULL
Item X
Item A
New

rear
front
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
MyQueueEnqueue(int data) ListNode pnew
ListNode(data) if (IsEmpty()) frontp else
rear-gtnextp rearp
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
frontNULL rearNULL
NULL
Item A
NULL
Item A
rear
front
rear
front
Case 3 The queue has more than one item
Value of Item A
Item X
Item B
NULL
Item A
Item X
Item B
Item A
NULL


front
rear
front
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
MyQueueDequeue() int ret_value if
(!IsEmpty()) ret_valuefront-gtgetData() fr
ontfront-gtnext if(frontNULL) rearNULL
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
Round Robin Schedule
  • job 1 4 time slots job 2 3 time slots
  • job 3 1 time slot job 4 2 time slots

18
Application 2 Reversing a Stack
Reversing a stack Stack s Queue p
while(!s-gtIsEmpty()) x s-gtpop() p-gtEnqueu
e(x) while (!p-gtIsEmpty()) x
p-gtDequeue() s-gtpush(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 AIs work to arrange that for you

Least important Most important Moderately
important
20
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 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
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 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.
24
Priority Queue
  • Priority Queue - List Implementation
  • 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
  • ltComparison is similar to array
    implementations.gt

We will come back to this after we learned trees
Write a Comment
User Comments (0)
About PowerShow.com