ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
i've been playing around with linked lists, and came up with this for exercise...
it works, but i dont know if its all done correctly, are there any memory leaks or any other kind of errors. maybe you could check it out and tell me if its ok?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct item
{
int foo;
struct item *next;
};
typedef struct item item;
/* fill the list */
item *current, *bar;
bar = NULL;
int baz;
for(baz = 1; baz <= 10; baz++)
{
current = (item *) malloc(sizeof(item));
current->foo = baz;
current->next = bar;
bar = current;
}
/* print list */
current = bar;
while(current)
{
printf("%d\n", current->foo);
current = current->next;
}
/* clean up */
current = bar;
item *temporary;
while(current)
{
temporary = current->next;
free(current);
current = temporary;
puts("Cleaned one.");
}
return 0;
}
The code looks fairly ok, but I want to add some points.
Writing this is a good idea only if you're just learning, but for all practical purposes you should use an existing library which provides these functionalities. Particularly in C++ you should check out STL. It has all the classes for linked lists, stacks, queues and other data structures like dictionaries etc. The advantage of using existing standard libraries is that the code is practically guaranteed to be error free and memory leaks would be very rare.
Second thing, use functions (or classes if you're using C++) to do things like adding nodes, removing nodes and freeing a list and don't put everything in main ().
Last edited by vharishankar; 08-04-2006 at 06:13 AM.
It works Natasl, but personally I do not like it and first thought it would not work until I realised its a bottom up linked list. ie the first node that is entered is actually the last in the list.
I would use a linked list with a head node, this gives you the entry point into the list, let me know if you would like an example.
edit:
Maybe I should say why I don't like it.
In my opinion a list should have the property of inserting/deleting a node at a given position, I can't see how you would accomplish this with your list.
CFL is a C library of useful functions that simplify systems software development on System V UNIX. The library includes routines for memory management, string parsing, filesystem traversal, subprocess execution, I/O, as well as implementations of common data structures such as linked lists, hash tables, stacks, and queues. The library also includes a high-level interface to Berkeley sockets, and an implementation of a scheduler which has functionality very similar to that of the cron daemon.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.