summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoroskar <[email protected]>2024-05-11 13:51:17 +0200
committeroskar <[email protected]>2024-05-11 13:51:17 +0200
commitca4b794c65014884b28c9561683ab96c2b54e7b8 (patch)
treee09fc153b8b069bafc1154b1e187ebe366a2da10
parentec3079e3f4a0124c744ec7e5b7a3fe78d2bbca77 (diff)
circular linked list
-rw-r--r--Makefile13
-rw-r--r--cd_linkedlist.c0
-rw-r--r--cs_linkedlist.c76
-rw-r--r--d_linkedlist.c2
-rw-r--r--s_linkedlist.c2
5 files changed, 87 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index ec5930a..bf658a9 100644
--- a/Makefile
+++ b/Makefile
@@ -3,28 +3,33 @@ CFLAGS_TESTBIN=-O3 -Wfatal-errors -Wall -Werror -Wextra -g -Wpedantic -std=c99
CFLAGS=-O3 -flto -march=native -DNDEBUG -fomit-frame-pointer -s -std=gnu99
TARGET1=s_linkedlist
TARGET2=d_linkedlist
+TARGET3=cs_linkedlist
TESTTARGET1=s_linkedlist-t
TESTTARGET2=d_linkedlist-t
+TESTTARGET3=cs_linkedlist-t
INSTALL_DIRECTORY=/usr/local/bin
MAKEFLAGS += -s
SRCSL=s_linkedlist.c
SRCDL=d_linkedlist.c
+SRCCSL=cs_linkedlist.c
all: release
clean:
- rm -f $(TARGET1) $(TARGET2)
- rm -f test/$(TESTTARGET1) test/$(TESTTARGET2)
+ rm -f $(TARGET1) $(TARGET2) $(TARGET3)
+ rm -f test/$(TESTTARGET1) test/$(TESTTARGET2) test/$(TESTTARGET3)
tests:
$(CC) $(CFLAGS_TESTBIN) $(SRCSL) -o test/$(TESTTARGET1)
- $(CC) $(CFLAGS_TESTBIN) $(SRCDL) -o test/$(TESTTARGET2)
- echo "$(TESTTARGET) is done."
+ $(CC) $(CFLAGS_TESTBIN) $(SRCDL) -o test/$(TESTTARGET2)
+ $(CC) $(CFLAGS_TESTBIN) $(SRCCSL) -o test/$(TESTTARGET3)
+
release:
$(CC) $(CFLAGS) $(SRCSL) -o $(TARGET1)
$(CC) $(CFLAGS) $(SRCDL) -o $(TARGET2)
+ $(CC) $(CFLAGS) $(SRCCSL) -o $(TARGET3)
diff --git a/cd_linkedlist.c b/cd_linkedlist.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/cd_linkedlist.c
diff --git a/cs_linkedlist.c b/cs_linkedlist.c
new file mode 100644
index 0000000..1bbbb38
--- /dev/null
+++ b/cs_linkedlist.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+// circular 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 = head;
+ } 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
+
+ }
+
+
+ return head;
+}
+
+void freellist(struct waster *wr1) {
+ //printf("freelist \n");
+ struct waster *head = wr1;
+ struct waster *save;
+ while (wr1->next != head) {
+ save = wr1;
+ wr1 = wr1->next;
+ free(save);
+ }
+}
+
+int main () {
+
+ uint64_t passes = 4; // 1 pass = the whole linked list. So 4 passes would be to print the linked list 4 times.
+ uint64_t passstep = 1; // counter to compare to passes
+ uint64_t steps = 20; // amount of nodes to create
+ struct waster *wr1 = llist(steps); // Create the nodes
+
+ while(passstep != passes) {
+
+
+ uint64_t steps_count = 0;
+ struct waster *p;
+ for (p = wr1 ; steps_count < steps ; p = p->next, steps_count++) {
+ printf("\n------%p----------", (void*)p);
+ printf("\ndata: %d\nnext: %p\n", p->gg, (void*)p->next);
+ printf("-----------------------------\n");
+ }
+ printf("\npass %lld done\n", passstep);
+ passstep = passstep+1;
+ }
+ printf("\nHEAD: %p\n", (void*)wr1);
+ freellist(wr1);
+ return 0;
+}
diff --git a/d_linkedlist.c b/d_linkedlist.c
index 025bbb9..9246dce 100644
--- a/d_linkedlist.c
+++ b/d_linkedlist.c
@@ -6,7 +6,7 @@
#include <stdbool.h>
#include <string.h>
-// Singly linked list demo
+// doubly linked list demo
struct waster {
int gg;
diff --git a/s_linkedlist.c b/s_linkedlist.c
index 761a262..b2f79ad 100644
--- a/s_linkedlist.c
+++ b/s_linkedlist.c
@@ -6,7 +6,7 @@
#include <stdbool.h>
#include <string.h>
-// Singly linked list demo
+// singly linked list demo
struct waster {
int gg;