Skip to content

Instantly share code, notes, and snippets.

@OlliV
Created July 2, 2015 21:15
Show Gist options
  • Save OlliV/6deafc2a04e126e7c053 to your computer and use it in GitHub Desktop.
Save OlliV/6deafc2a04e126e7c053 to your computer and use it in GitHub Desktop.
doubly linked list
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
struct node {
int x;
struct node * next;
};
void print(struct node * head)
{
struct node * n = head;
struct node * prev = NULL;
struct node * t;
do {
t = n;
n = (struct node *)((uintptr_t)n->next ^ (uintptr_t)prev);
prev = t;
printf("%d ", t->x);
} while (n);
printf("\n");
}
int main(void)
{
struct node nodes[10];
struct node * prev = NULL;
size_t i;
for (i = 0; i < 10; i++) {
nodes[i].x = i;
if (i < 9) {
nodes[i].next = (struct node *)((uintptr_t)prev ^
(uintptr_t)(nodes + i + 1));
} else {
nodes[i].next = prev;
}
prev = nodes + i;
}
print(nodes);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment