Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-17-2007, 02:13 PM
emda321 emda321 is offline
Newbie
 
Join Date: Jun 2007
Posts: 3
Rep Power: 0
emda321 is on a distinguished road
Default 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-18-2007 at 12:38 AM. Reason: Added code-tags.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-18-2007, 12:41 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,626
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-18-2007, 03:27 AM
emda321 emda321 is offline
Newbie
 
Join Date: Jun 2007
Posts: 3
Rep Power: 0
emda321 is on a distinguished road
Default thanks alot

thanks alot void,your program was really helpful to me..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C basics. justin1993 C and C++ 4 07-24-2007 08:56 AM


All times are GMT -5. The time now is 06:51 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads