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

what's wrong with my code of linked list?

I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

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

struct element {
int index;
struct element *next;
};

void free_list(struct element * first){
struct element *current,*next;
current=first;
next=current->next;
while(next !=NULL){
printf("free the %d the element\n",current->index);
free(current);
current=next;
next=current->next;
}
free(current);
}

void print_list(struct element *first){
struct element *current;
current=first;
while(current !=NULL){
printf("the %d th element visited\n",current->index);
current=current->next;
}
}
struct element *create_list(void){
struct element *temp,*leading,*previous;
int i;
leading=malloc(sizeof(struct element));
leading->index=0;
previous=leading;

for(i=1;i<1000;i++){
temp=malloc(sizeof(struct element));
temp->next==NULL;
temp->index=i;
previous->next=temp;
previous=temp;
}
return leading;
}

int main(){
int i;
struct element *dummy;
for(i=0;i<2;i++){
dummy=create_list();
print_list(dummy);
free_list(dummy);
}
return 0;
}

************************************************** ****
What's wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().

Thanks

Mar 30 '06 #1
12 1856
un************@hotmail.com wrote...
temp->next==NULL;


Mar 30 '06 #2
"questions?" <un************@hotmail.com> writes:
I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

#############################################
# include <stdio.h>
# include <stdlib.h> [snip]
************************************************** ****
What's wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().


You're going to have to give us a hint. What is the program supposed
to do, and what does it actually do?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 30 '06 #3
"questions?" <un************@hotmail.com> wrote:
I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

#############################################
[ Snip linked list code ]
************************************************** ****
What's wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().


What's your problem with it? It runs fine for me, and at first glance I
can't find anything wrong with it. The only nit I would pick is that
what you call "leading" is conventionally called the head node, but
that's not an actual error, it's a style issue.

Richard
Mar 30 '06 #4
Simple Simon <ss****@domain.invalid> wrote:
un************@hotmail.com wrote...
temp->next==NULL;


Argh! That's what's wrong with first glances: they miss the obvious...

Richard
Mar 30 '06 #5
On Wed, 29 Mar 2006 23:28:26 -0800, questions? wrote:

A minor glitch
temp->next==NULL;

temp->next = NULL;

Try next time to add this option to your compile statement: -Wall
Then you would have received this warning:
test.c: In function 'create_list':
test.c:41: warning: statement with no effect

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Mar 30 '06 #6
questions? wrote:
void free_list(struct element * first){
struct element *current,*next;
current=first;
next=current->next;

This is illegal if the list is empty (next == NULL).
Mar 30 '06 #7
Others already picked out various nits.

In article <11**********************@v46g2000cwv.googlegroups .com>,
questions? <un************@hotmail.com> wrote:
# include <stdio.h>
# include <stdlib.h>

struct element {
int index;
struct element *next;
};
I usually put the forward link first, not that it matters much in
this case. Also, I like to use the macros from the BSD <sys/queue.h>
headers, but that might be overkill in this case.
void free_list(struct element * first){
struct element *current,*next;
current=first;
next=current->next;
while(next !=NULL){
printf("free the %d the element\n",current->index);
free(current);
current=next;
next=current->next;
}
free(current);
}
To avoid redundant code and problems with freeing an empty list
and so on, I prefer to write:

void free_list(struct element *head) {
struct element *p, *next;

for (p = head; p != NULL; p = next) {
next = p->next;
free(p);
}
}
void print_list(struct element *first){
struct element *current;
current=first;
while(current !=NULL){
printf("the %d th element visited\n",current->index);
current=current->next;
}
}
Again, I would use a "for" loop; this time the match is so good
that we do not even need a separate "next" variable:

for (p = head; p != NULL; p = p->next)
printf("...\n", p->...);
struct element *create_list(void){

[contents snipped]

Here is where the BSD macros become particularly handy.

In general, I find that the list elements themselves might be
created with a "factory" (to steal a term more often applied to
OO-ish languages like C++). If the list is ordered in some
way, and/or if list-elements might be on multiple lists --
"every element in the entire system" but also "parent/child"
relationships with sibling and grandchild nodes and so forth,
and perhaps hash lists and timer queues and on and on -- I
can use the BSD macros to embed multiple "list" or "queue"
points inside each element.

Then, I can have multiple queue heads of various sorts, and
use TAILQ_APPEND, CIRCLEQ_INSERT_AFTER, and so on to do the
list manipulation.

Each type of queue has particular features and costs, as summarized
in the queue(3) manual (q.v.; use google).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 30 '06 #8
Simple Simon wrote:
un************@hotmail.com wrote...

temp->next==NULL;


One more item to add to the "Case Against C" list. ;-)
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Mar 30 '06 #9
In article <gm*******************@newsb.telia.net>,
August Karlstrom <fu********@comhem.se> wrote:
temp->next==NULL;
One more item to add to the "Case Against C" list. ;-)


A reasonable compiler will warn you about this.

-- Richard
Mar 30 '06 #10
Richard Tobin wrote:
In article <gm*******************@newsb.telia.net>,
August Karlstrom <fu********@comhem.se> wrote:

temp->next==NULL;


One more item to add to the "Case Against C" list. ;-)

A reasonable compiler will warn you about this.


Yes, you're right. It's unfortunate however that "no warnings" are
usually the default setting.
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Mar 30 '06 #11
August Karlstrom <fu********@comhem.se> writes:
Richard Tobin wrote:
A reasonable compiler will warn you about this.

Yes, you're right. It's unfortunate however that "no warnings" are
usually the default setting.


A reasonable programmer quickly learns to enable warnings :-)
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Mar 30 '06 #12

Ben Pfaff wrote:
August Karlstrom <fu********@comhem.se> writes:
Richard Tobin wrote:
A reasonable compiler will warn you about this.

Yes, you're right. It's unfortunate however that "no warnings" are
usually the default setting.


A reasonable programmer quickly learns to enable warnings :-)
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup


Thanks you all for a lot of good discussions.
I made an error in that line of ==.
I was trying to mimic what I did in my bigger program by giving a
simple example.
The error actually is not happening in the construct/free list process.

I found the package valgrind really helpful for finding errors.

Mar 31 '06 #13

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

Similar topics

3
by: sugaray | last post by:
hi, I wrote a simple program which merge two single linked lists into one for practice, but it always freezes after the creation of the first list, hope someone might help me out with this. thanx...
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...
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
5
by: Daniel | last post by:
I need to reverse the doubly linked list with dummy node. I think the solution is to exchange each node pointers' next and previous address. But what's wrong in my function? Thanks void...
2
by: Aris | last post by:
Hello! Im trying to implement a queue using a linked list. I've made that code and I expected my Degueue() function to return the value of the key of the node I constructed. But It does not....
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
5
by: Joey | last post by:
I am attempting to hook the onChange event for a dropdown list with javascript so that I can do some stuff and then initiate a postback with my code. I have not yet learned about how to do...
11
pbmods
by: pbmods | last post by:
A somewhat obscure hack has emerged recently that is an offshoot of the now-infamous XSS. It is known as Cross-Site Request Forgery, or XSRF for short. XSRF is a form of temporary identity theft...
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.