-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
178 lines (157 loc) · 3.69 KB
/
linked_list.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "linked_list.hpp"
linkedList *initLinkedList() {
linkedList *ll = (linkedList *)malloc(sizeof(linkedList));
ll->size = 0;
ll->root = NULL;
ll->end = NULL;
// ll->root = (linkedListNode *)malloc(sizeof(linkedListNode));
// ll->root->next = NULL;
// ll->root->prev = NULL;
return ll;
}
void ll_free_list(linkedList *ll) {
int i;
linkedListNode *node = ll->root;
linkedListNode *last = node;
for (i = 0; i < ll->size; i++) {
node = node->next;
free(last->content);
free(last);
last = node;
}
}
linkedListNode *ll_add_node(linkedList *ll, void *content) {
int i;
linkedListNode *node;
linkedListNode *newnode = (linkedListNode *)malloc(sizeof(linkedListNode));
newnode->content = content;
newnode->next = NULL;
newnode->prev = NULL;
if (ll->size == 0) {
ll->root = newnode;
ll->end = newnode;
} else {
node = ll->end;
node->next = newnode;
newnode->prev = node;
}
ll->end = newnode;
ll->size++;
return newnode;
}
void ll_add_after_node(linkedList *ll, linkedListNode *node, void *content) {
int i;
// linkedListNode* node = ll->root;
// for(i=0;i<ll->size;i++) {node = node->next;}
linkedListNode *newnode = (linkedListNode *)malloc(sizeof(linkedListNode));
newnode->content = content;
newnode->next = node->next;
newnode->prev = node;
if (newnode->next != NULL) {
newnode->next->prev = newnode;
}
if (node == ll->end) {
ll->end = newnode;
}
node->next = newnode;
ll->size++;
}
void ll_add_node_if_not_exist(linkedList *ll, void *content) {
int i;
linkedListNode *node = ll->root;
for (i = 0; i < ll->size; i++) {
if (node->content == content) {
return;
}
node = node->next;
}
node->next = (linkedListNode *)malloc(sizeof(linkedListNode));
node->content = content;
ll->size++;
}
gItem* ll_get_node_if_exist(linkedList *ll, int id) {
int i;
linkedListNode *node = ll->root;
for (i = 0; i < ll->size; i++) {
gItem *gi = (gItem *)node->content;
if (gi->id == id) {
return gi;
}
node = node->next;
}
return NULL;
}
linkedListNode *ll_pop_first_node(linkedList *ll) {
int i;
linkedListNode *node = ll->root;
if (ll->size > 1) {
ll->root = node->next;
} else {
ll->root = NULL;
}
ll->size--;
return node;
}
void ll_remove_node(linkedList *ll, int idx) {
int i;
linkedListNode *node = ll->root;
linkedListNode *last = node;
for (i = 0; i < ll->size; i++) {
if (*((int *)node->content) == idx) {
if (i == 0) {
ll->root = node->next;
} else {
last->next = node->next;
}
free(node);
ll->size--;
break;
}
last = node;
node = node->next;
}
}
void ll_remove_node2(linkedList *ll, int idx) {
int i;
linkedListNode *node = ll->root;
linkedListNode *last = node;
for (i = 0; i < ll->size; i++) {
/*if (*((int*) node->content) == idx) {*/
if (((gItem *)node->content)->id == idx) {
if (i == 0) {
ll->root = node->next;
} else {
last->next = node->next;
}
free(node);
ll->size--;
break;
}
last = node;
node = node->next;
}
}
void *ll_get_item(linkedList *ll, int idx) {
int i;
linkedListNode *node = ll->root;
for (i = 0; i < ll->size && i < idx; i++) {
node = node->next;
}
return node->content;
}
void ll_write_ints_to_file(linkedList *ll, const char *fn, int N) {
int i;
linkedListNode *node = ll->root;
for (i = 0; i < ll->size; i++) {
node = node->next;
}
FILE *fp;
fp = fopen(fn, "w");
for (int i = 0; i < ll->size; i++) {
for (int j = 0; j < N; j++) {
fprintf(fp, "%d ", ((int *)ll_get_item(ll, i))[j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}