From ec3079e3f4a0124c744ec7e5b7a3fe78d2bbca77 Mon Sep 17 00:00:00 2001 From: oskar Date: Fri, 10 May 2024 23:41:25 +0200 Subject: Implemented doubly linked list, fixed issued with singly linked list. Added some nicer visualization for both of them. --- s_linkedlist.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 s_linkedlist.c (limited to 's_linkedlist.c') diff --git a/s_linkedlist.c b/s_linkedlist.c new file mode 100644 index 0000000..761a262 --- /dev/null +++ b/s_linkedlist.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include + +// Singly linked list demo + +struct waster { + int gg; + struct waster *next; +}; + +struct waster *newnode () { + + struct waster *new = malloc(sizeof(struct waster)); + return new; +} + +struct waster *llist (uint64_t count) { + + struct waster *wr1 = malloc(sizeof(struct waster)); + struct waster *head = wr1; + for (uint64_t i = 0 ; i < count ; i++) { + + wr1->gg = i; + if (i == count-1) { + wr1->next = NULL; + } else { + wr1->next = newnode(); // Make new node and assign address of the new node to the current one + } + wr1 = wr1->next; // Make save point to the next node + + } + + wr1 = NULL; + return head; +} + +void freellist(struct waster *wr1) { + //printf("freelist \n"); + struct waster *save; + while (wr1 != NULL) { + save = wr1; + wr1 = wr1->next; + free(save); + } +} + +int main () { + + struct waster *wr1 = llist(20); + struct waster *p; + for (p = wr1 ; p != NULL ; p = p->next) { + printf("\n------%p----------", (void*)p); + printf("\ndata: %d\nnext: %p\n", p->gg, (void*)p->next); + printf("-----------------------------\n"); + } + printf("\nHEAD: %p\n", (void*)wr1); + freellist(wr1); + return 0; +} -- cgit v1.2.3