-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_LL.c
141 lines (126 loc) · 3.72 KB
/
xor_LL.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "xor_LL.h"
void init_xLinkedList(xLinkedList* list){
list->tail = NULL;
list->head = NULL;
}
void add_node(void* data, xLinkedList* list){
xNode* new_xNode = (xNode* )malloc(sizeof(xNode));
new_xNode->data = data;
unsigned long prev_addr = (unsigned long) list->tail;
unsigned long next_addr = (unsigned long)(new_xNode);
new_xNode->address_ptr = prev_addr;
if(!list->tail){
#if DEBUG_XOR_LL
printf("adding first xNode\n");
#endif
new_xNode->address_ptr = (unsigned long)NULL;
list->tail = new_xNode;
list->head = new_xNode;
}
else if(!list->tail->address_ptr){
#if DEBUG_XOR_LL
printf("adding second xNode\n");
#endif
list->tail->address_ptr = next_addr;
}
else{
#if DEBUG_XOR_LL
printf("adding a new xNode\n");
#endif
list->tail->address_ptr = next_addr ^ list->tail->address_ptr;
}
list->tail = new_xNode;
}
void traverse_list(xLinkedList* list){
//start at tail and traverse in reverse
xNode* curr = list->tail;
xNode* next;
xNode* prev = NULL;
printf("Traversing list in reverse\n");
if(!curr)printf("this list is empty\n");
while (curr){
printf ("data is: %p \n", curr->data);
next = (xNode*)((unsigned long)prev ^ (unsigned long)curr->address_ptr);
printf ("prev is %p, curr is %p, next is %p, address_ptr is %p\n",prev, curr, next, (void*)curr->address_ptr);
prev = curr;
curr = next;
}
}
void delete_head_func(xLinkedList* list){
xNode* curr = list->head;
//xNode* next = NULL;
xNode* prev = (xNode*)((unsigned long)(curr->address_ptr));
#if DEBUG_XOR_LL
printf("list contains more than one xNode. deleting head\n");
#endif
//now go back to the previous node. set the prev node's address pointer to point only back to the node before it;
//therefore the head node can now be popped from the list
xNode* before_prev = (xNode* )((unsigned long)curr ^ (unsigned long)prev->address_ptr);
prev->address_ptr = (unsigned long)before_prev;
list->head = NULL;
free(list->head);
list->head = prev;
}
void delete_tail_func(xLinkedList* list){
xNode* curr = list->tail;
xNode* next;
xNode* prev = NULL;
#if DEBUG_XOR_LL
printf("list contains more than one xNode. deleting tail\n");
#endif
next = (xNode*)((unsigned long)prev ^ (unsigned long)curr->address_ptr);
//go to next xNode
//set address_ptr at next xNode to point to the address of the next xNode it needs to go to
//prev will be set to NULL
prev = curr;
curr = next;
next->address_ptr = (unsigned long)((unsigned long)prev ^ (unsigned long)curr->address_ptr);
list->tail = NULL;
free(list->tail);
list->tail = next;
}
void delete_end(xLinkedList* list, int delete_head){
//set delete_head to 1 if deleting head, else deleting tail
//deletes the xNode for which next is NULL;
// note this is done from the perspecive of traversing the list in reverse as before
if (!list->tail){
//list empty, head and tail are NULL
#if DEBUG_XOR_LL
printf("list empty. unable to delete\n");
#endif
}
else if(!list->tail->address_ptr){
#if DEBUG_XOR_LL
printf("list contains only one xNode. This is being deleted\n");
#endif
list->tail = NULL;
free(list->tail);
list->head = NULL;
free(list->head);
}else {
if(delete_head){
delete_head_func(list);
}else{
delete_tail_func(list);
}
}
}
xNode* pop_node_stack(xLinkedList* list){
//pops the most recently added node
//returns the tail node of the list
// note this is done from the perspecive of traversing the list in reverse as before
xNode* temp = list->tail;
delete_end(list, 0);
return temp;
}
xNode* pop_node_queue(xLinkedList* list){
xNode* temp = list->head;
delete_end(list, 1);
return temp;
}
void reverse(xLinkedList* list){
xNode* temp;
temp = list->head;
list->head = list->tail;
list->tail = temp;
}