Queue Data Structure Lecture Notes PDF

Summary

This document provides lecture notes on queues, a fundamental data structure in computer science. It discusses various queue types, operations, and implementation methods. The notes are well-structured, highlighting key concepts and illustrating examples using diagrams. Topics include array and linked list implementations.

Full Transcript

Queue ARIFATUN NESA Lecturer Department of CSE Uttara University Simulating a Waiting Line A line, or queue, of people Queue It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called the rear and remo...

Queue ARIFATUN NESA Lecturer Department of CSE Uttara University Simulating a Waiting Line A line, or queue, of people Queue It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called the rear and removed from another end, called the front of the list. A queue of strings after (a) enqueue adds Jim; (b) enqueue adds Jess; (c) enqueue adds Jill; (d) enqueue adds Jane; A queue of strings after (e) enqueue adds Joe; (f) dequeue retrieves and removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves and removes Jess; Queue A queue is a first in, first out (FIFO) data structure This is accomplished by inserting at one end (the rear) and deleting from the other (the front) 0 1 2 3 4 5 6 7 myQueue: 17 23 97 44 front = 0 rear = 3 To insert: put new element in location 4, and set rear to 4 To delete: take element from location 0, and set front to 1 Queue Two basic operations are associated with queue: “Insert” operation is used to insert an element into a queue. – Called enqueue operation “Delete” operation is used to delete an element from a queue. – Called dequeue operation Queue Example: 7 Queue: AAA, BBB, CCC, DDD, EEE 6 1 2 3 4 5 6 7 EEE 5 AAA BBB CCC DDD EEE DDD 4 Front CCC 3 Rear BBB 2 AAA 1 Front Queue When queue is empty – rear=-1 – front=-1 Adding an element in queue will increase the value of rear – rear=rear+1; Removing an element in queue will increase the value of front – front=front+1; Array implementation of queue Queue empty rear=-1 front=-1 Array implementation of queue Queue empty rear=-1 front=-1 Add 10, rear=0 front=0 10 Array implementation of queue Queue empty rear=-1 front=-1 Add 10, rear=0 front=0 10 Add 20, rear=1, front=0 10 20 Array implementation of queue Queue empty rear=-1 front=-1 Add 10, rear=0 front=0 10 Add 20, rear=1, front=0 10 20 Add 30, rear=2, front=0 10 20 30 Array implementation of queue Queue empty rear=-1 front=-1 Add 10, rear=0 front=0 10 Add 20, rear=1, front=0 10 20 Add 30, rear=2, front=0 10 20 30 Add 40, rear=3, front=0 10 20 30 40 Array implementation of queue Add 40, rear=3, front=0 10 20 30 40 Remove, rear=3, front=1 20 30 40 Array implementation of queue Add 40, rear=3, front=0 10 20 30 40 Remove, rear=3, front=1 20 30 40 Remove, rear=3, front=2 30 40 Array implementation of queue Add 40, rear=3, front=0 10 20 30 40 Remove, rear=3, front=1 20 30 40 Remove, rear=3, front=2 30 40 Add 60, rear=4, front=2 30 40 60 Queue front = 0 rear = 3 Initial queue: 17 23 97 44 After insertion: 17 23 97 44 333 After deletion: 23 97 44 333 front = 1 rear = 4 Notice how the array contents “crawl” to the right as elements are inserted and deleted This will be a problem after a while! Queue Array Representation Linked-List Representation Insert Operations in Linear Queue Rear is the location in which the data element is to be inserted. Front is the location from which the data element is to be removed. Here N is the maximum size of the Queue 1. If Rear = N then Print: Overflow and Return. 2. Set Rear := Rear +1 3. Set Queue[Rear] := Item 4. Return. Delete Operations in Linear Queue Rear is the location in which the data element is to be inserted. Front is the location from which the data element is to be removed. Here N is the maximum size of the Queue 1. If Front = N+1 then Print: Underflow and Return. 2. Set Item := Queue[Front] 3. Set Front := Front + 1 4. Return. Types of Queue Simple Queue: A simple queue is the most basic queue. In this queue, the enqueue operation takes place at the rear, while the dequeue operation takes place at the front. Its applications are process scheduling, disk scheduling, memory management, IO buffer, pipes, call center phone systems, and interrupt handling. Types of Queue Circular Queue: A circular queue permits better memory utilization than a simple queue when the queue has a fixed size. In this queue, the last node points to the first node and creates a circular connection. Thus, it allows us to insert an item at the first node of the queue when the last node is full and the first node is free. It’s also called a ring buffer. It’s used to switch on and off the lights of the traffic signal systems. Apart from that, it can be also used in place of a simple queue in all the applications mentioned above. Types of Queue Priority Queue: A priority queue is a special kind of queue in which each item has a predefined priority of service. In this queue, the enqueue operation takes place at the rear in the order of arrival of the items, while the dequeue operation takes place at the front based on the priority of the items. That is to say that an item with a high priority will be dequeued before an item with a low priority. In the case, when two or more items have the same priority, then they’ll be dequeued in the order of their arrival. Hence, it may or may not strictly follow the FIFO rule. It’s used in interrupt handling, Prim’s algorithm etc. Types of Queue Double-Ended Queue (Deque): A deque is also a special type of queue. In this queue, the enqueue and dequeue operations take place at both front and rear. That means, we can insert an item at both the ends and can remove an item from both the ends. Thus, it may or may not adhere to the FIFO order It’s used to save browsing history, perform undo operations, implement A-Steal job scheduling algorithm, or implement a stack or implement a simple queue. Further, it has two special cases: input-restricted deque and output-restricted deque. Problem Once the queue is full, rear has already reached the Queue’s rear most position. Even though few elements from the front are deleted and some occupied space is relieved, it is not possible to add anymore new elements 1 2 3 4 5 6 7 77 88 11 22 33 front = 3 rear = 7 Circular queue We can treat the array holding the queue elements as circular (joined at the ends) 1 2 3 4 5 6 7 55 11 22 33 rear = 1 front = 5 Once the Queue is full the "First" element ofthe Queue becomes the "Rear" most element, if and only if the "Front“ has Front moved forward Rear Drawback of Linear Queue Once the queue is full, even though few elements from the front are deleted and some occupied space is relieved, it is not possible to add anymore new elements, as the rear has already reached the Queue’s rear most position. Circular Queue This queue is not linear but circular. Its structure can be like the following figure: In circular queue, once the Queue is full the "First" element of the Queue becomes the "Rear" most element, if and only if the "Front"has moved forward. otherwise it will again be a "Queue overflow" state. Figure: Circular Queue having Rear = 5 and Front = 0 25 For Insert Operation at Circular Queue Rear is the inserting location Front is the removing location. Here N is the maximum size of the Cqueue Initially Rear = 0 and Front = 0. begin if front = 0 and rear = n – 1, or front = rear + 1, then queue is full, and return otherwise if front = -1, then front = 0 and rear = 0 else if rear = n – 1, then, rear = 0, else rear := rear + 1 queue[rear] = key end For Delete Operation Circular Queue Rear is the inserting location Front is the removing location. Here N is the maximum size of the Cqueue Front element is assigned to Item, initially, Front = 1. begin if front = -1 then queue is empty, and return otherwise item := queue[front] if front = rear, then front and rear will be -1 else if front = n – 1, then front := 0 else front := front + 1 end Circular 1. Initially, Rear = 0, Front = 0. queue 4. Insert 20, Rear = 3, Front = 1. Front Rear 2. Insert 10, Rear = 1, Front = 1. 5. Insert 70, Rear = 4, Front = 1. Rear Front Front Rear 3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2. Front Rear Front Rear Circular 7. Insert 100, Rear = 5, Front = 2. queue 10. Delete front, Rear = 1, Front = 3. Front Rear Front Rear 8. Insert 40, Rear = 1, Front = 2. 11. Delete front, Rear = 1, Front = 4. Rear Rear Front Front 9. Insert 140, Rear = 1, Front = 2. 12. Delete front, Rear = 1, Front = 5. As Front = Rear + 1, so Queue overflow. Rear Rear Front Front Exercises Define Queue with graphical representation and real life example. Shortly Note on FIFO properties. Summarize the Basic operations of Queue. Follow the instruction (e.g. add 10, add 30, remove 10, add 80….)and build a linear and circular queue for given size and mark the front and rear index number after completion of each instruction.(Practise from slide) Types of Queue with definition and graphical representation. Explain the disadvantage of linear queue over circular queue. Algorithm: a. Enqueue, b. Dequeue Comparison between Stack and Queue.

Use Quizgecko on...
Browser
Browser