473,324 Members | 2,214 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,324 software developers and data experts.

Error in Linked List

hi
this is linked list program ,,,
this program display in reverse order...........
how to correct this error

#include <stdio.h>
struct list {
int value;
struct list *next;
};

main()
{
struct list *n1;
struct list *head =NULL;
int i;
head =n1;
for (i=0;i<10;i++)
{
n1=(struct list *)malloc(sizeof(struct list));
n1->value=i;
n1->next=head;
head=n1;
}
while( head != NULL )
{
printf("%d\n", head->value);
head = head->next;
}
}

Thank U
By
CNS

Nov 15 '05 #1
3 1278
chellappa wrote:
this is linked list program ,,,
this program display in reverse order...........
how to correct this error
add items at the end instead of the beginning. Add a tail pointer that
always points at the last item.


#include <stdio.h>
#include <stdlib.h> /* for malloc() */

struct list {
int value;
struct list *next;
};

main()
that's "int main (void)"

{
struct list *n1;
what is n1 pointing at?
struct list *head =NULL;
int i;
head =n1;
what is head pointing at?
for (i=0;i<10;i++)
{
n1=(struct list *)malloc(sizeof(struct list));
don't cast the return value of malloc()
n1->value=i;
n1->next=head;
head=n1;
}
while( head != NULL )
{
printf("%d\n", head->value);
head = head->next;
}
return 0;
}

--
Nick Keighley

Computers in the future may weigh no more than 1.5 tons
- Popular Science (1959)

Nov 15 '05 #2
Nick Keighley wrote:
chellappa wrote:
this is linked list program ,,,
this program display in reverse order...........
how to correct this error


add items at the end instead of the beginning. Add a tail pointer that
always points at the last item.


Or reverse the list (easy) when you've finished constructing it.

--
Chris "electric hedgehog" Dollin
Stross won one! Farah won one! Langford won TWO!
Nov 15 '05 #3
chellappa wrote:
hi
this is linked list program ,,,
this program display in reverse order...........
how to correct this error
*These* errors, including huge errors in program logic.
Try this for a starting point (OP's code at EOM)

#include <stdio.h>
#include <stdlib.h>
struct list
{
int value;
struct list *next;
};

int main(void)
{
struct list *n1, *last = 0;
struct list *head = 0;
int i;
for (i = 0; i < 10; i++, last = n1) {
n1 = malloc(sizeof *n1);
/* mha: add error handling here */
if (!head)
head = n1;
else
last->next = n1;
n1->value = i;
n1->next = 0;
}
for (n1 = head; n1; n1 = n1->next)
printf("%d\n", n1->value);
return 0;
}
[OP's code]

#include <stdio.h>
struct list {
int value;
struct list *next;
};

main()
{
struct list *n1;
struct list *head =NULL;
int i;
head =n1;
for (i=0;i<10;i++)
{
n1=(struct list *)malloc(sizeof(struct list));
n1->value=i;
n1->next=head;
head=n1;
}
while( head != NULL )
{
printf("%d\n", head->value);
head = head->next;
}
}

Thank U

^^^
Don't do this. It makes you look like a child. There are many things
done by non-native English writers that go unremarked, but SMS
abbreviations are not among them.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
0
by: crypto_solid via AccessMonster.com | last post by:
I have been using a SQL database with a VB5 frontend for about 5 years. Works well. Unfortunately I don't have access to the source code. I was tasked with implementing a "job entry" application...
3
by: picknicker187 | last post by:
hi, the program below is supposed to read data out of a txt file into an array and the second part of the data to a linked list. 8 is the number of words, the 10 terms below represent...
4
by: Stian Karlsen | last post by:
Hi. I'm getting an error in my program. It doesn't occur each time even if the same things happend in my program each time I run it. There will however be different values used for calculations in...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
1
by: Lpitt56 | last post by:
I am running MS Access 2007 and I want to update an Outlook Address book from my Access Database. I started out by importing the Outlook Address Book as a linked table and it linked fine. I then...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
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...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.