-
Notifications
You must be signed in to change notification settings - Fork 1
/
xorlinkedlist.c
64 lines (51 loc) · 1.31 KB
/
xorlinkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "xorlinkedlist.h"
#include <stdlib.h>
#include <stdio.h>
LinkedList* createNode (int value)
{
LinkedList* node = NULL;
node = (LinkedList*) malloc(sizeof(LinkedList));
if(NULL == node) {
printf("----> Debug -- Malloc failed! :-/");
return node;
}
node->value = value;
node->prev_next = NULL;
return node;
}
LinkedList* nextNode (LinkedList* nextOfNode)
{
return XOR (NULL, nextOfNode->prev_next);
}
LinkedList* prevNode (LinkedList* prevOfNode)
{
return XOR (prevOfNode->prev_next, NULL);
}
void insertNodeEnd (LinkedList* currentHead, LinkedList* newHead)
{
// current head is previous of new head
newHead->prev_next = XOR(currentHead, NULL);
if(NULL != currentHead) {
// new head is next of the old head
currentHead->prev_next = XOR(NULL, newHead);
}
}
void insertNodeFront (LinkedList* currentHead, LinkedList* newHead)
{
// we don't have previous node for head
// but we have a next node (currentHead)
newHead->prev_next = XOR(NULL, currentHead);
if(NULL != currentHead) {
// previous node of the old head is the new
currentHead->prev_next = XOR(newHead, NULL);
}
}
bool containsValue (LinkedList* first, int value)
{
return false;
}
/* see also the corresponding unit test */
LinkedList* XOR (LinkedList* lhs, LinkedList* rhs)
{
return (LinkedList*) ((intptr_t)lhs ^ (intptr_t)rhs);
}