Linked list question

This is a discussion on Linked list question within the C++ Programming forums, part of the General Programming Boards category; Hey all I've got a problem adding a new node to a linked list. Firstly, here's the relevant code: Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Linked list question

    Hey all

    I've got a problem adding a new node to a linked list. Firstly, here's the relevant code:

    Code:
    void addToList(Record *rptr)
    {
    	Record *nnode;
    
    	if ((nnode = (Record *) malloc(sizeof(Record))) == NULL)
    	{
    		cerr << "Cannot allocate memory for new node.\n";
    		exit(EXIT_FAILURE);
    	}
    
    	nnode = rptr;
    
    	if (tnode == NULL)
    		tnode = nnode;
    	else
    	{
    		for (cnode = tnode; cnode->next != NULL; cnode = cnode->next)
    			 ;
    		cnode->next = nnode;
    	}
    }
    The problem is that when the code reaches the nnode = rptr; line, nnode ends up pointing to the address held in rptr. That results in me only being able to add one node, being as the address for the next pointer is the same as the last (if that makes sense).

    If I cout the address of nnode before the assignment takes place, it always has a different memory address (which is what I want). However, is there a way to just assign the data in rptr to nnode, without making nnode point to the address? Not sure if that makes sense, but basically, I've got the following definitions for the structures:

    Code:
    typedef struct ir_record {
    	char cust_code[CUSTCHECKDIGIT + 1];
    	char part_num[PARTCHECKDIGIT + 1];
    	char qty[5];
    } irRecord;
    
    typedef struct d_record {
    	char cust_code[CUSTCHECKDIGIT + 1];
    } dRecord;
    
    typedef struct c_record {
    	char cust_code[CUSTCHECKDIGIT + 1];
    	char cust_name[CUSTNAMESIZE + 1];
    	char cust_addr[CUSTADDRSIZE + 1];
    	float balance;
    	float credit;
    } cRecord;
    
    typedef union current_record {
    	irRecord ir;
    	dRecord d;
    	cRecord c;
    } mRecord;
    
    typedef struct main_record {
    	char rec_type;
    	mRecord main;
    	struct main_record *next;
    } Record;
    Seems complex, but it's to solve an assignment problem. So as I say, is there a way to assign the data in rptr, to nnode, without having to perform a switch on rec_type to find out what data I need to assign (doing it manually of course)?

    Hope this makes sense,

    John.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ... bit ambigous but let's see if can make anything out of it

    at first you write:


    else
    {
    for (cnode = tnode; cnode->next != NULL; cnode = cnode->next)
    ;
    cnode->next = nnode;
    }
    that ( semicolon is not really helping you, b/c it will loop and will never let you get to (cnode->next = nnode) instruction.... but that's besides the point....


    and to answer your question on assigning data of a node that (rptr) points to -- > sure there is...

    one way is that you can pass all the private members of a node explicityly to the function and then assign it one by one

    another way is (probably more efficient) ----> to pass a pointer to rptr as you are doing so and obviously (b/c nnode is a pointer to some node) you allocate a memory of the heap for a new node and assign that address to nnode
    and since you are posting in C++ forum I will assume your program will utilize the powers of C++ and I will recommend that inside your class you can OVERLOAD the assignment (=) operator to be able to assign all the private data member (one by one) to another nnode, what i'm trying to say is that when the compiler sees that 2 nodes of the same type are trying to be assigned (1 to the other) then it will be able to look up the ASSIGNMENT OPERATOR USER DEFINED IMPLEMENTATION and thus will do what you are trying to accomplish...


    also you stated:


    The problem is that when the code reaches the nnode = rptr; line, nnode ends up pointing to the address held in rptr.
    well, essentially that's what it supposed to do


    anyway, i'm kind of seeing where you are going with this, but it seems to me that you must back up a little bit and take few things step by step (one at a time) b/c you are getting yourself lost within your own logic...

    ps. and what is ( tnode) ..... ???

    hope that helps....

    if not, (like i said) post things one task at a time and we'll look it over then...

    ....................................
    matheo917

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Firstly, thanks for replying.

    Second:

    Code:
    for (cnode = tnode; cnode->next != NULL; cnode = cnode->next)
    ;
    All that's supposed to do is get to the end of the list. It loops until cnode->next isn't NULL, hence the cnode = cnode->next portion of the loop. That means the instruction is reached.

    I've used the same piece of code in an extension to the UnrealIRCd code (was just a test to add some stuff), but it works fine. However, I had a different approach to adding to the list then, which was by assigning the members one by one.

    As I've not learnt how to use classes or overload operators yet, I'll do as you suggested, and stick with assigning one by one for now. I'll re-write it once I've actually implemented some classes.

    Thanks for the reply mate, very much appreciated.

    John.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Oops, almost forgot. tnode is the top node, and cnode is the current node. Just my odd naming convention lol.

    John.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    The individual assignment worked just fine. Wasn't as bad as what I was thinking actually. Realised I'd defined a struct to hold the others, so I was able to just do this:

    Code:
    nnode->rec_type = rptr->rec_type;
    nnode->main = rptr->main;
    nnode->next = NULL;
    Thanks again though, all fixed now.

    John.

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    glad i was able to help

    keep up the good work

    .......................
    matheo917

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