Re: Link lists..



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)
Mark Bluemel explained that already

#define A_H

typedef struct node nodestruct
wrong syntax

{
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 */
No, but so that it can be used in any module that #include's this file
without being defined there

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 */
This is done for you automagically

LL()/* computes the link list */
{
node *p, *prev; /*prev is previous node */

head = NULL;
head->next = NULL;
Wrong way round and without checking head first, still won't work as 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

{
p = (node *) malloc(sizeof(node));
Loose the cast, #include <stdlib.h>,use sizeof *p, check the result

p->next = NULL;

scanf("%d",&(p->data));
varadic function without ptototyp -> UB. Use #include <stdio.h>, better
don't use scanf anyway

if(head ==NULL)
head = p;
else
{
prev->next = p;
prev = p;
??? so now prev->next == NULL, as p->next == NULL !

}
}
} missing, probably due to poor indentation style


main()
int main(void)

{
struct node *p;
why struct here, it's typedef'd


LL();

p = head;

while(p!= NULL)
{
printf("%d\n",p->data);
varadic funtion without prototype -> UB

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


.



Relevant Pages

  • Re: Link lists..
    ... #ifndef A_H (can anyone please explain how ifndef works as well..i ... typedef struct node nodestruct ... extern node * head /*indicates head node of the list, ... otherwise you derefference a NULL pointer ...
    (comp.lang.c)
  • LinkedList Pointer (REPOST - diff version)
    ... struct node * next; ... No problem until you are dealing with a pointer variable. ... void Push(struct node** headRef, int newData); ... Given an int and a reference to the head pointer (i.e. a struct ...
    (comp.lang.c)
  • LinkedList Pointer (REPOST - diff version)
    ... struct node * next; ... No problem until you are dealing with a pointer variable. ... void Push(struct node** headRef, int newData); ... Given an int and a reference to the head pointer (i.e. a struct ...
    (comp.lang.c)
  • Re: branch prediction optimization
    ... If you had a loop like ... head = NULL; ... although the first time through the loop will suffer. ... struct node *next; ...
    (comp.unix.programmer)
  • Re: linked lists
    ... >> why do you have a pointer to a pointer to a node p?? ... > Because I need to modify the original pointer 'head'. ... struct node *next; ... struct node *newnode; ...
    (comp.lang.c)