Neater method of creating a linked list

  • Thread starter Ross Clement (Email address invalid - do not use)
  • Start date
R

Ross Clement (Email address invalid - do not use)

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 );
 
I

Ingo Menger

Ross said:
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.
 
R

Richard Harnden

Ross said:
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;
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top