Skip to content

Commit

Permalink
feat: circular-linked-queue
Browse files Browse the repository at this point in the history
  • Loading branch information
PassionPenguin committed Aug 5, 2024
1 parent 1a0fb44 commit c6f18f3
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
96 changes: 96 additions & 0 deletions data-structures/stack-n-queue/queue/circular-linked-queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Created by Hoarfroster on 06/8/2024.
//

#include <stdio.h>
#include <stdlib.h>
#include "circular-linked-queue.h"

// Clear the queue
void clear(CircularLinkedQueue *queue) {
Node *current = queue->rear;
Node *nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
queue->front = NULL;
queue->rear = NULL;
queue->length = 0;
}

// Whether the queue is empty
int empty(CircularLinkedQueue *queue) {
return queue->front == NULL;
}

// Length of the queue
int length(CircularLinkedQueue *queue) {
return queue->length;
}

// Initialization
CircularLinkedQueue *init() {
CircularLinkedQueue *queue = (CircularLinkedQueue *) malloc(sizeof(CircularLinkedQueue));
if(queue == NULL) {
printf("Failed to allocate memory\n");
exit(1);
}

queue->front = NULL;
queue->rear = NULL;
queue->length = 0;
return queue;
}

// Destroy
void destroy(CircularLinkedQueue *queue) {
clear(queue);
free(queue);
}

// Push an element to the rear
int enqueue(CircularLinkedQueue *queue, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
return 0; // failed to allocate memory - i.e. queue overflow
}
newNode->data = value;
if (empty(queue)) {
newNode->next = newNode; // link circularly
queue->front = newNode;
queue->rear = newNode;
} else {
// Insert new node at the rear
newNode->next = queue->rear->next;
queue->rear->next = newNode;
queue->rear = newNode;
}
queue->length++;

return 1;
}

// Pop the front element
int dequeue(CircularLinkedQueue *queue, int *value) {
if (queue->front == NULL) {
return 0; // queue already empty
}

Node *temp = queue->front;
*value = temp->data;
if (queue->length == 1) {
// if only containing one element
queue->front = NULL;
queue->rear = NULL;
} else {
// remove the front element
queue->front = temp->next;
queue->rear->next = queue->front;
}
free(temp);
queue->length--;

return 1;
}
40 changes: 40 additions & 0 deletions data-structures/stack-n-queue/queue/circular-linked-queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by Hoarfroster on 06/8/2024.
//

#ifndef C_ALGORITHM_CIRCULAR_LINKED_QUEUE_H
#define C_ALGORITHM_CIRCULAR_LINKED_QUEUE_H

typedef struct Node {
int data;
struct Node *next;
} Node;

typedef struct CircularLinkedQueue {
Node *front;
Node *rear;
int length;
} CircularLinkedQueue;

// Clear the queue
void clear(CircularLinkedQueue *queue);

// Whether the queue is empty
int empty(CircularLinkedQueue *queue);

// Length of the queue
int length(CircularLinkedQueue *queue);

// Initialization
CircularLinkedQueue *init();

// Destroy
void destroy(CircularLinkedQueue *queue);

// Push an element to the rear
int enqueue(CircularLinkedQueue *queue, int value);

// Pop the front element
int dequeue(CircularLinkedQueue *queue, int *value);

#endif //C_ALGORITHM_CIRCULAR_LINKED_QUEUE_H
32 changes: 32 additions & 0 deletions data-structures/stack-n-queue/queue/circular-linked-queue_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by Hoarfroster on 06/8/2024.
//

#include <stdio.h>
#include "circular-linked-queue.h"

int main() {
CircularLinkedQueue *queue = init();
int value;

enqueue(queue, 1);
printf("Enqueued %d to front\n", 1);
dequeue(queue, &value);
printf("Dequeued %d from rear\n", value);
enqueue(queue, 2);
printf("Enqueued %d to front\n", 2);
enqueue(queue, 3);
printf("Enqueued %d to front\n", 3);

printf("Length of the queue: %d\n", length(queue));

dequeue(queue, &value);
printf("Dequeued %d from rear\n", value);
dequeue(queue, &value);
printf("Dequeued %d from rear\n", value);

clear(queue);
destroy(queue);

return 0;
}

0 comments on commit c6f18f3

Please sign in to comment.