-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly_linklist.c
152 lines (125 loc) · 2.87 KB
/
doubly_linklist.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
//
// Created by moezgen on 12/20/24.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
struct node *prev;
} node_t;
typedef node_t *ListNodePtr;
void insertAtEnd(ListNodePtr *head, int data) {
ListNodePtr newNode = (ListNodePtr)malloc(sizeof(node_t));
if (newNode == NULL) {
printf("Memory allocation failed in insertAtEnd\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
ListNodePtr tmp = *head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newNode;
newNode->prev = tmp;
}
void insertAtStart(ListNodePtr *head, int data) {
ListNodePtr newNode = (ListNodePtr)malloc(sizeof(node_t));
if (newNode == NULL) {
printf("Memory allocation failed in insertAtStart\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
void insertAfter(ListNodePtr *head, int dataAfter, int data) {
if (*head == NULL) {
printf("List is empty, cannot insert after %d\n", dataAfter);
return;
}
ListNodePtr tmp = *head;
while (tmp != NULL && tmp->data != dataAfter) {
tmp = tmp->next;
}
if (tmp == NULL) {
printf("Node with data %d not found\n", dataAfter);
return;
}
ListNodePtr newNode = (ListNodePtr)malloc(sizeof(node_t));
if (newNode == NULL) {
printf("Memory allocation failed in insertAfter\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = tmp->next;
if (tmp->next != NULL) {
tmp->next->prev = newNode;
}
newNode->prev = tmp;
tmp->next = newNode;
}
void deleteAtEnd(ListNodePtr *head) {
if (*head == NULL) {
printf("List is empty, nothing to delete\n");
return;
}
ListNodePtr tmp = *head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
if (tmp->prev == NULL) {
*head = NULL;
} else {
tmp->prev->next = NULL;
}
free(tmp);
}
void deleteAtStart(ListNodePtr *head) {
ListNodePtr tmp = *head;
if (tmp == NULL) {
printf("Memory allocation failed in deleteAtStart\n");
exit(EXIT_FAILURE);
}
*head = (*head)->next;
(*head)->prev = NULL;
free(tmp);
}
void display(ListNodePtr *head) {
ListNodePtr tmp = *head;
if (tmp == NULL) {
printf("Memory allocation failed in display\n");
exit(EXIT_FAILURE);
}
while (tmp != NULL) {
printf("%d -> ", tmp->data);
tmp = tmp->next;
}
printf("NULL\n");
}
int main() {
ListNodePtr list = NULL;
insertAtEnd(&list, 1);
insertAtStart(&list, 2);
insertAtEnd(&list, 3);
insertAtEnd(&list, 4);
insertAtEnd(&list, 5);
insertAtEnd(&list, 6);
insertAfter(&list, 4, 10);
insertAtEnd(&list, 7);
deleteAtEnd(&list);
display(&list);
}