Understanding Queues in Data Structures

lecture eight queues n.w
1 / 17
Embed
Share

Learn about queues in data structures, their operations such as enqueue and dequeue, and their applications in various fields like operating systems, networks, and online businesses. Explore the FIFO (First In, First Out) structure of queues and how they help process data efficiently.

  • Queues
  • Data Structures
  • FIFO
  • Enqueue
  • Operating Systems

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture Eight Queues

  2. Queue A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front. These restrictions ensure that the data is processed through the queue in the order in which it is received. In other words, a queue is a first in, first out (FIFO) structure. Wednesday, June 18, 2025 Data Structure 2

  3. A Graphic Model of a Queue Head: Tail: All new items are added on this end All items are deleted from this end Wednesday, June 18, 2025 Data Structure 3

  4. Operations on queues Although we can define many operations for a queue, four are the basic: queue, enqueue, dequeue and empty. The queue operation creates an empty queue. The following shows the format. Wednesday, June 18, 2025 Data Structure 4

  5. The enqueue operation The enqueue operation inserts an item at the rear of the queue. The following shows the format. Wednesday, June 18, 2025 Data Structure 5

  6. The dequeue operation The dequeue operation deletes the item at the front of the queue. The following shows the format. Wednesday, June 18, 2025 Data Structure 6

  7. The empty operation The empty operation checks the status of the queue. The following shows the format. This operation returns true if the queue is empty and false if the queue is not empty. Wednesday, June 18, 2025 Data Structure 7

  8. Example The following shows a segment of an algorithm that applies the previously defined operations on a queue Q. Wednesday, June 18, 2025 Data Structure 8

  9. Queue applications Queues are one of the most common of all data processing structures. They are found in virtually every operating system and network and in countless other areas For example, queues are used in online business applications such as requests, jobs and orders. In a computer system, a queue is needed to process jobs and for system services such as print spools. processing customer Wednesday, June 18, 2025 Data Structure 9

  10. Queue applications Another common application of a queue is to adjust and create a balance between a fast producer of data and a slow consumer of data. For example, assume that a CPU is connected to a printer. The speed of a printer is not comparable with the speed of a CPU. If the CPU waits for the printer to print some data created by the CPU, the CPU would be idle for a long time. The solution is a queue. The CPU creates as many chunks of data as the queue can hold and sends them to the queue. The CPU is now free to do other jobs. The chunks are dequeued slowly and printed by the printer. The queue used for this purpose is normally referred to as a spool queue. Wednesday, June 18, 2025 Data Structure 10

  11. Queue implementation A queue ADT can be implemented using either an array or a linked list. In the array implementation we have a record with three fields. The first field can be used to store information about the queue. The linked list implementation is similar: we have an extra node that has the name of the queue. This node also has three fields: a count, a pointer that points to the front element and a pointer that points to the rear element. Wednesday, June 18, 2025 Data Structure 11

  12. Queue implementation Wednesday, June 18, 2025 Data Structure 12

  13. C++ program to implement Queue[using Array] int queue_array[MAX]; int rear = - 1; int front = - 1; main() { int choice; while (1) { cout<<"1.Insert element to queue \n ; cout<<"2.Delete element from queue \n ; cout<<"3.Display all elements of queue \n ; cout<<"4.Quit \n ; cout<<"Enter your choice : ; cin>>"%d", &choice; Wednesday, June 18, 2025 Data Structure #include <iostream.h> #define MAX 50 13

  14. C++ program to implement Queue[using Array] switch (choice) { case 1: insert(); case 2: delete(); case 3: display(); case 4: exit(1); default: cout<<"Wrong choice \n ; } /*End of switch*/ } /*End of while*/ } /*End of main()*/ break; break; break; Wednesday, June 18, 2025 Data Structure 14

  15. C++ program to implement Queue[using Array] int add_item; if (rear == MAX - 1) cout<<"Queue Overflow \n ; else { if (front == - 1) /*If queue is initially empty */ front = 0; cout<<"Inset the element in queue : ; cin>>"%d", &add_item; rear = rear + 1; queue_array[rear] = add_item; } } /*End of insert()*/ insert() { Wednesday, June 18, 2025 Data Structure 15

  16. C++ program to implement Queue[using Array] delete() { if (front == - 1 || front > rear) { cou<<"Queue Underflow \n"; return ; } else { cout<<"Element deleted from queue is : %d\n", queue_array[front]; front = front + 1; } } /*End of delete() */ Wednesday, June 18, 2025 Data Structure 16

  17. C++ program to implement Queue[using Array] display() { int i; if (front == - 1) cout<<"Queue is empty \n ; else { cout<<"Queue is : \n ; for (i = front; i <= rear; i++) cout<<"%d ", queue_array[i]; cout<<"\n"; } } /*End of display() */ Wednesday, June 18, 2025 Data Structure 17

More Related Content