Linked list, no out put,help

A

asm_fool

dear group,

/*Linked list in C*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}a;

void init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if(!p)
{
printf("mem error");
exit(EXIT_FAILURE);
}

a.data = data;
a.next = NULL;
p = &a;
}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);
}
while(a.next != NULL)
{
for(j=0;j<6;j++)
printf("value = %d\n",a.data);

}
return 0;
}

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]
 
A

Alf P. Steinbach

* (e-mail address removed):
dear group,

/*Linked list in C*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}a;

void init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if(!p)
{
printf("mem error");
exit(EXIT_FAILURE);
}

a.data = data;
a.next = NULL;
p = &a;
}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);
}
while(a.next != NULL)
{
for(j=0;j<6;j++)
printf("value = %d\n",a.data);

}
return 0;
}

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.

In your 'init' function you set 'a.next = NULL'. The continuation
condition for your output loop is 'a.next != NULL'. That condition
evaluates to 0 the first time, and the loop body is never executed.

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

Do you actually work in a company? This is not professional code. It's
student code.
 
O

osmium

/*Linked list in C*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}a;

void init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if(!p)
{
printf("mem error");
exit(EXIT_FAILURE);
}

a.data = data;
a.next = NULL;
p = &a;
}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);

Where is the pointer to the first element in the linked list? Init
apparently means what I would call add_node. But what is init supposed to
add it to? You give no help to iniit in figuring out what to do with this
new datum.

Note that the argument you pass in not initialized and thus contains
garbage.

You use the word data to have two different meanings. Use different words
for different things. The compiler can figure out what you *said* but not
what you *mean*.
}
while(a.next != NULL)
{
for(j=0;j<6;j++)
printf("value = %d\n",a.data);

}
return 0;
}

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]
 
P

pete

dear group,

/*Linked list in C*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}a;

void init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if(!p)
{
printf("mem error");
exit(EXIT_FAILURE);
}

a.data = data;
a.next = NULL;
p = &a;
}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);
}
while(a.next != NULL)
{
for(j=0;j<6;j++)
printf("value = %d\n",a.data);

}
return 0;
}

This is not the first time I am implementing a linked list.

First time in C?

/*Linked list in C*/

#include<stdio.h>
#include<stdlib.h>

struct node {
struct node *next;
int data;
};

struct node *init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if (p == NULL) {
puts("mem error");
exit(EXIT_FAILURE);
}
p -> next = NULL;
p -> data = data;
return p;
}

int main(void)
{
unsigned int i, j;
struct node *head, *tail, *ptr;

puts("Enter an integer.");
scanf("%d", &i);
tail = head = init(i);
for (j = 1; j < 6; j++) {
puts("Enter another integer.");
scanf("%d", &i);
tail -> next = init(i);
tail = tail -> next;
}
for (ptr = head; ptr != NULL; ptr = ptr -> next) {
printf("value = %d\n", ptr -> data);
}
return 0;
}
 
P

pete

pete said:
First time in C?

/*Linked list in C*/

#include<stdio.h>
#include<stdlib.h>

struct node {
struct node *next;
int data;
};

struct node *init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if (p == NULL) {
puts("mem error");
exit(EXIT_FAILURE);
}
p -> next = NULL;
p -> data = data;
return p;
}

void list_free(struct node *list)
{
struct node *next;

while (list != NULL) {
next = list -> next;
free(list);
list = next;
}
}
int main(void)
{
unsigned int i, j;
struct node *head, *tail, *ptr;

puts("Enter an integer.");
scanf("%d", &i);
tail = head = init(i);
for (j = 1; j < 6; j++) {
puts("Enter another integer.");
scanf("%d", &i);
tail -> next = init(i);
tail = tail -> next;
}
for (ptr = head; ptr != NULL; ptr = ptr -> next) {
printf("value = %d\n", ptr -> data);
}

list_free(head);
puts("The list has been freed.");
 
A

asm_fool

pete said:
First time in C?

/*Linked list in C*/

#include<stdio.h>
#include<stdlib.h>

struct node {
struct node *next;
int data;
};

struct node *init(int data)
{
struct node *p;
p = malloc(sizeof *p);
if (p == NULL) {
puts("mem error");
exit(EXIT_FAILURE);
}
p -> next = NULL;
p -> data = data;
return p;
}

int main(void)
{
unsigned int i, j;
struct node *head, *tail, *ptr;

puts("Enter an integer.");
scanf("%d", &i);
tail = head = init(i);
for (j = 1; j < 6; j++) {
puts("Enter another integer.");
scanf("%d", &i);
tail -> next = init(i);
tail = tail -> next;
}
for (ptr = head; ptr != NULL; ptr = ptr -> next) {
printf("value = %d\n", ptr -> data);
}
return 0;
}


Thanks a lot for your code. But I did not went through the code since I
want to
do this by myself. Pls don't give out any code.Just give a suggestion.
 
P

pete

With (p = &a), the value returned from malloc is lost forever.

'a' is a single structure,
unaffiliated with any other structure.
Even if 'a' was the first structure in a list,
there's nothing in this loop
that would change the value of a.next
Thanks a lot for your code.
But I did not went through the code

I went through your code.
since I
want to do this by myself.
Pls don't give out any code.Just give a suggestion.

Your code shows no comprehension on your part
of what the code is supposed to be doing.

I intended the code that I posted
as a simple working example of how to implement a linked list.

You could do much worse
than to try to understand the working code.

It's good practice to free a list when you're done with it.
 
D

D. Power

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
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]
 
G

goose

(e-mail address removed) wrote:

Thanks a lot for your code. But I did not went through the code since I
want to
do this by myself. Pls don't give out any code.Just give a suggestion.

Fussy, aren't you? From the look of your code, I'd suggest you at
least try to understand the code Pete posted. There are way too many
things wrong in your code to simply "fix one problem".

Maybe get a nice book, see http://accu.org/index.php/book_reviews
for something well recommended.

goose,
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top