Linked list question...

This is a discussion on Linked list question... within the C++ Programming forums, part of the General Programming Boards category; Hi there.. I have a simple question. How do you send the pointer back to the start of one's list ...

  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;''
    kooma - t.h@iki.fi

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, 08: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, 09:55 PM
  4. 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