A
aditya
how do we implement a generic linklist?
how do we implement a generic linklist?
aditya said:how do we implement a generic linklist?
aditya said:how do we implement a generic linklist?
how do we implement a generic linklist?
cur = cur->next;data) <= 0)
Chris Dollin said:Implement a specific linklist and then abstract.
aditya said:how do we implement a generic linklist?
how do we implement a generic linklist?
Richard Heathfield said:Chris M. Thomasson said:
That wasn't very kind.
What has the OP ever done to you?![]()
CBFalconer said:typedef struct listitem {
struct listitem *next;
void *data;
} listitem;
listitem *root = NULL;
Richard said:It's funny how you dont tell them to do their own homework when
you actually have a solution.
how do we implement a generic linklist?
how do we implement a generic linklist?
Several people have suggested code in which a node contains a void
pointer, which points to the actual data, but it is possible to do
things differently. This is from a program I wrote a number of years
ago - I've trimmed it down a bit.
Firstly, you include in the struct a pointer to the next one and the
previous one, eg:
struct appoint {
char day;
char mon;
int year;
char text[49];
struct appoint *next;
struct appoint *prior;
} ;
struct address {
char title[8];
char firstname[12];
char surname[20];
struct address *next;
struct address *prior;
} ;
Now, you need functions to get at these pointers. For example:
typedef void * Pointer;
Pointer *pnext(int type, Pointer thing) {
switch (type) {
case ADDRESS: return (Pointer *)
&(((struct address *) thing) -> next);
case APPOINT: return (Pointer *)
&(((struct appoint *) thing) -> next); }
return NULL; }
and a similar one for pprior. You can then write generic function such
as:
void linkin(int type, Pointer thing, Pointer before, Pointer after,
Pointer *first, Pointer *last) {
*pnext(type, thing) = after;
Surely you don't mean to return a void**.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^blargg said:how do we implement a generic linklist?
Sorry for the length. Here's a quick example of a doubly-linked list. It's
circular to eliminate special-cases.
#include <assert.h>
#include <stdio.h>
/* Circular doubly-linked list */
typedef struct Node Node;
struct Node
{
Node* next;
Node* prev;
};[...]
/* Example using list */
typedef struct Foo Foo;
struct Foo
{
Node node; /* must be first */
const char* str;
};
static void print_list( Foo* list )
{
[...]
}
int main()
{
[...]
}
pete said:It's a strange way to implement a list.
The nodes of the list are of type (struct Foo),
and the list doesn't contain any pointers to that type.
pete said:Do you think it's a good way to implement a list in C?
pete said:Chris said:^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^blargg said:how do we implement a generic linklist?
Sorry for the length. Here's a quick example of a doubly-linked list.
It's
circular to eliminate special-cases.
#include <assert.h>
#include <stdio.h>
/* Circular doubly-linked list */
typedef struct Node Node;
struct Node
{
Node* next;
Node* prev;
};[...]
/* Example using list */
typedef struct Foo Foo;
struct Foo
{
Node node; /* must be first */
This requirement can easily be removed by using the `offsetof'
function macro...
It's a strange way to implement a list.
The nodes of the list are of type (struct Foo),
and the list doesn't contain any pointers to that type.
blargg said:pete said:Chris said:how do we implement a generic linklist?
Sorry for the length. Here's a quick example of a doubly-linked list.
It's circular to eliminate special-cases.
#include <assert.h>
#include <stdio.h>
/* Circular doubly-linked list */
typedef struct Node Node;
struct Node
{
Node* next;
Node* prev;
};
[...]
/* Example using list */
typedef struct Foo Foo;
struct Foo
{
Node node; /* must be first */
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This requirement can easily be removed by using the `offsetof' function
macro...
Without cluttering user code up with even more complex casts?
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.