From 3b2246ab2a255710a3ef02fb0f9f19dc92e528a7 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 5 May 2024 22:20:10 +0200 Subject: fixed --- linked | Bin 0 -> 18320 bytes linked.c | 36 ++++++++++++++++-------------------- 2 files changed, 16 insertions(+), 20 deletions(-) create mode 100755 linked diff --git a/linked b/linked new file mode 100755 index 0000000..cbbae44 Binary files /dev/null and b/linked differ diff --git a/linked.c b/linked.c index 55a5ff3..8f47a41 100644 --- a/linked.c +++ b/linked.c @@ -4,6 +4,7 @@ #include #include #include +#include struct waster { int gg; @@ -16,30 +17,26 @@ void *newnode () { return new; } -int llist (uint64_t count, struct waster *wr1) { +struct waster *llist (uint64_t count) { - wr1->gg = 0; // Assign 0 to first node - wr1->next = newnode(); // Create new node and make next point to it - struct waster *save = wr1->next; // Save that address + struct waster *wr1 = malloc(sizeof(struct waster)); + struct waster *head = wr1; + for (uint64_t i = 0 ; i < count ; i++) { - - for (uint64_t i = 1 ; i < count ; i++) { - - save->gg = i; // Assing data to next node - save->next = newnode(); // Make new node and assign address of the new node to the current one - save = save->next; // Make save point to the next node + wr1->gg = i; + 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 } - save->next = NULL; - return 0; + wr1->next = NULL; + return head; } void freellist(struct waster *wr1) { - + //printf("freelist \n"); struct waster *save; - - while (wr1->next != NULL) { + while (wr1 != NULL) { save = wr1; wr1 = wr1->next; free(save); @@ -48,13 +45,12 @@ void freellist(struct waster *wr1) { int main () { - struct waster wr1; - llist(5, &wr1); - struct waster *p = &wr1; - for ( ; p->next != NULL ; p = p->next) { + struct waster *wr1 = llist(10); + struct waster *p; + for (p = wr1 ; p->next != NULL ; p = p->next) { printf("%d\n", p->gg); } - freellist(&wr1); + freellist(wr1); return 0; } \ No newline at end of file -- cgit v1.2.3