-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly.c
More file actions
164 lines (127 loc) · 3.42 KB
/
doubly.c
File metadata and controls
164 lines (127 loc) · 3.42 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
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev; // New pointer for DLL
};
void insertAtStart(int data, struct Node **head) {
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->prev = NULL;
newnode->next = *head;
if (*head != NULL) {
(*head)->prev = newnode; // Link old head back to new node
}
*head = newnode;
}
void insertAtEND(int data, struct Node **head) {
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
if (*head == NULL) {//incase list is empty it creates the list's first element
newnode->prev = NULL;
*head = newnode;
return;
}
struct Node *temp = *head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
newnode->prev = temp; // Link new node back to old last node
}
void insertAtPosition(int data, struct Node **head, int index) {
if (index == 0) {
insertAtStart(data, head);
return;
}
struct Node *temp = *head;
int counter = 0;
while(temp != NULL && counter < index - 1) {
temp = temp->next;
counter++;
}
if (temp == NULL) return; // Index out of range
struct Node *newnode = malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = temp->next;
newnode->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newnode; // Link succeeding node back to new node
}
temp->next = newnode;
}
void deleteAtFront(struct Node **head) {
if (*head == NULL) return;
struct Node *old = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL; // New head has no previous
}
free(old);
}
void deleteAtEnd(struct Node **head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node *temp = *head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL; // The second-to-last node now points to NULL
free(temp);
}
void deleteAtIndex(struct Node **head, int index) {
if (*head == NULL) return;
if (index == 0) {
deleteAtFront(head);
return;
}
struct Node *temp = *head;
int counter = 0;
while(temp != NULL && counter < index) {
temp = temp->next;
counter++;
}
if (temp == NULL) return; // Index out of range
// Unlinking the node
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}
void display(struct Node *head) {
struct Node *temp = head;
printf("Forward: ");
while(temp != NULL) {
printf("%d -> ", temp->data);
if (temp->next == NULL) break;
temp = temp->next;
}
printf("NULL\n");
// Proving it's a DLL by traversing backward
printf("Backward: ");
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
insertAtEND(20, &head);
insertAtEND(30, &head);
insertAtEND(40, &head);
insertAtStart(10, &head);
insertAtPosition(25, &head, 2);
display(head);
deleteAtIndex(&head, 2); // Removes 25
printf("\nAfter deleting index 2:\n");
display(head);
return 0;
}