C Programming: Creating a Queue Using Linked List 🚀

beginner
11 min

C Programming: Creating a Queue Using Linked List 🚀

Welcome to this comprehensive tutorial on creating a Queue using Linked List in C programming! In this lesson, we'll be diving deep into understanding queues, their importance, and how to implement them using a linked list. By the end of this tutorial, you'll have a practical understanding of queues and be able to implement them in your own projects. 🎯

What is a Queue? 📝

A queue is a linear data structure that follows a First-In-First-Out (FIFO) principle. This means that the first element that enters the queue is the first one to leave. Imagine a line of people at a bank or a supermarket; the first person who joined the line will be served first. This is the essence of a queue.

Why Use a Queue? 💡

Queues are essential in various real-world scenarios such as managing tasks in operating systems, handling network traffic, and processing requests in web servers. They help maintain the order of elements and provide a systematic way of processing them.

Understanding Linked List 📝

Before diving into creating a queue using a linked list, let's briefly review what a linked list is. A linked list is a linear data structure where each element, called a node, consists of data and a reference (or link) to the next node.

Creating a Queue Using Linked List 🎯

Now that we have a basic understanding of queues and linked lists, let's implement a queue using a linked list in C.

Data Structures

In our implementation, we'll be using the following data structures:

  • struct Node: Represents a node in the linked list.
  • struct Queue: Represents the queue itself.
c
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Queue { struct Node* front; struct Node* rear; };

Creating and Initializing the Queue 💡

To create and initialize a queue, we'll allocate memory for both the front and rear pointers.

c
void createQueue(struct Queue* q) { q->front = NULL; q->rear = NULL; }

Adding Elements to the Queue 💡

To add elements to the queue, we'll create a new node, assign the data, and link it to the existing rear node (or create a new rear node if the queue is empty).

c
void enqueue(struct Queue* q, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory Error\n"); return; } newNode->data = data; newNode->next = NULL; if (q->rear == NULL) { q->front = newNode; q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } }

Removing Elements from the Queue 💡

To remove elements from the queue, we'll store the front node's data, then advance the front pointer. If the front pointer reaches NULL after advancing (meaning the queue is empty), we'll also update the rear pointer.

c
int dequeue(struct Queue* q) { if (q->front == NULL) { printf("Queue is empty\n"); return -1; } int data = q->front->data; struct Node* temp = q->front; q->front = q->front->next; if (q->front == NULL) { q->rear = NULL; } free(temp); return data; }

Checking the Queue's Status 💡

To check the status of the queue, we'll simply compare the front and rear pointers.

c
int isEmpty(struct Queue* q) { return q->front == NULL; }

Practical Example 🎯

Let's put our queue implementation into practice by creating a simple server that handles client requests in a FIFO manner.

c
#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> // ... (Previous code) int main() { struct Queue q; createQueue(&q); // Server setup (not shown) while (1) { int newSocket = accept(serverSocket, NULL, NULL); if (newSocket < 0) { perror("accept failed"); continue; } printf("New connection accepted\n"); enqueue(&q, newSocket); // Process requests for each client in the queue (not shown) } return 0; }

Quiz 📝

Quick Quiz
Question 1 of 1

What is the main difference between a queue and a stack?

That's it for this tutorial! You now have a comprehensive understanding of creating a queue using a linked list in C programming. With this knowledge, you can confidently implement queues in various real-world scenarios. Happy coding! 🚀🎉