0% found this document useful (0 votes)
6 views

Queue_Implementation

This document contains a C program that implements a queue data structure using an array. It defines functions to check if the queue is empty or full, as well as to enqueue and dequeue elements. The main function demonstrates the usage of these queue operations.

Uploaded by

kisherno.webster
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Queue_Implementation

This document contains a C program that implements a queue data structure using an array. It defines functions to check if the queue is empty or full, as well as to enqueue and dequeue elements. The main function demonstrates the usage of these queue operations.

Uploaded by

kisherno.webster
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h> //The standard input library header file, without this you
cannot make you of the 'main' function below"
#include <stdbool.h> //The header file library that allows us to use the "bool"
(for Boolean) declarator

#define MAX_LENGTH 100 //The max length that a queue can be

typedef struct {
int elements[MAX_LENGTH]; //The actually queue of the structure
int head; //The head of the queue
int tail; //The tail of the queue
}Queue;

bool isEmpty(Queue* queue) {


return ((queue->head-queue->tail) == 0); //Return 0 (False) if their is
no space between the head and the tail, otherwise its true
}

bool isFull(Queue* queue) {


return (queue->head == 0); //Returns 1 (True) if their if the head is
equal to zero.
}

void enqueue(Queue* queue, int element) {


if (!isFull(queue)) { //Simply checks if the queue is full by running
the previously defined "isFull()" function and passing the queue as an argument
--queue->head; //Decrements the head by one to make space for
the incoming element
queue->elements[queue->head] = element; //The new element is now
inserted at the new beginning of the queue after the head was decremented
printf("\n%d", element); //Prints the newly added element to the
console
}
else {
printf("\nThe queue is full, please wait"); //If the queue is full, not
elements can be inserted.
}
}

void dequeue(Queue* queue) {


if (!isEmpty(queue)) { //Simply checks if the queue is empty.
--queue->tail; //Decrements the tail by one to remove the
element at the end
printf("\n%d", queue->elements[queue->tail]); //Prints out the new
element at the new tail
}
else {
printf("\nThe queue is empty, you cannot dequeue");
}
}

int main() {
Queue my_queue = {.elements = {}, .tail = MAX_LENGTH, .head = MAX_LENGTH};
enqueue(&my_queue, 0);
enqueue(&my_queue, 2);
enqueue(&my_queue, 4);
dequeue(&my_queue);
}

You might also like