Single-linked list help.

This is a discussion on Single-linked list help. within the C Programming forums, part of the General Programming Boards category; Code: struct link { char *listName; struct link *next; list *currentList; /*pointer to a double-linked list */ }; struct link ...

  1. #1
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42

    Single-linked list help.

    Code:
    struct link {
    	char *listName;
    	struct link *next;
    	list *currentList; /*pointer to a double-linked list */
    };
    
    struct link *first = NULL;
    struct link *current = NULL;
    struct link *previous = NULL;
    
    current = (struct link*) malloc (sizeof(struct link));
    
    if (first == NULL) {
    	first = current;
    }
    
    if (previous != NULL) {
    	previous -> next = current;
    }
    
    //set contents of the new link structure.
    current -> listName = userFirst;
    current -> next = NULL;
    
    previous = current; /* save address of last link structure */
    						
    current = first;
    printSingleList(&current);
    The code above is in a while loop that terminate once the user quits the program. userFirst is declared: char userFirst[50];

    If the user enters c Parameter1 then I create a node with the name Parameter. I then enter:

    c list1
    c list2
    c list3

    Now I go and print out the list from the first node and get:

    list3 list3 list3, instead of list1 list2 list3. Whatever am I doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >struct link {
    > char *listName;
    > struct link *next;
    > list *currentList; /*pointer to a double-linked list */
    >};

    //You need to change:
    char *listName
    //to:
    char listName[50];

    Otherwise you will need to allocate space for listName.

    >//set contents of the new link structure.
    >current -> listName = userFirst;

    //Should be:
    strcpy(current -> listName, userFirst);

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The easiest way to fix single linked list problems, is to not use single linked lists. Use double. They're much easier to use.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Swoopy,

    As I just got done posting I found that the structure (not matter which one I was referring to) would be pointing to the same string. Finally figured out what was wrong (wasn't using strcpy).

    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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