This repository contains a simple implementation of a queue data structure in the C programming language. The queue follows the First In, First Out (FIFO) principle, and the implementation supports common queue operations such as push, pop, front, back, and check for emptiness as well as the current size of the queue.
- Dynamic queue implementation (using a resizable array or linked list).
- Basic operations:
Create
: Creates the queue datastructure. -create_queue
Push
: Add an element to the back of the queue. -queue_push
Pop
: Remove the front element of the queue. -queue_pop
Front
: View the front element without removing it. -check_queue_front
Back
: View the back element without removing it. -check_queue_back
IsEmpty
: Check if the queue is empty. -check_queue_is_empty
Size
: Get the current size of the queue. -check_queue_size
Free
: Frees the queue datastructure. -free_queue
To compile and run the program, you need:
- A C compiler (e.g., GCC, Clang, or MSVC).
- Basic knowledge of working with C files.
-
Clone the repository:
git clone https://github.com/TiagoRodrigues1111/queue cd queue
-
Compile the code using one of the following options:
gcc queue.c simplemain.c -o queue
gcc queue_ll.c simplemain.c -o queue
make all/queue_test # uses queue.c make queue_ll # uses queue_ll.c
-
Run the executable:
./queue
make run
- queue.c: Contains the implementation of the queue functions (array-based)
- queue_ll.c: Contains the implementation of the queue functions (linked list-based)
- queue.h: Header file with function declarations and definitions
- simplemain.c: Example usage of the queue
- README.md: Documentation for the project
- Makefile: Simple Makefile
.
├── queue.c
├── queue_ll.c
├── queue.h
├── simplemain.c
├── README.md
└── Makefile
- Description: Creates the queue datastructure
- Input: The pointer to the memory position of the queue, the datatype size, and number of elements to allocate.
- Example:
void *queue= NULL; create_queue(&queue, sizeof(uint8_t), 10);
- Description: Adds an element to the front of the queue.
- Input: The pointer to the queue, as well as a void* to the input data.
- Example:
uint8_t data=10; queue_push(queue,(void*)&data);
- Description: Removes the back element from the queue.
- Output: None.
- Example:
queue_pop(queue);
- Description: Returns the front element of the queue without removing it.
- Output: The starting memory position of the element.
- Example:
uint8_t front = *((uint8_t*)check_queue_front(queue)); printf("Front element: %u\n",front);
- Description: Returns the back element of the queue without removing it.
- Output: The starting memory position of the element.
- Example:
uint8_t back = *((uint8_t*)check_queue_back(queue)); printf("Back element: %u\n",back);
- Description: Checks if the queue is empty.
- Output: A boolean value (0 or 1).
- Example:
if (check_queue_is_empty(queue)) { printf("Queue is empty\n"); }
- Description: Returns the number of elements currently in the queue.
- Output: An uint64_t representing the queue size.
- Example:
uint64_t size = check_queue_size(queue); printf("Queue size: %lu\n", size);
- Description: Frees the queue datastructure.
- Output: None.
- Example:
free_queue(queue);
Here is a simple example of how the queue can be used (tutorial() in simplemain.c):
#include <stdio.h>
#include "queue.h"
int main()
{
void *queue1 = NULL;
create_queue(&queue1, sizeof(uint16_t),5); // create a queue of 5 elements of uint16_t size
uint16_t data1 = 3;
queue_push(queue1,(void*) &data1); // Push a value onto the queue
data1 = 20;
queue_push(queue1,(void*) &data1); // Push another value onto the queue
if(!check_queue_is_empty(queue1)) // Checks if queue is not empty
{
printf("%u\n",*((uint16_t*)check_queue_front(queue1))); // prints front of queue
}
if(!check_queue_is_empty(queue1)) // Checks if queue is not empty
{
printf("%u\n",*((uint16_t*)check_queue_back(queue1))); // prints back of queue
}
queue_pop(queue1); // Pops a value from the queue
printf("%lu\n",check_queue_size(queue1)); // prints size of the queue
free_queue(queue1); // frees the queue
return 0;
}
uint64_t is the size of the queue, therefore comparisons of size must be to zero (unlike other implementations which might use size = -1 to initialize queue).
It is assumed that the queue will never have more than 2^64 elements (as in the back).
It is assumed that the user will grab only 1 element size from the memory position returned from check_queue_front check_queue_back.
Contributions are welcome! If you find any issues or have ideas for improvement, feel free to open an issue or submit a pull request.
This project is licensed under the Unlicense license. See the LICENSE
file for more details.