-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_list.c
169 lines (127 loc) · 3.75 KB
/
single_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
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
//
// Created by lflish on 2019/12/4.
// 头结点与节点不适用相同结构
// 需要考虑 头结点,但是可以通过二级指针解决
//
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int val;
struct node *next;
} Node;
typedef struct {
struct node *head;
}MyLinkedList;
/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
MyLinkedList *head = malloc(sizeof(MyLinkedList));
head->head = NULL;
return head;
}
/** 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) {
int i = 0;
Node *node = obj->head;
for( ;node != NULL; node = node->next, i++){
if(i == index)
return node->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) {
Node *node = malloc(sizeof(Node));
node->val = val;
node->next = obj->head;
obj->head = node;
return;
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
Node *new = malloc(sizeof(Node));
new->val = val;
new->next = NULL;
Node **node = &obj->head;
while(*node != NULL)
node = &(*node)->next;
*node = new;
}
/** 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;
}
int i = 0;
// 0 1 2
Node **node = &obj->head;
for(; *node != NULL; node = &(*node)->next, i++){
if(i == index) {
Node *new = malloc(sizeof(Node));
new->val = val;
new->next = (*node);
*node = new;
return;
}
}
if(i == index)
myLinkedListAddAtTail(obj, val);
return;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
int i = 0;
Node **node = &obj->head;
for( ; *node != NULL; node = &(*node)->next, i++){
if(i == index){
Node *tmp = *node;
*node = tmp->next;
free(tmp);
return;
}
}
return;
}
void myLinkedListFree(MyLinkedList* obj) {
Node *node = obj->head;
while(node != NULL){
Node *tmp = node;
node = node->next;
free(tmp);
}
free(obj);
return ;
}
/**
* 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 list(MyLinkedList *obj){
Node *node = obj->head;
while(node != NULL){
printf("%d\n", node->val);
node = node->next;
}
printf("---------\n");
}
int main(){
MyLinkedList* obj = myLinkedListCreate();
myLinkedListAddAtHead(obj, 1);
myLinkedListAddAtTail(obj, 3);
list(obj);
myLinkedListAddAtIndex(obj, 1, 2);
list(obj);
int param_1 = myLinkedListGet(obj, 1);
printf("test1 = %d\n", param_1);
myLinkedListDeleteAtIndex(obj, 1);
list(obj);
param_1 = myLinkedListGet(obj, 1);
printf("test2 = %d\n", param_1);
myLinkedListFree(obj);
}