ds/list.c
2025-01-06 19:39:32 +01:00

77 lines
1.7 KiB
C

#include <stdio.h>
// Single linked list with a single type
#if 0
#define MAKE_LIST(type) \
struct type##_node_t; \
typedef struct { \
type data; \
struct type##_node_t *next; \
} type##_node_t; \
typedef struct type##_node_t* type##_node_t_ptr
MAKE_LIST(int);
MAKE_LIST(char);
#endif
// Single linked list with fat struct
#if 1
typedef struct node_ node_t;
typedef struct node_* node_ptr;
typedef enum { INT, UINT, CHAR, FLOAT, DOUBLE, CHAR_PTR } node_value_t;
typedef struct node_ {
int type;
union {
int vi;
unsigned int vii;
char cv;
float fv;
double dv;
void *ptr;
};
node_ptr* children;
//node_ptr prev;
} node_t;
#endif
// Time (single-linked) Time (double-linked)
// Insertar al principio O(1) O(1)
// Insertar al final O(n) / O(1) O(n) / O(1)
// Insertar arbitrario O(n) O(n/2) / O(1)
// Borrar arbitrario O(n) O(n/2) / O(1)
// Buscar O(n) O(n/2) / O(1)
int main()
{
node_t node1 = { INT, { .vi = -1 }, 0 };
node_t node2 = { UINT, { .vii = 1 }, 0 };
node_t node3 = { CHAR, { .cv = 'v' }, 0 };
node1.next = &node2;
node2.next = &node3;
node_ptr n = &node1;
do {
switch(n->type) {
case INT:
printf("%d\n", n->vi);
break;
case UINT:
printf("%d\n", n->vii);
break;
case CHAR:
printf("%c\n", n->cv);
break;
default:
break;
}
} while ((n = n->next));
return 0;
}
/* int num = (int)this; */
/* &(this->mem) */
/* int *member = num + offsetof(node_t, next); */
/* *member = 1; */