-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.c
179 lines (156 loc) · 3.56 KB
/
dll.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
169
170
171
172
173
174
175
176
177
178
179
#include "dll.h"
#include <stdio.h>
#include <stdlib.h>
list_t *create_list() // return a newly created empty doubly linked list
{
// DO NOT MODIFY!!!
list_t *l = (list_t *)malloc(sizeof(list_t));
l->head = NULL;
l->tail = NULL;
l->size = 0;
return l;
}
void insert_front(list_t *list, int data) // TODO: inserts data to the beginning of the linked list
{
node_t *temp = malloc(sizeof(node_t));
temp->next = NULL;
temp->prev = NULL;
temp->data = data;
if (is_empty(list))
{
list->head = temp;
list->tail = temp;
}
else
{
node_t *first = list->head;
first->prev = temp;
temp->next = first;
list->head = temp;
}
list->size++;
}
void insert_back(list_t *list, int data) // TODO: inserts data to the end of the linked list
{
node_t *t = malloc(sizeof(node_t));
t->data = data;
t->next = NULL;
t->prev = NULL;
list->tail->next = t;
t->prev = list->tail;
list->tail = list->tail->next;
list->size++;
}
void insert_after(list_t *list, int data, int prev) // TODO: inserts data after the node with data “prev”. Do not insert or do anything if prev doesn't exist
{
node_t *t = malloc(sizeof(node_t));
t->data = data;
t->next = t->prev = NULL;
node_t *curr = list->head;
while (curr != NULL)
{
if (curr->data == prev)
{
if (curr == list->tail)
{
insert_back(list, data);
return;
}
t->prev = curr;
t->next = curr->next;
curr->next->prev = t;
curr->next = t;
return;
}
curr = curr->next;
}
list->size++;
}
void delete_front(list_t *list) // TODO: delete the start node from the linked list.
{
list->head = list->head->next;
free(list->head->prev);
list->size--;
}
void delete_back(list_t *list) // TODO: delete the end node from the linked list.
{
list->tail = list->tail->prev;
free(list->tail->next);
list->tail->next = NULL;
list->size--;
}
void delete_node(list_t *list, int data) // TODO: delete the node with “data” from the linked list.
{
node_t *curr = list->head;
if (is_empty(list))
return;
if (list->size == 1 && curr->data == data)
{
list->head = NULL;
list->tail = NULL;
return;
}
while (curr != NULL)
{
if (curr->data == data)
{
if (curr == list->head)
{
delete_front(list);
break;
}
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
free(curr);
list->size--;
return;
}
curr = curr->next;
}
}
node_t *search(list_t *list, int data) // TODO: returns the pointer to the node with “data” field. Return NULL if not found.
{
node_t *curr = list->head;
while (curr != NULL)
{
if (curr->data == data)
return (curr);
curr = curr->next;
}
return (NULL);
}
int is_empty(list_t *list) // return true or 1 if the list is empty; else returns false or 0
{
// DO NOT MODIFY!!!
return list->size == 0;
}
int size(list_t *list) // returns the number of nodes in the linked list.
{
// DO NOT MODIFY!!!
return list->size;
}
void delete_nodes(node_t *head) // helper
{
// DO NOT MODIFY!!!
if (head == NULL)
return;
delete_nodes(head->next);
free(head);
}
void delete_list(list_t *list) // free all the contents of the linked list
{
// DO NOT MODIFY!!!
delete_nodes(list->head);
free(list);
}
void display_list(list_t *list) // print the linked list by separating each item by a space and a new line at the end of the linked list.
{
// DO NOT MODIFY!!!
node_t *it = list->head;
while (it != NULL)
{
printf("%d ", it->data);
it = it->next;
}
printf("\n");
}