Re: Link lists..
- From: johnnash <johnnash86@xxxxxxxxx>
- Date: Fri, 29 Feb 2008 04:17:14 -0800 (PST)
On Feb 29, 4:58 pm, "Joachim Schmitz" <nospam.j...@xxxxxxxxxxxxxxxxxx>
wrote:
johnnash wrote:
i'm declaring a data structure for link list of integers in A.h
#ifndef A_H (can anyone please explain how ifndef works as well..i
just seem to see it in almost every program)
#define A_H
typedef struct node nodestruct
{
int data;
struct node *next;
}node;
extern node * head /*indicates head node of the list , declaring as
extern so that it can be used in A.c */
Now i want to use this link list so..A.c
#include "A.h"
node * head; /*definition, is this sufficient for link list ?, can i
initialize head to NULL here itself */
LL()/* computes the link list */
{
node *p, *prev; /*prev is previous node */
head = NULL;
head->next = NULL;
Swap these 2 lines, otherwise you derefference a NULL pointer
for(i=0;i<10;i++) /*Creating list of 10 elements */
{
p = (node *) malloc(sizeof(node));
lose the cast. Instead #include <stdlib.h>
p->next = NULL;
check p first. malloc() can fail, if so you derefferent a NULL pointer here
scanf("%d",&(p->data));
Avoid scanf. At least consider what happens to the <return>
if(head ==NULL)
head = p;
else
{
prev->next = p;
prev = p;
}
}
main()
{
struct node *p;
LL();
p = head;
while(p!= NULL)
{
printf("%d\n",p->data);
p = p->next;
}
}
yeah I admit head->next = NULL was actually mistyping.
why should i return something here ?
also can you please tell me the scope of this link list ? If it is
declared as extern does it mean that its scope is the entire program
or the values are immediately destroyed after ll() ends
.
- References:
- Link lists..
- From: johnnash
- Re: Link lists..
- From: Joachim Schmitz
- Link lists..
- Prev by Date: Re: Link lists..
- Next by Date: Re: Is there stack associated when a executing an inline function?
- Previous by thread: Re: Link lists..
- Next by thread: Re: Link lists..
- Index(es):
Relevant Pages
|