Re: Link lists..
- From: "Joachim Schmitz" <nospam.jojo@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 12:58:09 +0100
johnnash wrote:
i'm declaring a data structure for link list of integers in A.hSwap these 2 lines, otherwise you derefference a NULL pointer
#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;
for(i=0;i<10;i++) /*Creating list of 10 elements */lose the cast. Instead #include <stdlib.h>
{
p = (node *) malloc(sizeof(node));
p->next = NULL;check p first. malloc() can fail, if so you derefferent a NULL pointer here
Avoid scanf. At least consider what happens to the <return>
scanf("%d",&(p->data));
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;
}
}
.
- Follow-Ups:
- Re: Link lists..
- From: johnnash
- Re: Link lists..
- References:
- Link lists..
- From: johnnash
- Link lists..
- Prev by Date: Re: Link lists..
- Next by Date: Re: Link lists..
- Previous by thread: Re: Link lists..
- Next by thread: Re: Link lists..
- Index(es):
Relevant Pages
|