Re: Linked list, no out put,help



In article <1151666370.623666.38780@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
asm_fool@xxxxxxxxxxx wrote:

dear group,

/*Linked list in C*/


<delurk>

Hi;
I can at least point out the most obvious problems with this program.
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}a;
Here you declare a to be an object of type struct node at file scope so
that it is visible to all functions in this file.


void init(int data)
{
struct node *p;
p = malloc(sizeof *p);
Here you allocate enough memory to store an object of type struct node,
p points to that memory (assuming malloc has succeeded, of course).

if(!p)
{
printf("mem error");
exit(EXIT_FAILURE);
}

a.data = data;
a.next = NULL;
p = &a;
But here, you have assigned the address of a to p; at this point you no
longer have access to the memory that was allocated above. This is a
memory leak, you lose one such block of memory each time this function
is called.

}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);
}
You are not creating a linked list here, you are calling the function
init 5 times, modifying the same file scope variable, a, each time. At
the end of this loop a.data == 5, and a.next is NULL.

while(a.next != NULL)
And since a.next == NULL, this loop never executes, that's why you get
no output.

{
for(j=0;j<6;j++)
printf("value = %d\n",a.data);

}
return 0;
}
For a linked list, you need to keep track of the head of the list,
personally, I like to use a pointer to the first node (struct node
**headptr), then for each node, then declare the init function as
returning a pointer to node, i.e,
struct node *init (struct node **head,/*other init parameters*/)
{
struct node *current = *head;
struct node *ptr = malloc (sizeof *ptr);
/*Check that malloc succeeded, walk the list until current -> next ==
NULL, initialize the new node*/
return ptr;
}

Another possibility would be to keep a pointer to the last node in the
list, and pass a pointer to that to the init function, updating it to
reflect the new tail if you don't want the overhead of walking the list
each time a new node is created.

HTH
</delurk>

This is not the first time I am implementing a linked list.But every
time I
try to do that I get no out put. The above is the same situation. The
above
don't print any out put. Can any one tell me why.

[OT]
I can not configure news reader in my company. Same with the mail
reader. Though I use win98 the pop and nntp server simply functioning.
So I go for the ugly google.
[/OT]
.



Relevant Pages

  • 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: Stack with file access?.
    ... >> first record from the file or else an infinite loop. ... > of passing it a double pointer... ... int stackName; ... struct node * nextPtr; ...
    (comp.lang.c)
  • Re: Pointers
    ... Explicit pointer types serve the following purposes in C: ... void swap(int a, int b) ... The dynamic memory allocation functions, calloc, etc.) ... struct node *left; ...
    (comp.lang.c)
  • creating linked list
    ... -1 to exit. ... int item; ... // returns a pointer to a linked list containing those integers, ...
    (comp.lang.cpp)