J
johnnash
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;
for(i=0;i<10;i++) /*Creating list of 10 elements */
{
p = (node *) malloc(sizeof(node));
p->next = NULL;
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;
}
}
#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 */
{
p = (node *) malloc(sizeof(node));
p->next = NULL;
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;
}
}