-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linked_list.c
219 lines (158 loc) · 4.68 KB
/
doubly_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
typedef struct Node
{
int data;
struct Node *prev, *next;
} node;
node* insert_front(node *root, int value)
{
// Create a temp node with the desired value.
// "prev" for the node is NULL because it is at the front of the list.
node *temp = (node *) malloc(sizeof(node));
temp->data = value;
temp->prev = NULL;
// Puts the temp node before the current root.
temp->next = root;
// If root has contents, then "makes space" for the new node to become the new root.
if(root!=NULL)
{
root->prev = temp;
}
// Finalizes the new root, whether it declares a value or inserts a new node.
root = temp;
return root;
}
node* insert_end(node* root, int value)
{
// The "conductor" node to traverse the list.
node *temp2;
// temp node holds the input info.
node *temp;
// Create a temp node that takes input value.
// temp->next is NULL because it is at the end of the list.
temp = (node *) malloc (sizeof(node));
temp->data = value;
temp->next = NULL;
// If linked list is empty, set the prev of the temp node as NULL and make that the root.
if(root==NULL)
{
temp->prev = NULL;
root = temp;
}
else
{
// Use temp2 as a "conductor" node to traverse the list from the first node.
temp2 = root;
// Travels to the end of the list.
while(temp2->next!=NULL) {
temp2 = temp2->next;
}
// Once the "conductor" node reaches the end, the new node is inserted at the end.
temp2->next = temp;
temp->prev = temp2;
}
return root;
}
node* delete_node(node* root, int value)
{
node *temp2;
node *temp;
if(root==NULL) {
return root;
}
// If the target is at the root.
if(root->data==value)
{
// Store the root in the temp node
temp = root;
// Make the node after the root the new node.
root = root->next;
// Make the prev of the new node (formerly the second node) NULL.
if(root != NULL)
{
root->prev = NULL;
// Free the original root and return the new and ready root.
free(temp);
return root;
}
}
// Set the "conductor" node to start at the beginning.
temp2 = root;
// Continue traversing the list until it reaches a node with the desired value.
while(temp2->next!=NULL && temp2->next->data != value)
{
temp2 = temp2->next;
}
// If the traversal is completed without detecting a node with the desired value, end the method.
if(temp2->next == NULL)
{
return root;
}
// If the execution reaches this point, a node with the value was found.
// temp node holds the node that the traversal node is on ("to be deleted" node).
temp = temp2->next;
// The node that will be deleted has its position replaced by the node after.
temp2->next = temp2->next->next;
// If the node after the to-be-deleted node is NOT NULL
if(temp2->next->next!=NULL)
{
// The "prev" of the node after the TBD pointer now points to the node before the TBD node.
temp2->next->next->prev = temp2->next->prev;
}
// If the node before the to-be-deleted node is NOT NULL
if(temp2->next->prev!=NULL)
{
// The "next" of the node before the to-be-deleted node now points to the node after the TBD node
temp2->next->prev->next = temp2->next->next;
}
free(temp);
return root;
}
void print(node* root)
{
if(root==NULL)
{
printf("The linked list is empty.\n");
return;
}
else {
printf("[%d]", root->data);
}
root = root->next;
while(root!=NULL)
{
printf(" - [%d]", root->data);
root = root->next;
}
printf("\n");
}
int main()
{
srand(time(0));
int i, value;
int inserted_front_value = 10;
int inserted_end_value = 20;
node* root = NULL;
for(i=0;i<5;i++)
{
root = insert_front(root, rand()%100 + 1);
}
print(root);
root = insert_front(root, inserted_front_value);
printf("\nAfter inserting %d into the front: \n", inserted_front_value);
print(root);
root = insert_end(root, inserted_end_value);
printf("\nAfter inserting %d into the end: \n", inserted_end_value);
print(root);
printf("\nEnter the node you want to delete: ");
scanf("%d", &value);
root = delete_node(root, value);
printf("\nJust deleted %d: \n", value);
print(root);
/*while(1)
{
}*/
return 0;
}