C Programming

How to Implement a Queue in C

A data structure in C is a method of organizing and saving data in memory. C Language includes many inherent and defined user data structures that can be applied to store and modify data in a variety of ways. One of the main user-defined data structures is queues. Queues are frequently utilized in the field of computer science for activities such as system packet handling and event handling.

This article will discuss all aspects of queues and shadow the implementation of queues in C programming.

What are Queues in C

The queue is a data structure with flexibility whose size can be raised in response to demand. Elements of various data types can be stored in the queue. The queue is done using the first-in-first-out approach. The queue structure for data is useful if you need to retrieve data in the same format that it was saved.

Basic Queue Operations in C

Enqueue: This action moves an item from the end side of the queue.

Dequeue: The operation will eliminate the component at the beginning of the queue.

Front: This action returns the first value in a queue without deleting it.

Is_Empty: This operation checks if the queue is null or not.

Rear: The pointer element oversees retrieving the final queue element.

Queues Implementation in C Through Arrays

The implementation of the queues is very simple using arrays to save queue elements. There are two main points in queues; one is the rear pointer which is helpful to add elements in front of the queues and the other is front which is helpful to remove elements from the queue. Both pointers are equal to -1 when the queue is null. The pointer rear cannot make a move forward when the queue is full of its elements.

The following is an array-based queue data structure implementation in C programming.

#include <stdio.h>

int queue[100];

int front,rear = -1;

int empty() {

  return (front == -1 && rear == -1);

}

int full() {
    return (rear == 100-1);
}
void enqueue(int value) {
    if (full()) {
printf("The front queue is full: Error occurred \n");
        return;
    }
    else if (empty()) {
front,rear = 0;
    }
    else {
        rear++;
    }
    queue[rear] = value;
}
void dequeue() {
    if (empty()) {
printf("Error occurs as queue is null \n");
        return;
    }
    else if (front == rear) {
front,rear = -1;
    }
    else {
        front++;
    }
}
int front_element() {
    if (empty()) {
printf("Error occurs as queue is empty\n");
        return -1;
    }
    return queue[front];
}
void print_queue() {
    if (empty()) {
printf("Queue is null\n");
    }
    else {
printf("Elements of Queues are : ");
        for (int i = front; i<= rear; i++) {
printf("%d ", queue[i]);
        }
printf("\n");
    }
}

int main() {
enqueue(2);
enqueue(5);
enqueue(88);
enqueue(46);
enqueue(3);
print_queue();
printf("After calling the dequeue function. \n");
dequeue();
print_queue();
printf("The element present in front of queue is : %d\n", front_element());
    return 0;
}

In the above implementation, to show that the queue is empty, both rear and front indices are set to (-1). The execution starts from the main() function where enqueue() function inserts a component to the queue’s rear by increasing the rear index while setting the queue array’s value at the newly created rear index to the supplied value. By increasing the front index, the dequeue() method eliminates the component that is at the forefront of the queue. The front_element() method returns what’s stored at the queue’s front, while the print_queue() method prints the queue’s components.

Output

Conclusion

The queue is a data structure with a linear arrangement that uses the FIFO pattern, which means that the element that was added to the queue first will be withdrawn first. It provides quick insertion and deletion of queue items. In terms of the allocation of memory, queues are efficient. The above article has shown you various operations that can be performed on queues.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.