-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleList.c
184 lines (127 loc) · 3.95 KB
/
DoubleList.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
/*
* Created by lflish on 2020-07-02
* Double List
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct list{
int val;
struct list *next, *prev;
} MyLinkedList;
/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
MyLinkedList *node = malloc(sizeof(MyLinkedList));
node->next = NULL;
node->prev = NULL;
return node;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
static MyLinkedList* myLinkedListGetNode(MyLinkedList* obj, int index) {
MyLinkedList *cur = obj->next;
int i = 0;
for(i = 0; cur != NULL; i++, cur = cur->next){
if(i == index)
return cur;
}
return NULL;
}
int myLinkedListGetLen(MyLinkedList* obj) {
MyLinkedList *cur = obj->next;
int i = 0;
while(cur != NULL)
i++, cur = cur->next;
return i;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int myLinkedListGet(MyLinkedList* obj, int index) {
MyLinkedList *cur = obj->next;
int i = 0;
for(i = 0; cur != NULL; i++, cur = cur->next){
if(i == index)
return cur->val;
}
return -1;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyLinkedList *node = myLinkedListCreate();
node->val = val;
node->next = obj->next;
node->prev = obj;
if(node->next != NULL)
node->next->prev = node;
obj->next = node;
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList *node = myLinkedListCreate();
node->val = val;
while(obj->next != NULL)
obj = obj->next;
obj->next = node;
node->prev = obj;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if(index == 0){
myLinkedListAddAtHead(obj, val);
return;
}
if(index == myLinkedListGetLen(obj)){
myLinkedListAddAtTail(obj, val);
return;
}
MyLinkedList *cur = myLinkedListGetNode(obj, index);
if(cur == NULL)
return;
MyLinkedList *node = myLinkedListCreate();
node->val = val;
node->next = cur;
node->prev = cur->prev;
cur->prev->next = node;
cur->prev = node;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
MyLinkedList *node = myLinkedListGetNode(obj, index);
if(node == NULL)
return;
node->prev->next = node->next;
if(node->next != NULL)
node->next->prev = node->prev;
free(node);
}
void myLinkedListFree(MyLinkedList* obj) {
free(obj);
}
/**
* Your MyLinkedList struct will be instantiated and called as such:
* MyLinkedList* obj = myLinkedListCreate();
* int param_1 = myLinkedListGet(obj, index);
* myLinkedListAddAtHead(obj, val);
* myLinkedListAddAtTail(obj, val);
* myLinkedListAddAtIndex(obj, index, val);
* myLinkedListDeleteAtIndex(obj, index);
* myLinkedListFree(obj);
*/
void debug(MyLinkedList *obj){
MyLinkedList *cur = obj->next;
for(; cur != NULL; cur = cur->next)
printf("%d ", cur->val);
printf("\n");
}
int main()
{
MyLinkedList *obj = myLinkedListCreate();
int param_1 = myLinkedListGet(obj, 1);
myLinkedListAddAtHead(obj, 1);
debug(obj);
myLinkedListAddAtTail(obj, 3);
debug(obj);
myLinkedListAddAtIndex(obj, 1, 2);
debug(obj);
myLinkedListDeleteAtIndex(obj, 1);
debug(obj);
myLinkedListFree(obj);
}