-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
157 lines (143 loc) · 3.25 KB
/
linked_list.c
File metadata and controls
157 lines (143 loc) · 3.25 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
// Node for the linked list
#include "linked_list.h"
llist* initlist() {
llist* temp_list = malloc(sizeof(llist));
temp_list->head = NULL;
temp_list->tail = NULL;
temp_list->size = 0;
return temp_list;
}
// Adds a node to the end of the linked list with the given data
void add_to_end(llist *list, void *data, int data_size)
{
/* We can have repeats
if (list_has(list, data)) {
return;
}
*/
node *new = initnode(data, data_size);
new->next = NULL;
new->prev = list->tail;
if (list->head == NULL)
{
list->head = new;
}
if (list->tail != NULL)
{
list->tail->next = new;
list->tail = new;
}
else
{
list->tail = new;
}
list->size++;
}
// Initializes a node with the given data
node *initnode(void *data, int data_size)
{
node *ptr = malloc(sizeof(node));
if (ptr == NULL)
{
return NULL;
}
else
{
ptr->ptr = data;
ptr->size = data_size;
return ptr;
}
}
// Pops and returns pointer to the least recently added node to the linked list
void* pop(llist *list)
{
void *ret = NULL;
node *next_node = NULL;
node *head = list->head;
if (head == NULL)
{
return NULL;
}
next_node = head->next;
if (next_node == NULL)
{
list->tail = NULL;
} else {
next_node->prev = NULL;
}
ret = head->ptr;
free(head);
list->head = next_node;
list->size--;
return ret;
}
//Removes the given element from the list
void list_remove(llist *list, void *element, int element_size) {
node *temp = list->head;
while (temp != NULL)
{
int size = max(element_size, temp->size);
if (memcmp(temp -> ptr, element, size) == 0) //todo make sure this works
{
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {//it must be the head
list->head = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
} else {//removing the tail
list->tail = temp->prev;
}
/*
printf("%p\n", temp);
printf("%p\n", temp->next);
printf("%p\n", temp->prev);
*/
printf("here\n");
free(temp);
printf("still here\n");
list->size--;
return;
}
temp = temp->next;
}
return;
}
// Returns if the list has a node with the given data
bool list_has(llist *list, void *data, int data_size)
{
node *temp = list->head;
while (temp != NULL)
{
int size = max(data_size, temp->size);
if (memcmp(temp -> ptr, data, size) == 0) //todo make sure this works
{
return true;
}
temp = temp->next;
}
return false;
}
//returns the nth element of the list.
void* index_at(llist *list, int index) {
node * temp = list->head;
for (int i = 0; i <index && temp != NULL; i++) {
}
return temp;
}
int max(int a, int b) {
if (a > b)
return a;
return b;
}
void* get_element_at(llist * list, int element_num) {
if ( element_num < 0 || element_num >= list->size) {
return NULL;
}
node * temp = list->head;
for (int i = 0; i < element_num; i++){
temp = temp-> next;
}
return temp->ptr;
}