Re: Link lists..
- From: "Joachim Schmitz" <nospam.jojo@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 15:23:06 +0100
johnnash wrote:
i'm declaring a data structure for link list of integers in A.hMark Bluemel explained that already
#ifndef A_H (can anyone please explain how ifndef works as well..i
just seem to see it in almost every program)
#define A_Hwrong syntax
typedef struct node nodestruct
{No, but so that it can be used in any module that #include's this file
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 */
without being defined there
Now i want to use this link list so..A.cThis is done for you automagically
#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 */Wrong way round and without checking head first, still won't work as NULL
{
node *p, *prev; /*prev is previous node */
head = NULL;
head->next = NULL;
isn't knwon here, due to lack of #include the relevant file
for(i=0;i<10;i++) /*Creating list of 10 elements */i is indefined
{Loose the cast, #include <stdlib.h>,use sizeof *p, check the result
p = (node *) malloc(sizeof(node));
p->next = NULL;varadic function without ptototyp -> UB. Use #include <stdio.h>, better
scanf("%d",&(p->data));
don't use scanf anyway
if(head ==NULL)??? so now prev->next == NULL, as p->next == NULL !
head = p;
else
{
prev->next = p;
prev = p;
}} missing, probably due to poor indentation style
}
int main(void)
main()
{why struct here, it's typedef'd
struct node *p;
varadic funtion without prototype -> UB
LL();
p = head;
while(p!= NULL)
{
printf("%d\n",p->data);
p = p->next;free whatever you malloc'd when you're done with it
}return 0;
}Please post something that at least compiles. Idealy without compiler
warnings even if warning level is set at it's highest.
Bye, Jojo
.
- References:
- Link lists..
- From: johnnash
- Link lists..
- Prev by Date: Re: Variable-sized lines of text in linked list
- Next by Date: Re: Is there stack associated when a executing an inline function?
- Previous by thread: Re: Link lists..
- Next by thread: dual core and c
- Index(es):
Relevant Pages
|