Linked list problem

This is a discussion on Linked list problem within the C Programming forums, part of the General Programming Boards category; I get this problem where I am copying words into a list as words are entered. Essentially everytime a word ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Linked list problem

    I get this problem where I am copying words into a list as words are entered. Essentially everytime a word is entered, I create a node and link that into the list.

    At the end, I print out the list, but for some reason I only get the last word that I entered printing, several times.

    Code:
    list print () {
    
      list current = head;
      while(current != NULL) {
        printf("%s\n", current->string);
        current = current->next;
      }
      return (head);
    }
    
    void list (char *input) {
    
      list node;
    
      node = makeN();
      
      if(head == NULL) {
        head = node;
        tail =node;
        head->string= input;
        tail->string = input;
      }
      
      else {
        tail->next = node;
        tail = tail->next;
        tail->word = input;
      }
    
    
    }
    My head and tail are globals
    Last edited by JFonseka; 05-23-2008 at 06:07 AM.
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without reading the code I can tell you that you are storing the address of the string entered. You need to copy the string into the linked list. You can either have a fixed size array or allocate memory dynamically according to the length of the word [and remember to make space for the zero at the end of the word].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ah, hehe, that worked =)

    THANKS!
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21