Queue Data Structure Implementation using Arrays | Lab 3 | Logic and Program | Tamil | MPR

 Introduction


In computer science, data structures play an essential role in organizing and storing data. One such data structure is a queue, which is used to store elements in a linear manner. In a queue, elements are stored in a first-in, first-out (FIFO) order. This means that the element that is added first to the queue is the first one to be removed. In this blog post, we will explore the queue data structure in detail, including its implementation, operations, and applications.
Implementation of a Queue
A queue can be implemented using an array or a linked list. In an array-based implementation, we use a fixed-size array to store the elements of the queue. We keep two pointers, one pointing to the front of the queue and the other pointing to the rear of the queue. When an element is added to the queue, it is added to the rear end of the array. When an element is removed from the queue, it is removed from the front end of the array. The array-based implementation has a disadvantage that it has a fixed size, and if the queue becomes full, it cannot store more elements.
On the other hand, in a linked-list-based implementation, we use a singly linked list or a doubly linked list to store the elements of the queue. We keep two pointers, one pointing to the front of the queue and the other pointing to the rear of the queue. When an element is added to the queue, it is added to the rear end of the linked list. When an element is removed from the queue, it is removed from the front end of the linked list. The linked-list-based implementation has an advantage that it can grow dynamically and can store more elements.
Operations on a Queue
A queue supports the following operations:
  1. Enqueue: It adds an element to the rear end of the queue.
  2. Dequeue: It removes an element from the front end of the queue.
  3. Front: It returns the element at the front end of the queue without removing it.
  4. Rear: It returns the element at the rear end of the queue without removing it.
  5. IsEmpty: It checks whether the queue is empty or not.
Applications of a Queue
A queue has many real-life applications. Some of the applications are as follows:
  1. Operating System: In an operating system, a queue is used to store the processes that are waiting to be executed by the CPU. The processes are added to the queue in a FIFO manner, and the process at the front of the queue is executed first.
  2. Web Servers: In a web server, a queue is used to store the requests that are waiting to be served by the server. The requests are added to the queue in a FIFO manner, and the request at the front of the queue is served first.
  3. Traffic Management: In traffic management, a queue is used to store the vehicles waiting at a traffic signal. The vehicles are added to the queue in a FIFO manner, and the vehicle at the front of the queue is allowed to move first.
Conclusion
In conclusion, a queue is a data structure used to store elements in a linear manner. It follows the FIFO principle, where the element that is added first is removed first. A queue can be implemented using an array or a linked list. It supports various operations such as enqueue, dequeue, front, rear, and IsEmpty. A queue has many real-life applications such as in operating systems, web servers, traffic management, and many more.

Source Code:
#include <stdio.h>

#define MAX 100

int queue[MAX], i;
static int front = -1,back=-1;

void add(int);
void rem();
void display();

int main() {
int choice, element;
while(1) {
printf("Enter the choice : \n");
printf(" 1. Add an element to the queue \n 2. Remove an element to the queue \n 3. Display all elements of the queue \n 4. Exit \n");
scanf("%d",&choice);
switch(choice) {
case 1:
printf("Enter the element to be added: \n");
scanf("%d",&element);
add(element);
break;
case 2:
rem();
break;
case 3:
display();
break;
case 4:
printf("Exiting Program....\n");
printf("Done! Good Bye!!!");
return 0;
default:
printf("Invalid Input....Try Again!!!\n");}
}
}

void add(int n) {
if(front==-1){
front = 0;
}
if(back==MAX-1) {
printf("The queue is full!");
return;
}
else{
back++;
queue[back] = n;
printf("%d has been added to the Queue!\n",n);
} }

void rem() {
if(front==-1) {
printf("Queue has no elements to remove!\n");
}
else{
int rmEle = queue[front];
front++;
printf("%d is removed from the Queue!\n",rmEle);
} }

void display() {
if(front==-1) {
printf("Queue has no elements to display!\n");
}
else{
printf("The Queue Elements are: ");
for (i=front;i<=back;i++) {
printf(" %d ",queue[i]);
}
printf("\n");

Comments

Popular posts from this blog

Andrax - Advanced pentesting Platform...