commit 44e461b5f6f990a790e732aedcc3f8e06d8e24a9 Author: Sarthak Jain Date: Thu Mar 12 13:22:29 2026 +0530 initial commit diff --git a/llist.c b/llist.c new file mode 100644 index 0000000..0656c88 --- /dev/null +++ b/llist.c @@ -0,0 +1,212 @@ +#include "llist.h" +#include + +List *create_list(void *data) +{ + ListNode *new_node; + + List *new_list = (List *) malloc(sizeof(List)); + *new_list = (ListNode *) malloc(sizeof(ListNode)); + + new_node = *new_list; + new_node->data = data; + new_node->next = NULL; + + return new_list; +} + +List *list_remove(List *list, uint32_t position) +{ + ListNode *head; + ListNode *current; + ListNode *tmp; + + if (list == NULL || *list == NULL) + { + return list; + } + + if (position == 0) + { + head = *list; + head = head->next; + *list = head; + return list; + } + + if (position >= list_size(list)) + { + current = *list; + while(current->next) + { + tmp = current; + current=current->next; + } + + tmp->next = NULL; + free(current); + + return list; + } + + current = *list; + tmp = current; + while (current && position > 0) + { + tmp = current; + current = current->next; + position--; + } + + tmp->next = current->next; + free(current); + + return list; +} + +List *list_add(List *list, void *data, uint32_t position) +{ + ListNode *new_node; + ListNode *head; + ListNode *current; + + if (list == NULL || *list == NULL) + { + return NULL; + } + + head = *list; + + if (head == NULL) + { + head->data = data; + head->next = NULL; + return list; + } + + new_node = (ListNode*) malloc(sizeof(ListNode)); + new_node->data = data; + + if (position == 0) + { + new_node->next = *list; + *list = new_node; + return list; + } + + if (position >= list_size(list)) + { + current = *list; + while (current->next) + { + current = current->next; + } + current->next = new_node; + return list; + } + + /* add at any position */ + current = *list; + while (current && position-- >1) + { + current = current->next; + } + + ListNode *tmp = current->next; + current->next = new_node; + new_node->next = tmp; + + return list; +} + +void print_list(List *list) +{ + ListNode *current; + uint32_t index = 0; + + if (list == NULL || *list == NULL) + return; + + current = *list; + + while (current) + { + printf("At %d value = %p\n", index, current); + index++; + current = current->next; + } +} + +void list_delete(List *list) +{ + ListNode *current; + ListNode *next; + + if (list == NULL || *list == NULL) + return; + + current = *list; + while (current->next) + { + next = current->next; + free(current); + current = next; + } + + free(list); +} + +uint32_t list_size(List *list) +{ + uint32_t size = 0; + ListNode *current; + + if (list == NULL || *list == NULL) + return size; + + current = *list; + while (current) + { + size++; + current = current->next; + } + + return size; +} + +void print_size_and_list(List *list) +{ + printf("size = %d\n", list_size(list)); + print_list(list); +} + +int main(int argc, char **argv) +{ + void *new_data = (void *) malloc(sizeof(void)); + void *new_data2 = (void *) malloc(sizeof(void)); + void *new_data3 = (void *) malloc(sizeof(void)); + void *new_data4 = (void *) malloc(sizeof(void)); + + List *new_list = create_list(new_data); + print_size_and_list(new_list); + + list_add(new_list, new_data2, 0); + print_size_and_list(new_list); + + list_add(new_list, new_data3, 3); + print_size_and_list(new_list); + + list_add(new_list, new_data4, 2); + print_size_and_list(new_list); + + list_remove(new_list, 2); + print_size_and_list(new_list); + + list_delete(new_list); + new_list = NULL; + + print_size_and_list(new_list); + + return 0; +} + diff --git a/llist.h b/llist.h new file mode 100644 index 0000000..34cb6f2 --- /dev/null +++ b/llist.h @@ -0,0 +1,34 @@ +/* + * llist.h + * Generic linked list implementation in C + */ +#ifndef __LLIST_H__ +#define __LLIST_H__ + +#include +#include +#include + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type,member));}) + + +typedef struct ListNode{ + void *data; + struct ListNode *next; +} ListNode; + +typedef ListNode * List; + +List *create_list(void *data); + +uint32_t list_size(List *list); + +List *list_add(List *list, void *data, uint32_t position); + +List *list_remove(List *list, uint32_t position); + +void list_delete(List *list); + +#endif /* __LLIST_H__ */