Closed Thread
Results 1 to 3 of 3

Thread: adding new nodes in a linked list while looping

  1. #1
    emda321 is offline Newbie
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

    adding new nodes in a linked list while looping

    hello everybody

    i wanna creat a linked list,and the list is contained in a big loop.and i'm supposed to create new DIFFERENT nodes containg different information that can be accessed later during each iteration of the loop.i tried to dynamically allocate a new node each iteration and save the data and then delete the pointer.but strangely in the next iteration when i allocate the pointer again,it points to the previous node and overwrites its data so eventually i'll have one node only!!!..can u plz tell me why??

    here's my code..

    Code:
    struct user * f;   //front pointer
    f=NULL;
    struct user * r;     //rear pointer
    r=NULL;
    struct user * temp;     //the temporary pointer that i use to allocate
    temp=NULL;
    int back;
    back=1;
    while(back==1)
    {
    if(f==NULL)     //first node to be added
       {
        temp=(struct user *)malloc(sizeof(struct user));    //user is my structure
       
        temp->link=NULL;                      //the link field to the following node
       stpcpy((temp->data1),input1);
       stpcpy((temp->data2),input2);
       f=temp;
       r=f;
       free(temp);
      
       }
       else
       {
        temp=(struct user *)malloc(sizeof(struct user));
       temp->link=NULL;
       stpcpy((temp->data1),input1);
       stpcpy((temp->data2),input2);
       r->link=temp;
       r=temp;
        free(temp);
    
        }
    }
    thanks alot...
    Last edited by v0id; 06-17-2007 at 09:38 PM. Reason: Added code-tags.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I have made a complete example of using Linked List, that you may find interesting. You can eventually use it to see how it can be done.

    You can find it on my homepage here:
    v0id.dk
    and a direct link here:
    http://v0id.dk/fv/code/c/linked_list.c

  4. #3
    emda321 is offline Newbie
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

    thanks alot

    thanks alot void,your program was really helpful to me..

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Linked List in C
    By crazycaw in forum C and C++
    Replies: 1
    Last Post: 07-10-2010, 08:54 PM
  2. Replies: 2
    Last Post: 06-28-2010, 08:45 PM
  3. help with Linked List
    By M0SS_99 in forum C and C++
    Replies: 3
    Last Post: 01-08-2010, 03:18 PM
  4. Replies: 5
    Last Post: 12-09-2008, 02:36 PM
  5. Linked list
    By BINNY88 in forum C and C++
    Replies: 5
    Last Post: 10-03-2008, 06:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts