Education logo

what is queue in C?

Basics and Implementation of Queues in C Programming

By Pushpendra SharmaPublished 2 years ago 4 min read
Queues in C

Understanding Queues in C: An Overview

In the realm of computer science and data structures, a queue is a widely used linear data structure that follows a particular order in which operations are performed. The order is First In, First Out (FIFO). This means that the first element added to the queue will be the first one to be removed. Queues are essential for various applications such as scheduling processes in operating systems, handling requests in web servers, and managing tasks in real-time systems.

Key Characteristics of a Queue

  • FIFO Principle:

The fundamental property of a queue is that it is a FIFO (First In, First Out) structure. This ensures that elements are processed in the order they were added.

  • Operations:
  1. Enqueue: Adding an element to the end of the queue.
  2. Dequeue: Removing the element from the front of the queue.
  3. Peek/Front: Accessing the element at the front of the queue without removing it.
  4. IsEmpty: Checking whether the queue is empty.
  5. IsFull: Checking whether the queue is full (mainly in the case of a bounded queue).
  6. Use Cases: Queues are used in scenarios where order of processing is important. Some examples include:

(a) Task scheduling

(b) Breadth-first search in graph algorithms

(c) Handling requests in a resource-sharing system

Implementing a Queue in C

To implement a queue in C, we typically use structures and pointers. Here is a simple implementation using an array and a structure.

Array-Based Implementation

In an array-based implementation, we use a fixed-size array to store the queue elements. Here’s a basic example:

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define MAX 100

typedef struct {

int data[MAX];

int front;

int rear;

} Queue;

// Function to initialize the queue

void initQueue(Queue *q) {

q->front = -1;

q->rear = -1;

}

// Function to check if the queue is empty

bool isEmpty(Queue *q) {

return q->front == -1;

}

// Function to check if the queue is full

bool isFull(Queue *q) {

return q->rear == MAX - 1;

}

// Function to add an element to the queue

void enqueue(Queue *q, int value) {

if (isFull(q)) {

printf("Queue is full!\n");

return;

}

if (isEmpty(q)) {

q->front = 0;

}

q->rear++;

q->data[q->rear] = value;

printf("Enqueued %d\n", value);

}

// Function to remove an element from the queue

int dequeue(Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return -1;

}

int value = q->data[q->front];

q->front++;

if (q->front > q->rear) {

q->front = q->rear = -1;

}

printf("Dequeued %d\n", value);

return value;

}

// Function to get the front element of the queue

int front(Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return -1;

}

return q->data[q->front];

}

int main() {

Queue q;

initQueue(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

printf("Front element is %d\n", front(&q));

dequeue(&q);

printf("Front element is %d\n", front(&q));

return 0;

}

Explanation

  • Initialization: The initQueue function initializes the queue by setting front and rear to -1, indicating that the queue is empty.
  • Enqueue: The enqueue function adds an element to the rear of the queue. If the queue is full, it prints an error message.
  • Dequeue: The dequeue function removes an element from the front of the queue. If the queue is empty, it prints an error message.
  • Front: The front function returns the front element of the queue without removing it.

Dynamic Queue Implementation Using Linked List

An alternative to the array-based implementation is using a linked list, which allows for dynamic memory allocation. Here’s a basic example of a queue using a linked list:

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct {

Node *front, *rear;

} Queue;

// Function to initialize the queue

void initQueue(Queue *q) {

q->front = q->rear = NULL;

}

// Function to check if the queue is empty

int isEmpty(Queue *q) {

return q->front == NULL;

}

// Function to add an element to the queue

void enqueue(Queue *q, int value) {

Node* temp = (Node*)malloc(sizeof(Node));

temp->data = value;

temp->next = NULL;

if (q->rear == NULL) {

q->front = q->rear = temp;

return;

}

q->rear->next = temp;

q->rear = temp;

printf("Enqueued %d\n", value);

}

// Function to remove an element from the queue

int dequeue(Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return -1;

}

Node* temp = q->front;

int value = temp->data;

q->front = q->front->next;

if (q->front == NULL) {

q->rear = NULL;

}

free(temp);

printf("Dequeued %d\n", value);

return value;

}

// Function to get the front element of the queue

int front(Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return -1;

}

return q->front->data;

}

int main() {

Queue q;

initQueue(&q);

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

printf("Front element is %d\n", front(&q));

dequeue(&q);

printf("Front element is %d\n", front(&q));

return 0;

}

Explanation

  • Initialization: The initQueue function initializes the queue by setting both front and rear to NULL.
  • Enqueue: The enqueue function adds an element to the end of the queue. It dynamically allocates memory for a new node and updates the rear pointer.
  • Dequeue: The dequeue function removes an element from the front of the queue. It deallocates the memory of the removed node and updates the front pointer.
  • Front: The front function returns the front element of the queue without removing it.

Conclusion

Queues in C are a fundamental data structure that can be implemented in various ways, including using arrays and linked lists. Understanding how to implement and use queues in C is crucial for handling scenarios where the order of processing elements is essential. Whether managing tasks, scheduling processes, or handling real-time data streams, queues provide an efficient and effective solution.

collegecoursesdegreehigh schoolstudentteachervintage

About the Creator

Pushpendra Sharma

I am currently working as Digital Marketing Executive in Tutorials and Examples.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.