Don’t hesitate to comment below if you have any questions or additional phrases
Queues Implementations
,Outline Queues
Basic operations
Examples of use Implementations Array-based and linked list-based
,Building a QueueClass In a queue,
new values are always addedat the front or head of the list
values are removedfrom the opposite end of the list, the rear or tail Examples of queues checkout at supermarket
vehicles at toll booth ticket line at movies Queue exhibits First-In-First-Out behavior ,D C B A Queue Queue: First In First Out (FIFO)
Toll Station Car comes, pays, leaves
Check-out in Big Y market Customer comes, checks out and leaves
Output Input
,Queues in a Computer System When a process (program) requires a certain resource
Printer
disk access on a network characters in a keyboard buffer. When you enter data into the computer, the characters are kept in queue in a buffer until the operating system can deal with them. ,More Examples of Queue In our daily life
Airport Security Check
Cinema Ticket Office Bank, ATM ,Examples Queue is a British word for line.
Expect O ( 1 ) time per queue operation because it is similar to a stack. The word "queueing" and its derivatives are the only English words with five consecutive vowels. ,Applications Queues are also useful for storing pending work:
Shortest paths problem (minimize the number of connecting flights between two arbitrary airports)
Operating Systems use queues to schedule tasks. Queues are often used in simulations e.g. Simulating traffic at an intersection by creating a growing line of automobiles waiting for the light to change.
,Abstract Data Types Queue
Operating on both ends
Operations: EnQueue(in),DeQueue(out) enqueue dequeue C B A front rear
,Printing Job Management Many users send their printing jobs to ECS public printer
Printer will put them into a queue according to the arrival time and print the jobs one by one These printing documents are A.doc, B.doc, C.doc and D.doc ,Printing Queue A.doc B.doc C.doc arrive to printer.
Now printing A.doc C B A C B A.doc is finished. Now printing B.doc D.doc comes D C B Now still printing B.doc D C B.doc is finished. Now printing C.doc D C.doc is finished. Now printing D.doc
,First-in First-out (FIFO) The first one enqueued is the first one dequeued. (FIFO) When we enqueue entries in the queue and then dequeue them one by one, we will get the items in the same order. A, B, C come out
,Implementing a Queue Class Implement as a LinkedList
insertions and deletions from either end are efficient, occur in constant O(1) time
good choice Implement as an ArrayList Not as simple
adding values at one end, removing at other end require multiple shifts Need to use a circular array ,Implementing a QueueClass Build a Queue from scratch
build a linked structure to store the queue elements
Attributes required A handle for the head node – Head handle for tail node – Tail integer to store number of values in the queue – count use LinearNode ,QueueStructure aQueue Head Size Tail n . . . value0 value1 . . . valuen-1
,Operations enqueue
add a new item at the rear
dequeue remove a item from the front
isEmpty check whether the queue is empty or not
size return the number of items in the queue
peek return the front item
,The QueueADT interface in UML ,Listing 7.1 Interface ,Abstract Data Type Same as Stack class, we use the T data type
Use an Arrayqueue, which stores all items that come in. T Queue[ ];
Other implementation with a Linked list we will address later. ,Queues: Simple Idea Store items in an array with front item at index zero and back item at index rear. We will add items at one end of a queue and remove them from the other end.
Enqueue is easy: increment rear by one and store the new item in data[rear]. To get the next item, we retrieve data[front]. Dequeue is inefficient: all elements have to be shifted up one place. Result: Dequeue will be O( N ). ,Two Solutions Shifting all items to front in the array when dequeue operation. ( To
Post time: Feb-20-2017