From: hartman@ulogic.UUCP (Richard M. Hartman)
Newsgroups: comp.lang.c
Subject: Re: Making a linked list...
Date: 29 Sep 92 22:40:35 GMT
Article-I.D.: ulogic.315
Organization: negligable

In article <1992Sep24.091108.29805@dxcern.cern.ch> riipinen@dxcern.cern.ch (Petri Juhani Riipinen) writes:
>Hello there.
>
>This may be a trivial question for some, but as I usually have been
>programming with Pascal and now just started to do a C-project, I have
>a bit of a difficulty with this.
>
>The problem: To do a linked list with one function.
>
>My attempt so far (lots of things left out in this example):
>
>(here are lots if includes)
>
>typedef struct { int a_somedata;   /* I should make a list of these */
>		 int b_somedata;
>		 void *next;
>	       } ONE_NODE;

Item #1: put your next pointer at the top of the struct
Item #2: (covered by other answer) don't use void * for next pointer
so far:

typedef struct node_struct {
	struct node_struct *next;
	int a_somedata;
	int b_somedata;
	} ONE_NODE;

>void main()
>{ void *LINKED_LIST=NULL;   /* LINKED_LIST should contain the start addr */
>   int a,b;    /* Just dummy variables */
>
>  a=getsomedata();
>  b=getanotherdata();
>
>  /* Now, I should call one funcion, which would add the nodes to list */
>  /* I think, that I have to give the address of LINKED_LIST to the
>     add_node-functin, if I want to change the LINKED_LIST...? */
>  add_node(&LINKED_LIST,a,b);
>
>  /* I have also tried the following, which I think, is the correct way,
>     but it does not work...? */
>  add_node(LINKED_LIST,a,b);
>
>  /* After this I would just do repeated calls to the add_node routine
>     and always there would be another node added to the list. Note, that
>     the first time LINKED_LIST should contain NULL, and after the first
>     call LINKED_LIST should contain the address of the first node */
>
>}

Item #3: "dummy" head nodes make some linked list coding easier,
	e.g. 
		(option a):		ONE_NODE LINKED_LIST;
	but if you absolutely have to use the pointer, forget about
	void * (it will only get you into trouble).  Use:
		(option b):		ONE_NODE *pLINKED_LIST;

>void add_node(void *list,int a,int b)
>{ ONE_NODE *aa=list;

Item #4: now that we know what type your list is this function
should be either 
	add_node(ONE_NODE *pList, int a, int b)
or
	add_node(ONE_NODE **ppList, int a, int b)
depending upon whether you are going w/ option a or option b as 
outlined in item #3.

>
>  /* I would like to see, if the contents if list is null, should I do
>    if (*list==NULL) or if (list==NULL) ? */

Item #5:
option a:
	not necessary

option b:
	if (*list == NULL)

>)
>  /* Anyway, my code is now, like the following */
>  if (*list==NULL) { *list=(ONE_NODE *)malloc(sizeof(ONE_NODE);
>		     aa=*list;
>		   }

Item #5 continued:
	this would work for option b above
	for option a you don't need to check because we are
	assuming a dummy head-of-list node.  Oh, and we would
	have started w/ "aa=pList->next;" not "aa=pList;"

>		   /* If there is stuff in the list, then find the end */
>   else            {  while (aa!=NULL) aa=aa->next;
>		    /* Add one node to the end */
>		      aa->next=(ONE_NODE *)malloc(sizeof(ONE_NODE);
>		      aa=aa->next;
>		    }
>
>    /* So, now anyway the aa should be pointing to newly added node */
>    aa->a_somedata=a;
>    aa->b_somedata=b;
>    aa->next=NULL;
>}

Item #6:
	there is one problem w/ option a, and that is the assumption
	that when we declare "ONE_NODE LINKED_LIST" the next ptr 
	(LINKED_LIST.next) will be initialized to NULL.   It *should* 
	be properly initialized by the compiler, but to be certain you
	should do: "ONE_NODE LINKED_LIST = { NULL };", (which is why
	you wanted the next ptr up in the front of the structure;


Hope this helps.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Blasting, bursting, billowing forth with	|
the power of ten billion butterfly sneezes,	|	-Richard Hartman	
Man, with his flaming fire,			|	hartman@uLogic.COM
has conquered the wayword breezes.		|
Newsgroups: comp.lang.c