-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0707-design-linked-list.c
130 lines (114 loc) · 3 KB
/
0707-design-linked-list.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
typedef struct Node {
struct Node* next;
struct Node* prev;
int val;
} Node;
typedef struct {
Node* head;
Node* tail;
int length;
} MyLinkedList;
Node* createNode(int val) {
Node* new_node = (Node*)malloc(sizeof(Node) );
new_node->next = NULL;
new_node->prev = NULL;
new_node->val = val;
return new_node;
}
MyLinkedList* myLinkedListCreate() {
MyLinkedList* list = (MyLinkedList*)malloc(sizeof(MyLinkedList) );
list->head = createNode(-1);
list->tail = list->head;
list->length = 0;
return list;
}
Node* traverseList(MyLinkedList* obj, int index) {
if (index < 0 || index >= obj->length)
return NULL;
Node* trev = obj->head;
while (index >= 0) {
trev = trev->next;
index -= 1;
}
return trev;
}
int myLinkedListGet(MyLinkedList* obj, int index) {
Node *node = traverseList(obj, index);
if (node != NULL)
return node->val;
return -1;
}
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
Node* new_node = createNode(val);
new_node->next = obj->head->next;
new_node->prev = obj->head;
obj->head->next = new_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
else
obj->tail = new_node;
obj->length += 1;
return;
}
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
Node* new_node = createNode(val);
obj->tail->next = new_node;
new_node->prev = obj->tail;
obj->tail = obj->tail->next;
obj->length += 1;
return;
}
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if (index == 0) {
myLinkedListAddAtHead(obj, val);
return;
}
else if (index == obj->length) {
myLinkedListAddAtTail(obj, val);
return;
}
Node* prev_node = traverseList(obj, index - 1);
if (prev_node == NULL)
return;
Node* new_node = createNode(val);
new_node->next = prev_node->next;
new_node->prev = prev_node;
prev_node->next = new_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
obj->length += 1;
return;
}
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
Node *node = traverseList(obj, index);
if (node == NULL)
return;
Node *temp = node;
node->prev->next = node->next;
if (node->next != NULL)
node->next->prev = node->prev;
else
obj->tail = node->prev;
obj->length -= 1;
free(temp);
return;
}
void myLinkedListFree(MyLinkedList* obj) {
Node *prev = NULL, *cur = obj->head;
while (cur != NULL) {
prev = cur;
cur = cur->next;
free(prev);
}
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);
*/