Thread: Circular double linked list not working

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    2

    Circular double linked list not working

    So I need to create a circular double linked list with the names of some students. But after I make the add function and the create list function, when I try to print the list , the root of my list is always changing to the last student added and I don't understand why. Can someone help me please?

    Here is the code I wrote : #include <stdio.h> #include <stdlib.h> #include <string.h> unsigned n; s - Pastebin.com

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    700
    Unless your code is huge, you can always paste it here between [code][/code] tags:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    unsigned n;
     
    struct node
    {
      char *key;
      struct node *next, *prev;
    };
     
    void addBack(struct node *root,struct node *end, char *newkey)
    {
      struct node *newnode;
      newnode = malloc(sizeof(struct node));
     
      end->next = newnode;
      newnode->key = newkey;
      newnode->prev = end;
      newnode->next = root;
    }
     
    struct node *createList(FILE *fp)
    {
      char *name = malloc(512 * sizeof(char));
      struct node *root = malloc(sizeof(struct node));
      fscanf(fp, "%s", name);
      root->key = name;
      root->next = root->prev = NULL;
      struct node *end = root;
      for(int i = 1 ; i < n; i++)
        {
          fscanf(fp , "%s", name);
          addBack(root, end, name);
          end = end->next;
        }
     
      return root;
    }
     
    void printList(struct node *root)
    {
      printf("%s ", root->key);
      printf("\n");
    }
     
    int main()
    {
      FILE *fp;
      fp = fopen("name.txt", "r");
      printf("Enter the number of students:\n");
      scanf("%u", &n);
      struct node *root = createList(fp);
      printList(root);
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,372
    You need to replace all your root->key assignments with something that does a malloc followed by a strcpy.

    All your strings point to the same bit of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2018
    Posts
    2
    Yep , that fixed it. Thank you !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circular double linked list issue ..need help
    By spanlength in forum C Programming
    Replies: 3
    Last Post: 06-30-2012, 12:03 AM
  2. how do i reverse a circular double linked list
    By kobra_swe in forum C Programming
    Replies: 13
    Last Post: 04-08-2008, 04:20 PM
  3. Circular Double Linked List - Logic in Operator=
    By INFERNO2K in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 12:45 PM
  4. Circular linked list
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2004, 11:53 AM
  5. Circular Linked list ....
    By yescha in forum C Programming
    Replies: 1
    Last Post: 11-17-2001, 03:41 AM

Tags for this Thread