This repository was archived by the owner on Dec 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
171 lines (148 loc) · 4.62 KB
/
linked_list.c
File metadata and controls
171 lines (148 loc) · 4.62 KB
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
#include "linked_list.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
//################################################################
//#############################PRIVTE#############################
//################################################################
struct linked_list_node {
void* data;
struct linked_list_node* forward;
struct linked_list_node* backward;
};
typedef struct linked_list_node linked_list_node;
struct linked_list {
int size;
linked_list_node* front;
linked_list_node* back;
};
typedef struct linked_list linked_list;
linked_list_node* new_linked_list_node(void* data) {
linked_list_node *node = malloc(sizeof(linked_list_node));
assert(node != NULL);
node->data = data;
node->forward = NULL;
node->backward = NULL;
return node;
}
linked_list_node* get_node(linked_list *list, int index) {
if (index == list->size)
return NULL;
assert(list != NULL);
assert(index >= 0 && index < list->size);
assert(list->front != NULL && list->back != NULL);
linked_list_node *node;
if (index < list->size / 2) {
node = list->front;
for (int i = 0; i < index; i++) {
assert(node != NULL);
node = node->forward;
}
} else {
node = list->back;
for (int i = list->size - 1; i > index; i--) {
assert(node != NULL);
node = node->backward;
}
}
return node;
}
//################################################################
//#############################PUBLIC#############################
//################################################################
void* new_linked_list() {
linked_list *list = malloc(sizeof(linked_list));
assert(list != NULL);
list->size = 0;
list->front = NULL;
list->back = NULL;
return list;
}
void* linked_list_set(void *list, void* data, int index) {
linked_list *l = (linked_list*) list;
linked_list_node *node = get_node(l, index);
void* old_data = node->data;
node->data = data;
return old_data;
}
void linked_list_insert(void* list, void* data, int index) {
assert(list != NULL);
linked_list *l = (linked_list*) list;
linked_list_node *new_node = new_linked_list_node(data);
if (l->front == NULL || l->back == NULL) {//SPECIAL CASE: FIRST NODE BEING INSERTED
assert(l->front == NULL && l->back == NULL); //if this assertion fails, then I've messed up. Messed up bad.
assert(index == 0);
l->front = new_node;
l->back = new_node;
l->size++;
return;
}
if (index == l->size) { //SPECIAL CASE: BEING INSERTED AT BACK; REQUIRES l->back TO BE UPDATED
new_node->forward = NULL; //redundant, for the sake of clarity
new_node->backward = l->back;
l->back->forward = new_node; //we know that l->back != NULL because of the prior if statment
l->back = new_node;
l->size++;
return;
}
if (index == 0) { //SPECIAL CASE: BEING INSERTED AT FRONT; REQUIRES l->front TO BE UPDATED
new_node->forward = l->front;
new_node->backward = NULL; //redundant, just here so that you know that I know what I'm doing
l->front->backward = new_node;
l->front = new_node;
l->size++;
return;
}
linked_list_node *node = get_node(l, index);
new_node->backward = node->backward;
new_node->forward = node;
if (node->backward != NULL)
node->backward->forward = new_node;
else
l->front = new_node;
node->backward = new_node;
l->size++;
}
void* linked_list_remove(void *list, int index) {
assert(index >= 0 && index < linked_list_size(list));
linked_list *l = (linked_list*) list;
linked_list_node *node = get_node(l, index);
if (node->backward != NULL)
node->backward->forward = node->forward;
else
l->front = node->forward;
if (node->forward != NULL)
node->forward->backward = node->backward;
else
l->back = node->backward;
void* removed_data = node->data;
free(node);
l->size--;
return removed_data;
}
void* linked_list_get(void *list, int index) {
linked_list *l = (linked_list*) list;
linked_list_node *node = get_node(l, index);
return node->data;
}
int linked_list_size(void* list) {
return((linked_list*) list)->size;
}
void linked_list_insert_Front(void *list, void *data) {
linked_list_insert(list, data, 0);
}
void linked_list_insert_Back(void *list, void *data) {
linked_list_insert(list, data, linked_list_size(list));
}
void* linked_list_get_front(void *list) {
return linked_list_get(list, 0);
}
void* linked_list_get_back(void *list) {
return linked_list_get(list, linked_list_size(list) - 1);
}
void* linked_list_remove_Front(void *list) {
return linked_list_remove(list, 0);
}
void* linked_list_remove_back(void *list) {
return linked_list_remove(list, linked_list_size(list) - 1);
}