Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added all the Subtypes of Linked List #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions linkedlist/CLL/cll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "cll.h"
#include <stdio.h>

// Function definitions for int type as an example

void insertAtHead__int(CLL__int* cll, int data) {
struct CLLNode__int* newNode = (struct CLLNode__int*)malloc(sizeof(struct CLLNode__int));
newNode->data = data;

if (cll->head == NULL) {
cll->head = cll->tail = newNode;
cll->tail->next = cll->head;
} else {
newNode->next = cll->head;
cll->head = newNode;
cll->tail->next = cll->head;
}
cll->size++;
}

void insertAtTail__int(CLL__int* cll, int data) {
struct CLLNode__int* newNode = (struct CLLNode__int*)malloc(sizeof(struct CLLNode__int));
newNode->data = data;

if (cll->head == NULL) {
cll->head = cll->tail = newNode;
cll->tail->next = cll->head;
} else {
cll->tail->next = newNode;
cll->tail = newNode;
cll->tail->next = cll->head;
}
cll->size++;
}

void deleteHead__int(CLL__int* cll) {
if (cll->head == NULL) return;

struct CLLNode__int* temp = cll->head;
if (cll->head == cll->tail) {
cll->head = cll->tail = NULL;
} else {
cll->head = cll->head->next;
cll->tail->next = cll->head;
}
free(temp);
cll->size--;
}

void deleteTail__int(CLL__int* cll) {
if (cll->head == NULL) return;

if (cll->head == cll->tail) {
free(cll->head);
cll->head = cll->tail = NULL;
} else {
struct CLLNode__int* temp = cll->head;
while (temp->next != cll->tail) {
temp = temp->next;
}
temp->next = cll->head;
free(cll->tail);
cll->tail = temp;
}
cll->size--;
}

void freeCLL__int(CLL__int* cll) {
while (cll->size > 0) {
deleteHead__int(cll);
}
}

void printCLL__int(CLL__int* cll) {
if (cll->head == NULL) {
printf("Circular Linked List is empty.\n");
return;
}

struct CLLNode__int* temp = cll->head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != cll->head);
printf("(head)\n");
}
65 changes: 65 additions & 0 deletions linkedlist/CLL/cll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file : cll.h
* @brief : Header file for the circular linked list implementation.
*/

#ifndef CLL_H
#define CLL_H

#include <stddef.h>
#include <stdlib.h>

#define DECLARE_CLL(type) \
struct CLLNode__##type { \
type data; \
struct CLLNode__##type* next; \
}; \
typedef struct { \
struct CLLNode__##type* head; \
struct CLLNode__##type* tail; \
int size; \
} CLL__##type; \

/**
* @brief Macro to declare function prototypes for circular linked list operations.
*
* This macro declares function prototypes for various operations on the circular linked list
* with a specific data type.
*
* @param type The data type for the circular linked list.
*/
#define CLL_PROTO(type) \
void insertAtHead__##type(CLL__##type* cll, type data); \
void insertAtTail__##type(CLL__##type* cll, type data); \
void insertAt__##type(CLL__##type* cll, type data, int targetIdx); \
void deleteHead__##type(CLL__##type* cll); \
void deleteTail__##type(CLL__##type* cll); \
void deleteAt__##type(CLL__##type* cll, int targetIdx); \
void freeCLL__##type(CLL__##type* cll); \
void printCLL__##type(CLL__##type* cll); \

// Declaration for int data type
DECLARE_CLL(int);
// Declaration for float data type
DECLARE_CLL(float);
// Declaration for char data type
DECLARE_CLL(char);
// Declaration for double data type
DECLARE_CLL(double);

CLL_PROTO(int)
CLL_PROTO(float)
CLL_PROTO(char)
CLL_PROTO(double)

// Macro aliases for function calls
#define insertAtHead(cll, data, type) insertAtHead__##type(cll, data)
#define insertAtTail(cll, data, type) insertAtTail__##type(cll, data)
#define insertAt(cll, data, targetIdx, type) insertAt__##type(cll, data, targetIdx)
#define deleteHead(cll, type) deleteHead__##type(cll)
#define deleteTail(cll, type) deleteTail__##type(cll)
#define deleteAt(cll, targetIdx, type) deleteAt__##type(cll, targetIdx)
#define freeCLL(cll, type) freeCLL__##type(cll)
#define printCLL(cll, type) printCLL__##type(cll)

#endif /* CLL_H */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.