Neater method of creating a linked list

Discussion in 'C Programming' started by Ross Clement (Email address invalid - do not use), Nov 30, 2005.

  1. Hi. I would like to make a typedef for a structure which can be joined
    up into a linked list. I include my code below. My code works, but I
    have a vague suspicion that I could have declared my "spectrum" type in
    a neater way. Is there a better way of doing the following?

    Thanks in anticipation,

    Ross-c

    #include <stdlib.h>

    typedef struct spectrum spectrum;

    struct spectrum
    {
    double *magnitude;
    double *phase;
    int n;

    double phaseNormalisation;
    double magnitudeNormalisation;

    spectrum *next;
    };

    spectrum *spectrum_make( double *data[], int N );
    void spectrum_free( spectrum *s );
    Ross Clement (Email address invalid - do not use), Nov 30, 2005
    #1
    1. Advertising

  2. Ross Clement (Email address invalid - do not use)

    Ingo Menger Guest

    Ross Clement (Email address invalid - do not use) schrieb:

    > Hi. I would like to make a typedef for a structure which can be joined
    > up into a linked list. I include my code below. My code works, but I
    > have a vague suspicion that I could have declared my "spectrum" type in
    > a neater way. Is there a better way of doing the following?


    Yes.
    It might be a good idea to write code for linked lists as such. Most
    functions on lists work for list of spectrums as well as for list of
    characters or list of luxury cars. One example would be reverse(),
    another append(), that appends one list to another list. Others do need
    helper functions, for example sort(), filter() or map().

    Alternatively, switch to C++ and use some library, such as the STL,
    where container data types are already available in all sizes and
    shapes.
    Ingo Menger, Nov 30, 2005
    #2
    1. Advertising

  3. Ross Clement (Email address invalid - do not use) wrote:
    > Hi. I would like to make a typedef for a structure which can be joined
    > up into a linked list. I include my code below. My code works, but I
    > have a vague suspicion that I could have declared my "spectrum" type in
    > a neater way. Is there a better way of doing the following?
    >
    > Thanks in anticipation,
    >
    > Ross-c
    >
    > #include <stdlib.h>
    >
    > typedef struct spectrum spectrum;
    >
    > struct spectrum
    > {
    > double *magnitude;
    > double *phase;
    > int n;
    >
    > double phaseNormalisation;
    > double magnitudeNormalisation;
    >
    > spectrum *next;
    > };
    >
    > spectrum *spectrum_make( double *data[], int N );
    > void spectrum_free( spectrum *s );
    >


    Seperate the linked-list from the spectrum parts, ie

    struct spectrum
    {
    double *magnitude;
    double *phase;
    int n;
    double phaseNormalisation;
    double magnitudeNormalisation;
    };

    struct list
    {
    void *payload;
    struct list *next;
    };

    typedef void (*free_payload)(void *p);

    struct list_info
    {
    free_payload free_fn;
    struct list *first;
    };
    Richard Harnden, Nov 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stimp
    Replies:
    5
    Views:
    461
    Stimp
    Oct 14, 2005
  2. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  3. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  4. Bill Cunningham

    neater code

    Bill Cunningham, Jan 31, 2010, in forum: C Programming
    Replies:
    14
    Views:
    620
    Ian Collins
    Mar 1, 2010
  5. Replies:
    9
    Views:
    93
    Brad Baxter
    May 1, 2006
Loading...

Share This Page