Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #168168

Re: Generic Linked List Library (similar to C++ STL list) - version 1.0

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Generic Linked List Library (similar to C++ STL list) - version 1.0
Date 2022-11-13 13:17 +0000
Organization A noiseless patient Spider
Message-ID <87wn7zvvra.fsf@bsb.me.uk> (permalink)
References <9822ed97-2d42-4ffc-bf82-267b3c1d987dn@googlegroups.com>

Show all headers | View raw


Amit <amitchoudhary0523@gmail.com> writes:

> Generic Linked List Library (similar to C++ STL list) - version 1.0

I'm not sure what you want from posting this.  I'm going to assume you
want some comments on the code.

My main objection is the you force copying of the user's data when it's
often useful to have lists that share data.  You could provide both an
'add' and an 'add_copy' function, but since copying the data is simple,
you could just leave it up to the caller.

A second issue if that you return what is essentially a private
structure when returning data.  Surely you should simply return the data
pointer itself?  The user can't so anything useful with a struct
node_data, can they?

A third design issue is that you expose the definition of struct
generic_linked_list_container to the user.  This should be an 'opaque'
type visible only to the implementation.

Finally, why only add to end and remove from front?  A generic list
should support add to front and remove from end as well as a more
general mechanism to insert and remove data at any position.

> struct generic_linked_list_container
> *init_generic_linked_list_container(before_deleting_data_callback_function
> func)

It's all very well having long explanatory names (though I am not a fan)
but they should all start with some common prefix to indicate that they
are all related functions.  Something like gll_<whatever> for example.

> int add_new_node_to_end(struct generic_linked_list_container
> *gllc_ptr, void *data, long data_len)

size_t data_len would be more standard.

>     lln = malloc(sizeof(*lln));
>     if (!lln) {
>         return NO_MEMORY;
>     }
>
>     nddt = malloc(sizeof(*nddt));
>     if (!nddt) {
>         free(lln);
>         return NO_MEMORY;
>     }

I would try to avoid doing to allocations here.  It's not trivial to
arrange, but it can be done in modern C.

>     nddt->data = malloc((size_t)(data_len));
>     if (!nddt->data) {
>         free(nddt);
>         free(lln);
>         return NO_MEMORY;
>     }
>
>     nddt->data_len = data_len;
>     memmove(nddt->data, data, (size_t)(nddt->data_len));

There's no need to use memmove here.  A plain memcpy will be fine.  The
source and destination can't possibly overlap.

>     lln->nd_ptr = nddt;
>     lln->next = NULL;
>
>     if (gllc_ptr->total_number_of_nodes == 0) {
>         gllc_ptr->first = lln;
>         gllc_ptr->last = lln;
>     } else {
>         gllc_ptr->last->next = lln;
>     }
>
>     gllc_ptr->total_number_of_nodes = gllc_ptr->total_number_of_nodes + 1;

I'd shorten that:

  gllc_ptr->total_number_of_nodes += 1;

or

  ++gllc_ptr->total_number_of_nodes;

>     return GLLL_SUCCESS;
>
> } // end of add_new_node_to_end

-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-13 00:40 -0800
  Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-13 13:17 +0000
    Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-13 05:46 -0800
      Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-14 02:47 +0000
        Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-13 21:18 -0800
          Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Öö Tiib <ootiib@hot.ee> - 2022-11-14 00:39 -0800
            Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-14 01:14 -0800
          Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-11-14 12:25 +0000
            Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-14 06:25 -0800
              Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-14 09:03 -0800
                Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-11-14 09:16 -0800
  Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-11-13 12:58 -0800
    Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-11-14 01:32 -0800
      Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Po Lu <luangruo@yahoo.com> - 2022-11-14 17:47 +0800
        Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Amit <amitchoudhary0523@gmail.com> - 2022-11-14 02:07 -0800
  Re: Generic Linked List Library (similar to C++ STL list) - version 1.0 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-11-14 08:57 -0800

csiph-web