Created
July 2, 2015 21:15
-
-
Save OlliV/6deafc2a04e126e7c053 to your computer and use it in GitHub Desktop.
doubly linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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