diff options
author | oskar <[email protected]> | 2024-05-11 14:10:11 +0200 |
---|---|---|
committer | oskar <[email protected]> | 2024-05-11 14:10:11 +0200 |
commit | 96651caeeceeefd90a9449aece0e23599370fe9f (patch) | |
tree | 3b26e87c5299b24eae229fca01163353e4b69a71 | |
parent | ca4b794c65014884b28c9561683ab96c2b54e7b8 (diff) |
circular doubly linked list, not done yet
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | cd_linkedlist.c | 79 |
2 files changed, 86 insertions, 4 deletions
@@ -4,33 +4,36 @@ CFLAGS=-O3 -flto -march=native -DNDEBUG -fomit-frame-pointer -s -std=gnu99 TARGET1=s_linkedlist TARGET2=d_linkedlist TARGET3=cs_linkedlist +TARGET4=cd_linkedlist TESTTARGET1=s_linkedlist-t TESTTARGET2=d_linkedlist-t TESTTARGET3=cs_linkedlist-t +TESTTARGET4=cd_linkedlist-t INSTALL_DIRECTORY=/usr/local/bin MAKEFLAGS += -s SRCSL=s_linkedlist.c SRCDL=d_linkedlist.c SRCCSL=cs_linkedlist.c +SRCCDL=cd_linkedlist.c all: release clean: - rm -f $(TARGET1) $(TARGET2) $(TARGET3) - rm -f test/$(TESTTARGET1) test/$(TESTTARGET2) test/$(TESTTARGET3) + rm -f $(TARGET1) $(TARGET2) $(TARGET3) $(TARGET4) + rm -f test/$(TESTTARGET1) test/$(TESTTARGET2) test/$(TESTTARGET3) test/$(TESTTARGET4) tests: $(CC) $(CFLAGS_TESTBIN) $(SRCSL) -o test/$(TESTTARGET1) $(CC) $(CFLAGS_TESTBIN) $(SRCDL) -o test/$(TESTTARGET2) $(CC) $(CFLAGS_TESTBIN) $(SRCCSL) -o test/$(TESTTARGET3) - + $(CC) $(CFLAGS_TESTBIN) $(SRCCDL) -o test/$(TESTTARGET4) release: $(CC) $(CFLAGS) $(SRCSL) -o $(TARGET1) $(CC) $(CFLAGS) $(SRCDL) -o $(TARGET2) $(CC) $(CFLAGS) $(SRCCSL) -o $(TARGET3) - + $(CC) $(CFLAGS) $(SRCCSL) -o $(TARGET4) diff --git a/cd_linkedlist.c b/cd_linkedlist.c index e69de29..a1226fd 100644 --- a/cd_linkedlist.c +++ b/cd_linkedlist.c @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <unistd.h> +#include <stdint.h> +#include <stdbool.h> +#include <string.h> + +// circular doubly linked list demo + +struct waster { + int gg; + struct waster *prev; + 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; + + wr1->gg = 0; // Data for first node + wr1->prev = NULL; // There is no previous node *yet* + wr1->next = newnode(); // Make new node, next points to it + struct waster *prv = wr1; // Make prv point to current node + wr1 = wr1->next; // Make wr1 point to next node + + + for (uint64_t i = 1 ; i < count ; i++) { // Actual loop starts + + wr1->gg = i; // Data for 2nd node + wr1->prev = prv; // make prev point to prv = wr1 = last node + if (i == count-1) { + wr1->next = head; + head->prev = wr1; + } else { + wr1->next = newnode(); // Make next node, next points to it + } + prv = wr1; // prv points to current node + wr1 = wr1->next; // Make wr1 point to the next node + + } + + + return head; +} + +void freellist(struct waster *wr1) { + + struct waster *head = wr1; + struct waster *save; + while (wr1->next != head) { + 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) { + void *prev = (void*)p->prev; + void *next = (void*)p->next; + printf("\n------%p----------",(void *)p); + printf("\ndata: %d\nprev: %p\nnext: %p\n", p->gg, prev, next); + printf("-----------------------------\n"); + } + + freellist(wr1); + return 0; +} |