help needed with linked lists. 
Author Message
 help needed with linked lists.

ok. please look through the following code and tell me where i'm going wrong.
i assume the problem lies in pointing to the new head of the list.

thanks in advance
Silvio
--

#include <stdio.h>

typedef struct Item_Node {
  int Data;

  Item_Node *Next;

Quote:
} Item_Ptr;

void Add_Item(Item_Ptr *Item_Head,int Data);

int main(void)
{
  Item_Ptr *Item=NULL;

  Add_Item(Item,23);

Quote:
}

void Add_Item(Item_Ptr *Item_Head,int Data)
{
  Item_Ptr *Item=new Item_Ptr;
  Item->Data=Data;
  Item->Next=Item_Head;
  Item_Head=Item;  
Quote:
}



Mon, 19 May 1997 17:38:13 GMT  
 help needed with linked lists.

    Silvio> #include <stdio.h>

    Silvio> typedef struct Item_Node {
    Silvio>   int Data;
    Silvio>   Item_Node *Next;
    Silvio> } Item_Ptr;

You want 'struct Item_Node *Next', for starters. Also, the name
'Item_Ptr' could be confusing, since it's not actually a pointer.

    Silvio> void Add_Item(Item_Ptr *Item_Head,int Data);

    Silvio> int main(void)
    Silvio> {
    Silvio>   Item_Ptr *Item=NULL;
    Silvio>   Add_Item(Item,23);
    Silvio> }

    Silvio> void Add_Item(Item_Ptr *Item_Head,int Data)
    Silvio> {
    Silvio>   Item_Ptr *Item=new Item_Ptr;

Umm... 'new' isn't C, it's C++ (which explains the missing 'struct'
above). But let's pretend that you used malloc().

    Silvio>   Item->Data=Data;
    Silvio>   Item->Next=Item_Head;
    Silvio>   Item_Head=Item;  
    Silvio> }

C pointers (and all other C objects except for arrays) are passed into
functions by value: a copy of the object is passed, not the actual
object. This means that when you call Add_Item(Item, 23) in main(), a
copy of main::Item is made and placed in Add_Item::Item_Head (by
foo::bar I mean variable bar in procedure foo). So when you assign
Item to Item_Head at the end of Add_Item(), you are altering the copy
of the pointer, not the original pointer.

There are a few ways around this. You could pass an Item_Ptr ** into
Add_Item, and change the final assignment to read *Item_Head =
Item. You would also need to change the call in main to
Add_Item(&Item, 23). A more typical solution would be to keep a dummy
head element, and pass that around, e.g.:

int main()
{
  Item_Ptr *Head = (Item_Ptr *)malloc(sizeof(Item_Ptr)); /* or new Item_ptr */
  Head->Next = NULL;
  Add_Item(Head, 23);

Quote:
}

void Add_Item(Item_Ptr *Item_Head, int Data)
{
  Item_Ptr *Item = (Item_Ptr *)malloc(sizeof(Item_Ptr)); /* or new Item_ptr */
  Item->Data = Data;
  Item->Next = Item_Head->Next;
  Item_Head->Next = Item;

Quote:
}

Even though Add_Item::Item_Head is a copy of main::Head, they hold the
same pointer. Thus, Add_Item::Item_Head->Next and main::Head->Next are
the same object, and when you assign to Item_Head->Next in Add_Item it
modifies Head->Next.

Hope this helps,

Jake

(warning: code is untested)



Tue, 20 May 1997 05:02:38 GMT  
 help needed with linked lists.

Quote:

>ok. please look through the following code and tell me where i'm going wrong.
>i assume the problem lies in pointing to the new head of the list.
>#include <stdio.h>
>typedef struct Item_Node {
>  int Data;
>  Item_Node *Next;

Does you compiler accept this? If yes, please talk to your compiler
vendor, if he/she claims to sell an ANSI C compiler.

You either have to add

  typedef struct Item_Node Item_Node;

before you use Item_Node in it's own definition, or you have to use

  typedef struct Item_Node {
    struct Item_Node *Next;
  } Item_Node;

Quote:
>} Item_Ptr;

The Name Item_Ptr for a node, not a pointer to a node, is very
misleading. It will confuse anybody but you, because it is very
natural to associate a pointer with the ending _Ptr.

Either do this:

  typedef struct Item_Node *Item_Ptr;
  struct Item_Node {
    int Data;
    Item_Ptr Next;
  };

or this:

  typedef struct Item_Node {
    int Data;
    struct Item_Node *Next;
  } Item_Node;
  typedef Item_Node *Item_Ptr;

I prefer the second version, but if you don't need a name for the
node type, the first one will do as well. (There a some dozends
of syntatctic variations of the same theme, so don't take the
either/or too literally).

Quote:
>void Add_Item(Item_Ptr *Item_Head,int Data);

This is where it starts to get real confusing. You don't pass a
pointer to a pointer to a node to Add_Item, all you pass is a
pointer to a node.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-2158




Tue, 20 May 1997 02:28:48 GMT  
 help needed with linked lists.

Quote:
> ok. please look through the following code and tell me where i'm going wrong.
> i assume the problem lies in pointing to the new head of the list.

    Yes, this is a classic example of pass by value instead of pass by
    reference. When you modify the list pointer in your Add_Item()
    function, you need to pass it the address of the list pointer, not
    just its value.

Quote:
> Silvio

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Tue, 20 May 1997 19:41:02 GMT  
 help needed with linked lists.

Quote:

>ok. please look through the following code and tell me where i'm going wrong.
>i assume the problem lies in pointing to the new head of the list.
>thanks in advance
>Silvio
>--
>#include <stdio.h>
>typedef struct Item_Node {
>  int Data;
>  Item_Node *Next;
>} Item_Ptr;

Judging from the name (Item_Ptr), this should probably be:
 } *Item_Ptr;

Quote:
>void Add_Item(Item_Ptr *Item_Head,int Data);
>int main(void)
>{
>  Item_Ptr *Item=NULL;
>  Add_Item(Item,23);

   Add_Item(&Item,23);      /* see comment below */

Quote:
>}
>void Add_Item(Item_Ptr *Item_Head,int Data)
>{
>  Item_Ptr *Item=new Item_Ptr;
>  Item->Data=Data;
>  Item->Next=Item_Head;
>  Item_Head=Item;  

These last two lines are the problem.  You change the parameter Item_Head,
but that doesn't affect the parameter passed in.  The last two lines should
be:
  Item->Next = *Item_Head;
  *Item_Head = Item;
Quote:
>}



Tue, 20 May 1997 03:42:44 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Help needed with linked list

2. Help needed with linked list

3. Help needed w/ Linked Lists

4. Help needed on link list

5. *Urgent* help needed with Linked lists and pointers to pointers!

6. Link List to Link List, HELP friends

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

8. Need help with doubly linked lists

9. help needed for loading link list

10. need help with linked lists

11. Need help with linked list & text files

12. Please I need some help -- traversing trough a doubly linked list--

 

 
Powered by phpBB® Forum Software