Thread: Linked list question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Question Linked list question

    Hi there..

    I have a simple question.

    How do you send the pointer back to the start of one's list after creating it..

    I have this:
    typedef struct myitem
    {
    char *listitem;
    struct myitem *next;
    } MYLIST;


    I use the function:
    void slstore(struct myitem *i)
    {
    static struct myitem *last=NULL;
    if(!last) last = i;
    i->next = NULL;
    last = i;

    }

    to store my items into the list..

    I want to use
    void display(struct myitem *top)
    {
    while (top){
    printf("%s\n",top->listitem);
    top = top->next;
    }
    }

    But I guess I need to put the pointer back to the start of the list before calling display..

    Any ideas on how to do that?


    //kor

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I have just learnt linked list from my book as well. To set the pointer back to the top, one way is to loop through the pointer and stop when it reaches the top. Or sipmly set the pointer to point to the first node of the list, which is alwasy the top of the list even though u modify it during the program if u know what i mean.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Don't crosspost. See same thread on C++ board.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM