-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.c
159 lines (138 loc) · 2.93 KB
/
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
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
#include <stdio.h>
#include <stdlib.h>
#define MIN_INT (-2147483648)
typedef struct _node {
struct _node *prev;
struct _node *next;
int val;
} node_t;
typedef struct _list {
node_t *items;
} list_t;
int max(list_t *list) {
int current_max = MIN_INT;
node_t *node = list->items;
while (node) {
if (node->val > current_max) {
current_max = node->val;
}
node = node->next;
}
return current_max;
}
// just to insert something quickly
// at the front
void quick_insert(list_t *list, int item){
node_t *node;
node = (node_t*) malloc(sizeof(*node));
node->val = item;
node->next = list->items;
node->prev = NULL;
list->items = node;
}
// if index is greater than the length of the list this returns last element
node_t* get(list_t *list, int index) {
if (!(list->items)) {
return NULL;
}
node_t *node = list->items;
for (int i = 0; i < index; i++) {
if (node->next == NULL) {
return node;
}
node = node->next;
}
return node;
}
void insert_here(list_t *list, node_t *node, int elem) {
// if the list is empty
if (!node) {
node = (node_t*) malloc(sizeof(*node));
node->val = elem;
node->next = list->items;
node->prev = NULL;
list->items = node;
return;
}
node_t *new = (node_t*) malloc(sizeof(*node));
new->prev = node->prev;
new->next = node;
new->val = elem;
if (node->prev == NULL) {
list->items = new;
} else {
node->prev->next = new;
}
node->prev = new;
}
void insert_sorted(list_t *list, int elem) {
node_t *node = list->items;
if (!node) {
node = (node_t*) malloc(sizeof(*node));
node->val = elem;
node->next = list->items;
node->prev = NULL;
list->items = node;
return;
}
while (node->val <= elem && node->next) {
node = node->next;
}
insert_here(list, node, elem);
}
void delete_here(list_t *list, node_t *node) {
// if the list is empty
if (!(list->items)) {
return;
}
if (node->next) {
node->next->prev = node->prev;
}
if (node->prev) {
node->prev->next = node->next;
free(node);
return;
}
list->items = node->next;
free(node);
}
void print_array(list_t *list) {
node_t *node = list->items;
while (node) {
printf("%d ", node->val);
node = node->next;
}
printf("\n\n");
}
void setup(list_t *list){
list->items = NULL;
}
void setupSize(list_t *list, int size){
list->items = (node_t*)malloc(size*sizeof(*(list->items)));
}
/*
int main(){
list_t list;
setup(&list);
print_array(&list);
for (int i = 0; i < 10; i++) {
printf("loc =");
int loc;
scanf("%d", &loc);
int elem;
printf("elem =");
scanf("%d", &elem);
node_t *node = get(&list, loc);
insert_here(&list, node, elem);
print_array(&list);
}
for (int i = 0; i < 10; i++) {
printf("loc =");
int loc;
scanf("%d", &loc);
node_t *node = get(&list, loc);
delete_here(&list, node);
print_array(&list);
}
}
*/