One more linked list implementation

This is a discussion on One more linked list implementation within the C Programming forums, part of the General Programming Boards category; Code: BEN10, i explicitly let know that actually it returns pointer to my struct type. Just to second BEN10, my ...

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    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():
    Code:
    //handle the deletion of first element
        if (list->ptr != NULL && list->data == value)
            {
            first = list->ptr;
            list->ptr = NULL;
            free(list);
            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:
    Code:
    if (list->data == value) {
        if (!list->ptr) {   /* the only element */
            free(list);
            return NULL;
        } else {            /* the first element */
            first = list->ptr;
            free(list);
            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.
    Code:
    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
    Location
    AZERBAIJAN
    Posts
    78
    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 program...so many number of allocation and freeing the memory...is 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 11:21 AM.

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

    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.
    HOPE YOU UNDERSTAND.......

    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

Page 2 of 2 FirstFirst 12
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, 11:03 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21