๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ queue โ€บ index.php
C Programming Exercises, Practice, Solution : Queue
October 16, 2025 - Write a C program to implement a queue using an array. Create a function that removes an element from the queue. ... Initialize a queue! Check the queue is empty or not? Yes Insert some elements into the queue: 1 2 3 Insert another element into ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ data_structures_algorithms โ€บ queue_program_in_c.htm
Queue Program In C
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #define MAX 6 int intArray[MAX]; int front = 0; int rear = -1; int itemCount = 0; int peek() { return intArray[front]; } bool isEmpty() { return itemCount == 0; } bool isFull() { return itemCount == MAX; } int size() { return itemCount; } void insert(int data) { if(!isFull()) { if(rear == MAX-1) { rear = -1; } intArray[&plus;&plus;rear] = data; itemCount&plus;&plus;; } } int removeData() { int data = intArray[front&plus;&plus;]; if(front == MAX) { front = 0; } itemCount--; return data; } int main() { /* insert 5 it
Discussions

data structures - How can I implement a basic queue in C? - Stack Overflow
I am trying to learn data structures and I am struggling with getting this code to work. Problem is I am getting segmentation fault(core dumped) with gcc C compiler. It is supposed to be a queue. ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
My generic queue implementation in C
As already stated here, dynamic array implementation is generally better. Also, you used too many indirection levels and naming convention is quite inconsistent and problematic. Finally, in real life applications, one generally avoids generic programming in C unless it is strictly required (yours is an exercise so it's ok). More on reddit.com
๐ŸŒ r/C_Programming
11
28
November 1, 2025
How can I create a queue out of structure arrays?
Queue is an abstract data structure; that is to say, only the behavior (FIFO) is specified, and it is not restricted to any particular concrete implementation. Arrays are the best choice to implement small fixed-size queues, but your requirement seems to be that of a linked list based implementation (due to the phrase "queue node" and the member next in TrainDetails structure). Here is a brief outline of the approach (assume that malloc does not fail): Define pointers for the two ends of a queue: TrainDetails *front, *rear; When the first node is added, it will act as both front and rear. Example: (front = rear = malloc(sizeof *rear))->next = NULL; Assign rear->train_id and rear->train_time as per the requirement. Subsequent enqueue operations will change rear to the new node, without changing front. Example: (rear = rear->next = malloc(sizeof *rear))->next = NULL; Conversely, dequeue operations will change front to the next node, without changing rear (except when removing the last node). Example: void *temp = front; front = front->next; free(temp); if (!front) rear = NULL; More on reddit.com
๐ŸŒ r/C_Programming
5
0
August 8, 2022
First time making a queue in C
Do you have a question? More on reddit.com
๐ŸŒ r/C_Programming
15
1
January 5, 2025
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ queue-in-c
How to Create a Queue in C (With Code Examples) | DigitalOcean
May 2, 2025 - Learn how to implement a queue in C using arrays and linked lists. Includes step-by-step code, enqueue/dequeue operations, and practical examples.
๐ŸŒ
Dremendo
dremendo.com โ€บ c-programming-tutorial โ€บ c-queue
Queue in C Programming | Dremendo
In the above image, we can see an array named arr whose size is 5. We take two variables R and F, The variable R stands for rear and the default value is -1. The variable F stands for front and the default value is 0. For add operation in the queue first, we check if the value of R is equal to the value of size-1 then, we will display a message Queue is full, else we will increase the value of R by 1 and add the element in the array at the new location of R.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ queue-in-c
Queue In C | C Program To Implement Queue | Edureka
March 29, 2022 - A lady is entering the names of all the people in a file. The person who comes first gets places first. When the doctor is free, he calls the first patient inside. This is a queue and follows a first in first out method as the first person to ...
๐ŸŒ
Thiyagaraaj
c-lang.thiyagaraaj.com โ€บ data-structures โ€บ c-queue-programs โ€บ simple-queue-program-in-c-programming
Simple Queue Program in C Programming - C Programming
Simple Queue Example - Array Queue Main Menu 1.Insert 2.Remove 3.Display Others to exit Enter Your Choice : 1 Enter The Value to be Insert : 100 ## Position : 1 , Insert Value : 100 Queue Main Menu 1.Insert 2.Remove 3.Display Others to exit Enter Your Choice : 1 Enter The Value to be Insert : 200 ## Position : 2 , Insert Value : 200 Queue Main Menu 1.Insert 2.Remove 3.Display Others to exit Enter Your Choice : 1 Enter The Value to be Insert : 300 ## Position : 3 , Insert Value : 300 Queue Main Menu 1.Insert 2.Remove 3.Display Others to exit Enter Your Choice : 1 Enter The Value to be Insert :
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ queue-in-c
Queue in C - GeeksforGeeks
March 8, 2026 - Current Queue: 10 Current Queue: 10 20 Current Queue: 10 20 30 Front element: 10 Current Queue: 20 30 Front element after dequeue: 20 ยท The queue above works fine only for single usage. For example, lets fill the queue completely and then dequeue all the elements.
๐ŸŒ
Softdevlead
softdevlead.com โ€บ home โ€บ queue in c: theory, code examples, and real-world applications
Queue in C: Theory, Code, and Real-World Applications
April 5, 2025 - Data Buffering: Queues are used to manage data that arrives in a stream, such as network packets. Order Processing: For instance, in e-commerce websites, order requests can be processed in a queue, ensuring that orders are handled in the order they are received...
Find elsewhere
๐ŸŒ
EmbeTronicX
embetronicx.com โ€บ tutorials โ€บ p_language โ€บ c โ€บ queue-in-c
Queue in C Introduction and Implementation- DS 4 โ‹† EmbeTronicX
October 28, 2023 - CPU scheduling, Disk Scheduling. When data is transferred asynchronously between two processes, the queue is used for synchronization. For example IO Buffers, pipes, file IO, etc.
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ computer science courses โ€บ computer science 111: programming in c
Queues in C Programming: Uses & Example | Study.com
June 20, 2024 - In this lesson, we will discuss the concept of Queues in C programming. We will understand the operations first-in first-out (FIFO) and last-in first-out (LIFO) and how they are different. Queue operations enqueue and dequeue are also demonstrated.
Top answer
1 of 2
3

As mentioned by OctaveL, in the function create you try to set the fields of the queue but the pointer passed to the function does not point to a queue, it is uninitialized. If you add the option -Wall to gcc it will actually warn you about this:

$ gcc -o test -Wall test.c
test.c: In function 'main':
test.c:71:5: warning: 'q' is used uninitialized in this function [-Wuninitialized]
     create (q);
     ^~~~~~~~~~

Solution 1: Declare q as a record and pass the address of q to the function create:

struct queue q;
create (&q);

Solution 2: Declare q as a pointer and allocate a new queue variable:

struct queue *q;
q = malloc(sizeof *q);
create(q);

I would also advice you to rename the function create to init or clear since it doesn't create a new queue, it only initializes (or clears) it.

To make memory allocation easier and to handle errors properly it is convenient to introduce two macros:

#define NEW_ARRAY(ptr, n) \
    (ptr) = malloc((n) * sizeof (ptr)[0]); \
    if ((ptr) == NULL) { \
        fprintf(stderr, "Memory allocation failed: %s\n", strerror(errno)); \
        exit(EXIT_FAILURE); \
    }

#define NEW(ptr) NEW_ARRAY(ptr, 1)

With these in place and if create is renamed to init you can write solution 2 as

struct queue *q;
NEW(q);
init(q);
2 of 2
2

You didn't allocate memory for q in your main(), so it crashes when attempting to access q->front in create().

int main (void) {
    struct queue *q; // No allocation here
    ...
}

You probably wanted this, which works just fine:

int main (void) {
    struct queue q;
    create (&q);
    enqueue(&q, 5);
}
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ cpp_queues.asp
C++ Queues
// Create a queue of strings called cars queue<string> cars; Note: The type of the queue (string in our example) cannot be changed after its been declared.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-implement-queue
Queue Program in C (Implementation and Examples) - Sanfoundry
November 18, 2022 - Example: Insert the new element 23 in the given queue. Here, the size of the queue is 6. Step 1: Rear is incremented by one, and moved from index 3 to 4. ... Time Complexity: O(1) It takes constant time to shift the rear and insert new data ...
๐ŸŒ
Vocal Media
vocal.media โ€บ education โ€บ what-is-queue-in-c
what is queue in C? | Education - Vocal Media
Front: The front function returns the front element of the queue without removing it. 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:
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ queue in c
Queue in C - Scaler Topics
October 31, 2023 - Now, there can be a case where we delete all elements in the queue, and in that case, the value of the front variable will exceed the value of the rear variable because in the process of deletion, we keep increasing the front variable, but the rear variable remains the same. This means that the queue is now empty, and we should reset the front and rear variable to -1. The time complexity of this operation is ... Let us look at an example to understand all these operations better.
๐ŸŒ
CodeScracker
codescracker.com โ€บ c โ€บ c-queues.htm
Queue in C programming with an example program
In real life, queues are very common. There are lines at every restaurant and ticket counter where people stand in order to purchase a ticket. The first individual in line will be given the ticket first. To illustrate how a queue operates, consider the functions qstore() and qretrieve().
๐ŸŒ
Learn C
learnc.net โ€บ home โ€บ c data structures โ€บ c queue
C Queue
April 13, 2025 - Donโ€™t confuse a queue with a stack, as a stack works based on the last-in-first-out (LIFO) principle. A good example of a queue is a line of customers in front of a shop.
๐ŸŒ
Medium
medium.com โ€บ @theodoretsori โ€บ learn-about-queues-in-c-programming-definitions-uses-and-examples-10fb99e7f77d
Learn about Queues in C Programming: Definitions, Uses, and Examples | by Theodore Tsori | Medium
December 13, 2022 - In this example, the queue data structure contains an array data to store the elements of the queue, as well as two indices: front and rear to keep track of the front and rear elements of the queue, respectively.
๐ŸŒ
Medium
medium.com โ€บ @komukenneth216 โ€บ stacks-and-queues-in-c-f2300c26e6e2
Stacks and Queues in C. In this Stacks and Queues in Cโ€ฆ | by Komukenneth | Medium
July 11, 2023 - Exit\n\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(0); break; default: printf("Sorry, invalid choice!\n"); break; } } while(choice!=4); return 0; } void insert() { int element; if (rear == LIMIT - 1) printf("Queue Overflow\n"); else { if (front == - 1) front = 0; printf("Enter the element to be inserted in the queue: "); scanf("%d", &element); rear++; queue[rear] = element; } } void delete() { if (front == - 1 || front > rear) { printf("Queue Underflow \n"); } else { printf("The deleted element in the queue is: %d\n", queue[front]); front++; } } void display() { int i; if (front == - 1) { printf("Queue underflow\n"); } else { printf("The elements of the queue are:\n"); for (i = front; i <= rear; i++) printf("%d\n", queue[i]); } }
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ stacks-and-queues-in-c
Stacks and Queues in C โ€“ Master the Concepts of LIFO & FIFO - DataFlair
December 30, 2022 - In order to better understand the concept of queues in C, we can say that it follows the rule of โ€œFirst Come First Serveโ€. Let us consider a simple scenario to help you get a clear picture of queues. Suppose you want to purchase a movie ticket. ...