-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlink.c
213 lines (175 loc) · 3.76 KB
/
dlink.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// DoubleLinkedList -- dlink.c
// BigPa
#include "dlink.h"
// declare node with two pointers
typedef struct tagOfNode {
struct tagOfNode* prev;
struct tagOfNode* next;
void* content;
} node;
// head node
static node* head = NULL;
// number of nodes
static int count = 0;
static node* createNode(void* pval) {
// create temp node
node* temp = NULL;
temp = (node*) malloc(sizeof(node));
if (!temp) {
// printf("Node creation error\n");
return NULL;
}
else {
// point to itself
temp->prev = temp->next = temp;
temp->content = pval;
return temp;
}
}
int createDLink() {
head = createNode(NULL);
if (!head) {
return -1;
}
else {
count = 0;
head->content = head;
}
return 0;
}
int dLinkIsEmpty() {
return count == 0;
}
int dLinkSize() {
return count;
}
static node* getNode(int index) {
if (index < 0 || index > count) {
// printf("index is out of range\n");
return NULL;
}
// check one by one from closer side
if (index <= count / 2) {
int i = 0;
node* temp = head; //->next;
while (i++ < index) {
temp = temp->next;
}
return temp;
}
else {
// i is the steps to move
int i = count - index + 1;
node* temp = head; //->next;
while (i-- != 0) {
temp = temp->prev;
}
return temp;
}
}
static node* getFirstNode() {
return getNode(1);
}
static node* getLastNode() {
return getNode(count);
}
void* getContentOfNode(int index) {
node* temp = getNode(index);
if (!temp) {
// printf("Get Node content failed.\n");
return NULL;
}
if (temp->content == NULL) {
// printf("The Node has no content.\n");
}
return temp->content;
}
void* getContentOfFirstNode() {
return getContentOfNode(1);
}
void* getContentOfLastNode() {
return getContentOfNode(count);
}
int insertNodeAtFirst(void* content) {
node* temp = createNode(content);
if (!temp) {
return -1;
}
temp->prev = head;
temp->next = head->next;
head->next->prev = temp;
head->next = temp;
++count;
return 0;
}
int insertNodeAtLast(void* content) {
node* temp = createNode(content);
if (!temp) {
return -1;
}
temp->next = head;
temp->prev = head->prev;
head->prev->next = temp;
head->prev = temp;
++count;
return 0;
}
int deleteNode(int index) {
node* temp = getNode(index);
if (!temp) {
// printf("Get node failed, index is out of range.\n");
return -1;
}
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
free(temp);
--count;
return 0;
}
int deleteNodeAtFirst() {
return deleteNode(1);
}
int deleteNodeAtLast() {
return deleteNode(count);
}
int destoryDLink() {
if (!head) {
// printf("dLink does not exist.\n");
return -1;
}
node* temp = head->next;
// node being destoryed
node* dest = NULL;
while (temp != head) {
dest = temp;
temp = temp->next;
free(dest);
}
free(head);
head = NULL;
count = 0;
return 0;
}
int insertNode(int index, void* content) {
if (index == 0) {
// printf("Inserted After Header");
return insertNodeAtFirst(content);
}
else {
node* temp = getNode(index);
if (!temp) {
// printf("get node failed.\n");
return -1;
}
node* nnode = createNode(content);
if (!nnode) {
return -1;
}
nnode->prev = temp->prev;
nnode->next = temp;
temp->prev->next = nnode;
temp->prev = nnode;
++count;
return 0;
}
}