LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-04-2006, 06:01 AM   #1
Natasl
LQ Newbie
 
Registered: May 2006
Posts: 15

Rep: Reputation: 0
linked lists


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;
}
 
Old 08-04-2006, 06:09 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
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.
 
Old 08-04-2006, 06:57 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
        for(baz = 1; baz <= 10; baz++)
        {
                current = (item *) malloc(sizeof(item));
                current->foo = baz;
                current->next = bar;
                bar = current;
        }
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.

Last edited by dmail; 08-04-2006 at 07:12 AM.
 
Old 08-04-2006, 08:29 AM   #4
Natasl
LQ Newbie
 
Registered: May 2006
Posts: 15

Original Poster
Rep: Reputation: 0
i wasn't going for any usability or anything... it's just an exercise, my only goal was for it to be technically correct
 
Old 08-04-2006, 11:26 AM   #5
llmmix
Member
 
Registered: Jun 2005
Posts: 73

Rep: Reputation: 15
compare your code with below library, it is written in c.
http://www.hyperrealm.com/main.php?s=cfl
Quote:
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.
 
Old 08-04-2006, 12:02 PM   #6
Natasl
LQ Newbie
 
Registered: May 2006
Posts: 15

Original Poster
Rep: Reputation: 0
well, it's C_linklist_destroy() function is quite similar to my cleanup part of the code, so i guess i did that right. ^^
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists: java vs c kpachopoulos Programming 4 10-30-2005 04:41 PM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
linked lists w/ nodes? |2ainman Programming 1 05-19-2004 01:32 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration