Thread: Linked list question...

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

    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
    Join Date
    Aug 2001
    Posts
    129
    You need to create two items for that list: one to traverse though and one to hold the position of first item; ``myitem *head, *current;''

    Now, never change the contents of head (only when you want to create/destroying the list). When you need to dig a specific node from the list do ``current = current->next;'' and jump to the beginning like ``current = head;''

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