Re: Link lists..



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
.



Relevant Pages

  • Re: Static vs. global
    ... Within the scope of a function a static variable persists between function ... The function could return a pointer ... at from outside a module just impossible to see using "extern". ...
    (comp.arch.embedded)
  • Re: Specifier extern
    ... extern int b; // my line 6 ... more of an exercise for myself to see if I have a good grasp of linkage ... The outer one, at file scope, ... The "extern" keyword is particularly squirrely, ...
    (comp.lang.c)
  • Re: why still use C?
    ... Using extern "C" is not sufficient to cover those cases. ... A function or function pointer ... library required them to send the compiler output through a "warning ... >> times the size of the thing we really want, cast the resultant pointer to ...
    (comp.lang.c)
  • Re: Converting enums to pointers
    ... > You declare EMPTY to be a null pointer here. ... though it might be nice to use the extern keyword. ...
    (comp.lang.cpp)
  • Re: extern const char * vs. extern const char []http://tinyurl.com/47e3k
    ... >creates a pointer to a 'C' string literal. ... >array and it has a location, ... many platforms) since your const char* is a different type from the ... extern variables in the other TU - you need const charas the type ...
    (comp.lang.cpp)