Insert in a linked list 
Author Message
 Insert in a linked list

Hello,

I am trying to insert a node into a linked list. I am using the same
logic I learn when
working with Pascal. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct pc {
   char name[15];
   int bytes;
   struct pc *next;

Quote:
} CLIENT;

void Insert (CLIENT *, CLIENT *);

void main()
{
   CLIENT *client, *temp;

   /* client points to empty list */
   client = NULL;

   /* allocate memory for new node to insert */
   temp = (CLIENT *) malloc (sizeof (CLIENT));

   /* assign struc members */
   strcpy (temp->name, "Name");
   temp->bytes = 123;

   /* insert temp into empty list */
   Insert (client, temp);

   printf ("%s\n", client->name);

Quote:
}

/* inserts temp into list client */
void Insert (CLIENT *head, CLIENT *temp)
{
   CLIENT *prev, *cur;

   prev = NULL;
   cur = head;
   while (cur != NULL &&
          (cur->bytes >= temp->bytes)) {
      prev = cur;
      cur = cur->next;
   }

   if (cur == head) {
      /* insert at beginning of list */
      temp->next = head;
      head = temp;
   }
   else {
      /* insert in middle or end of list */
      prev->next = temp;
      temp->next = cur;
   }
   printf ("%s\n", head->name);

Quote:
}

The printf in the Insert function correctly prints the name, but the
printf in the
main function prints null. Any suggestions??

Thanks,
David



Sat, 08 Jan 2000 03:00:00 GMT  
 Insert in a linked list

The problem is here:

   if (cur == head) {
      /* insert at beginning of list */
      temp->next = head;        
      head = temp;      // <<-- no good
   }
You change head in the belief that you are changing client back in main.
But client  was passed by value so you are changing a copy of client, not
client.  The solution is easy in C++, change the function signature to:
  void Insert(CLIENT*&, CLIENT*)
 This passes CLIENT* by reference and so client ( the real head of the
list) gets changed.
 I tried this on a C++ compiler and it worked. I'll let you figure out the
C solution, it has to do with pointers to pointers or some such .....  .  
 Provides a good incentive to go to C++, but I suppose this is an
inopportune time to switch.

------------------------------------------


Quote:
> I am trying to insert a node into a linked list. I am using the same
> logic I learn when
> working with Pascal. Here is the code:

>  [big SNIP]

> The printf in the Insert function correctly prints the name, but the
> printf in the
> main function prints null. Any suggestions??



Sun, 09 Jan 2000 03:00:00 GMT  
 Insert in a linked list

<snip>

Quote:

>void main()

You might want to check section 11.12 of the C FAQ
(main() returns an int).
URL: http://www.eskimo.com/~scs/C-faq/top.html

Quote:
>   CLIENT *client, *temp;

>   /* client points to empty list */
>   client = NULL;

You set client to NULL ...

Quote:

>   /* allocate memory for new node to insert */
>   temp = (CLIENT *) malloc (sizeof (CLIENT));

See section 7.7 of the C FAQ, you do not need to cast
the return value of malloc(), it might only cause problems.

Quote:

>   /* assign struc members */
>   strcpy (temp->name, "Name");
>   temp->bytes = 123;

>   /* insert temp into empty list */
>   Insert (client, temp);

>   printf ("%s\n", client->name);

The variable client is still NULL. You didn't make
any assignments to client since client = NULL;

Maybe you thought the call to Insert() would change
the variable client, but C uses pass by value.

(So did Pascal right? Didn't you need the var keyword
to make changes to a parameter that was passed in?)

You could have Insert() return a value and assign the return
value to client, or you could pass a pointer to client when you
call Insert().

i.e.

        Insert( &client, temp );
....

void Insert (CLIENT **head, CLIENT *temp)
{
....
        *head = new_location_to_point_at;

Quote:
}

If you need some more help to understand the problem / solution,
just ask. Hope this helps.
--
Scott Allen            



Sun, 09 Jan 2000 03:00:00 GMT  
 Insert in a linked list

Quote:

> Hello,

> I am trying to insert a node into a linked list. I am using the same
> logic I learn when
> working with Pascal. Here is the code:

[beginning of test code snipped]

Quote:
> /* inserts temp into list client */
> void Insert (CLIENT *head, CLIENT *temp)
> {
>    CLIENT *prev, *cur;

>    prev = NULL;
>    cur = head;
>    while (cur != NULL &&
>           (cur->bytes >= temp->bytes)) {
>       prev = cur;
>       cur = cur->next;
>    }

[rest snipped]

Quote:
> The printf in the Insert function correctly prints the name, but the
> printf in the
> main function prints null. Any suggestions??

Hi David Serio,

Your "Insert()" function works fine, if it does not have to change the
head element ! You pass the pointer to the head element *per value*
which means that you can change what it points to (ie. a list) but
not it's contents. You can keep the "Insert()" function if you create
your list with one empty dummy head element (maybe write a function
"CreateList()" for this).

The alternative is to change the prototype to the "Insert()" function
like this, to allow changing the "head" pointer itself:
  void Insert ( CLIENT **head, CLIENT *temp )

BTW, I saw that you are using an illegal declaration for "main". The
only correct return type for "main" is "int":
  int main( void )
  int main( int argc, char *argv[] )

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 09 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. link list of a linked list (chained links?)

2. Problem of insert and sort of Linked list

3. Insert Function for an ordered linked list

4. Incompatible NULL Assignments || Linked List inside Linked List

5. Clearing Memory || Linked List INSIDE of a Linked List

6. Freeing a Linked List inside of a Linked List

7. Linked List of Linked Lists

8. Define a linked list of a linked list

9. Link List to Link List, HELP friends

10. linked lists (searching/moving a link)

11. inserting a hyper link in the help file...

12. Insert string into list position

 

 
Powered by phpBB® Forum Software