473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Linked list, no out put,help

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
8 1475
* 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
<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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
2
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.