Thread: One more linked list implementation

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    BEN10, i explicitly let know that actually it returns pointer to my struct type.
    Just to second BEN10, my observation would be that most people do not cast malloc and it is not necessary. The pointer type will be set by the left hand side of the assignment.

    Today's code looks much closer to the norm, I notice you are using the return pointers which IMO is the best way to go. A couple of minor things from removenode():
    //handle the deletion of first element
        if (list->ptr != NULL && list->data == value)
            first = list->ptr;
            list->ptr = NULL;
            return first;
    It's sort of pointless to set list->ptr to NULL there since it is part of a struct that is about to be free'd anyway. A good rule with pointer when you free them is to then set them NULL, but only if you are keeping the handle in a variable that might get reused. But after free(list), it will be impossible to make use of list->ptr.

    You could nest the two blocks dealing with first matching value:
    if (list->data == value) {
        if (!list->ptr) {   /* the only element */
            return NULL;
        } else {            /* the first element */
            first = list->ptr;
            return first;
    Which will be a slight optimization since in the case of deleting the first element, we only need to check those (binary/boolean) conditions (value and ->ptr) once each instead of checking list->ptr twice and then value.
    while (list->ptr != NULL)
    if you make this "while (list)" you can include deleting the last pointer in this loop and not need a seperate block afterward. If none of the nodes match, list will finally get assigned NULL because the last list->ptr is/should be NULL, and the loop will end.

    But generally a good job methinks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    excellent, that optimization makes sense, thank you very much

    but one question about linked lists... how often do you really need such stuff? i'm mostly into electronics than programming now..

    it is just interesting for me how really good it is... for example in some big sophisticated many number of allocation and freeing the it efficient?

    how about stack? do you think implementing a stack with a linked lists is a good idea?

    how often have u ever had to implement linked list to solve real problem, rather than for educational purposes?
    Last edited by BlackOps; 07-16-2009 at 10:21 AM.

  3. #18
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    [QUOTE=BlackOps;877659]how about stack? do you think implementing a stack with a linked lists is a good idea?

    I'm also very new to datastructure, so I can't tell about your other doubts. But about linked implementation of stacks, it has more advantages than array implementation. No space is pre allocated to a stack and that's why no stack uses more space than it needs. Also more than one stack can be implemented from the single available list. The disadvantage of the array implementation is that not exact number of nodes can be determined at the compile time, thus there's always a possibility that the size we allocated becomes insufficient.

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior

    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM