By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Linked list, no out put,help

P: n/a
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]

Jun 30 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* as******@yahoo.co.uk:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 30 '06 #2

P: n/a
<as******@yahoo.co.uk> wrote:
/*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]

Jun 30 '06 #3

P: n/a
as******@yahoo.co.uk wrote:

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;
}
--
pete
Jun 30 '06 #4

P: n/a
pete wrote:

as******@yahoo.co.uk wrote:

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;
}


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.");
return 0;
}


--
pete
Jun 30 '06 #5

P: n/a

pete wrote:
as******@yahoo.co.uk wrote:

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;
}
--
pete

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.

Jun 30 '06 #6

P: n/a
as******@yahoo.co.uk wrote:

pete wrote:
as******@yahoo.co.uk wrote:

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;
With (p = &a), the value returned from malloc is lost forever.
}

int main(void)
{
unsigned int i,j;
for(j=0;j<6;j++)
{
scanf("%d",&i);
init(i);
}
while(a.next != NULL)
'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
{
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?

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.

--
pete
Jun 30 '06 #7

P: n/a
In article <11*********************@75g2000cwc.googlegroups.c om>,
as******@yahoo.co.uk 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]

Jun 30 '06 #8

P: n/a

as******@yahoo.co.uk wrote:

<snipped>
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,

Jun 30 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.