1 CS 212 DATA STRUCTURES Computer Science Department





























- Slides: 29
1 CS 212: DATA STRUCTURES Computer Science Department Lecture 5: Queues
Lecture Contents 2 § § § Basic Queue Operation Enqueue Dequeue Queue Front Queue Rear Queue Linked List Design Data strucure Queue Algorithms Priority Queues 01 -Feb-22 Computer Science Department
3 Queues 01 -Feb-22 Computer Science Department
Queues 4 What is Queue? 01 -Feb-22 A queue is a linear list in which data can only be inserted at one end , called rear , and deleted from the other end , called the front A queue is simply a waiting line that grows or shrinks by adding or taking elements form it. Unlike stack, it’s a structure in which both ends are used. Computer Science Department
Queues 5 What is Queue? 01 -Feb-22 Aqueue is a first in first out (FIFO) structure � no search, � no adding in arbitrary positions, � no sorting, � no access to anything beyond the front and rear elements. Computer Science Department
Queues 6 Queue Operatio n 01 -Feb-22 There are four basic queue operations � Enqueue � Dequeue � Queue Front � Queue Rear Computer Science Department
Queues 7 Queue Operation 01 -Feb-22 Computer Science Department
Basic Queue Operations 8 Enqueue: inserts an element at the rear of the queue. grape Data apple front kiwi Enqueue rear queue apple kiwi rear front operation grape queue
Basic Queue Operations 9 Dequeue: deletes element at the front of the queue. apple Data apple kiwi grape Dequeue rear front queue kiwi front operation grape rear queue
Basic Queue Operations 10 Queue Front: � Examines the element at the front of the queue. apple Data apple kiwi grape rear front queue Queue Front operation apple front kiwi grape rear
Basic Queue Operations 11 Queue Rear: � Examines the element at the rear of the queue. grape Data apple kiwi grape rear front queue Queue Rear operation apple front kiwi grape rear
Queue Linked List Design 12 For a linked list implementation of a queue, we use two types of structures: a head and a node. apple kiwi grape fig rear front Conceptual queue 4 front apple kiwi count rear grape Physical queue fig
Queue Linked List Design 13 front count rear Queue head structure queue. Head front count <node pointer> <integer> rear <node pointer> end queue. Head node data next Queue node structure data next end node <datatype> <node pointer>
Queue Algorithms 14 Create Queue algorithm create. Queue 1 queue. front = null queue. rear = null 3 queue. count = 0 end create. Queue 2 0 front count rear Queue head structure
Enqueue Queue front count rear 0 data plum next new. Ptr Before Case 1: insert into null queue Queue front count rear 1 plum new. Ptr count front Queue rear next After count front Queue 1 data rear 2 new. Ptr plum data next kiwi next data next kiwi Before 15 data next Case 2: insert into queue After new. Ptr
En queue 16 algorithm enqueue ( ref queue <metadata>, val data. In <data. Type>) This algorithm inserts data into a queue. Pre queue is a metadata structure Post data. In has been inserted Return true if successful, false if overflow If (queue full) 1 1 end if allocate (newptr) newptr->data = data. In newptr->next = null pointer if (queue. count = zero) 2 3 4 5 6 1 1 9 10 queue. front = new. Ptr else 7 8 return false queue. rear->next = new. Ptr end if queue. rear queue. count = newptr = queue. count + 1
Dequeue Queue count front rear 1 count front Queue rear 0 plum data next data delete. Loc next After Before Case 1: delete only item in queue count front Queue 2 plum data next data count front Queue next delete. Loc rear 1 plum kiwi Before 17 rear data kiwi next After Case 2: delete item at front of queue data next
Dequeue 18 algorithm enqueue ( ref queue <metadata>, val data. In <data. Type>) This algorithm delete a node from a queue. Pre queue is a metadata structure Post Data at front of queue returned to user through item and front element deleted and recycled. Return true if successful , false if underflow 1 If (queue. count is 0) 1 return false 2 end if 3 item = queue. front->data 4 delete. Loc= queue. front 5 if (queue. count 1) Deleting only item in queue 1 queue. rear= null pointer 6 end if 7 queue. front= queue. front->next 8 queue. count =queue. count -1 9 recycle ( delete. Loc) 10 return End dequeue 01 -Feb-22 Computer Science Department
Queues 19 Retrievin g Queue Data algorithm Queue. Front (val queue <metadata. Type> ) This algorithm receives the data at the front of the queue without changing the queue contents. Pre queue is a meteadata structure Post data passed back to caller Return true if successful, false if underflow 1 2 3 4 01 -Feb-22 if (queue. count is 0) 1 return false end if dataout = queue. front->data return true end Queue. Front Computer Science Department
Queues 20 Retrievin g Queue Data algorithm empty. Queue (val queue <metadata> ) This algorithm checks to see if a queue is empty. Pre queue is ametadata structure Return true if empty, false if queue has data return (if queue. count equal 0) end empty. Queue 01 -Feb-22 Computer Science Department
Queues 21 Retrievin g Queue Data algorithm full. Queue (val queue <metadata> ) This algorithm checks to see if a queue is full. The queue is full if memory cannot be allocated for another node. Pre queue is ametadata structure Return true if full, false if there is room for another node allocate (temp. Ptr) 1 If (allocation successful) 1 recycle(temp. Ptr) 2 return false 2 else 1 return true 3 end if end full. Queue 01 -Feb-22 Computer Science Department
Queues 22 Retrievin g Queue Data algorithm Queuecount (val queue <metadata> ) This algorithm return the number of elements in the queue Pre queue is ametadata structure Return queue count return queue. count end Queuecount 01 -Feb-22 Computer Science Department
Queues 23 Retrievin g Queue Data algorithm destroy. Queue (ref queue <metadata> ) This algorithm deletes all data from a queue. Pre queue is a metadata strucrure Post recycled 1 2 3 4 5 6 7 01 -Feb-22 all data have been deleted and ptr = queue. front Loop (ptr not null) 1 delete. Ptr = ptr 2 ptr = ptr->next 3 recycle (delete. Ptr) end loop queue. front = null queue. rear = null queue. count = 0 return end destroy. Queue Computer Science Department
Priority Queues 24 a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.
Priority Queues 25 Priority queue returns elements in priority order, order determined by key. It stores collection of entries. Each entry is a (key, value) pair. Stacks and Queues: Removal order determined by order of inserting Priority Queue: Order determined by key ( Key may be part of element data or separate) 01 -Feb-22 Computer Science Department
Priority Queues 26 Applications: � � Hospital Emergency Rooms Stock market
Priority Queue 27 Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? You set your priority based on due days. Due days are called keys. Priority Due day Database Systems 2 October 3 UNIX 4 October 10 Data Structure & Algorithm 1 September 29 Structured Systems Analysis 3 October 7 Course
What’s so different? 28 Stacks and Queues: • Removal order determined by order of inserting (stack LIFO, Queue FIFO) Sequences: • User chooses exact placement when inserting and explicitly chooses removal order Priority Queue • Removal order determined by key • Key may be part of element data or separate Examples: � Processes scheduled by CPU Hospital Emergency Rooms College admissions process for students
29 End Of Chapter References: • Text book, chapter 5: Queues 01 -Feb-22 Computer Science Department