Link lists..


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;

}

}
 
Ad

Advertisements

M

Mark Bluemel

johnnash said:
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)

Doesn't your text book or tutorial explain this?

If they don't help, simply Googling for "ifndef" gave me loads of
hits...

The pattern

#ifndef something
#define something
....
....
#endif

is a common technique for handling multiple inclusions of headers.

[snip]
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;

Bang! you've just tried to dereference a NULL Pointer...

[snip]

What was your actual question?
 
J

Joachim Schmitz

johnnash said:
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 said:
p->next = NULL;
check p first. malloc() can fail, if so you derefferent a NULL pointer here
 
J

johnnash

Swap these 2 lines, otherwise you derefference a NULL pointer




check p first. malloc() can fail, if so you derefferent a NULL pointer here


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
 
J

Joachim Schmitz

johnnash said:
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 said:
p->next = NULL;

scanf("%d",&(p->data));
better said:
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
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
 
Ad

Advertisements

R

Robbie Hatley

johnnash said:
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;

}

}


Ewww.

Chock full o' errors.

A "linked list" is a kind of a thing, no? So start by defining
the kind of thing you want. But since your list will consist of
nodes, you need to define the concept of "node" first:

typedef struct my_node_tag
{
struct my_node_tag *pNext;
/*struct my_node_tag *pPrev; (optional) */
int data;
} my_node_t;

The list will be a chain of these nodes, with a head and a tail:

typedef struct my_list_tag
{
my_node_t *pHead;
my_node_t *pTail;
} my_list_t;

Now define a way to paste a node to the end of the list:

int my_list_push (my_list *pList, my_node_t *pNode)
{
// Bail out if either input pointer is NULL:
if (!pList || !pNode)
{
return 666;
}

// If list is empty, new node becomes new head and tail:
if (!pList->pHead)
{
pNode->pNext = NULL; /* new node doesn't have next yet */
pList->pHead = pNode; /* new node becomes new head */
pList->pTail = pNode; /* new node becomes new tail */
}
// Otherwise, stick it on the end:
else
{
pNode->pNext = NULL; /* new node doesn't have next yet */
pList->pTail->pNext = pNode; /* old tail's next -> new node */
pList->pTail = pNode; /* new node becomes new tail */
}
}

You can test this out in main:

int main(void)
{
my_list_t Argle;
my_node_t *pNode;

pNode = malloc(sizeof(my_node_t));
pNode->data = 42;
my_list_push(Argle, pNode);

pNode = malloc(sizeof(my_node_t));
pNode->data = 17;
my_list_push(Argle, pNode);

pNode = malloc(sizeof(my_node_t));
pNode->data = 98;
my_list_push(Argle, pNode);

for ( pNode = Argle.pHead ; pNode ; pNode = pNode->pNext )
{
printf("%d\n", pNode->data);
}

return 0;
}

Should print:
42
17
98

(Caveat: All completely untested, off the top of my head. Probably
has bugs. You'll have to debug it yourself, I don't have time right
now, gotta run. Hope this helps, though.)
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top