1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
4
- // Node structure
5
4
typedef struct Node {
6
- int data ;
7
- struct Node * next ;
8
- struct Node * prev ;
5
+ void * data ;
6
+ struct Node * next ;
7
+ struct Node * prev ;
8
+ int ref_count ;
9
9
} Node ;
10
10
11
- // Doubly Linked List structure
12
11
typedef struct DoublyLinkedList {
13
- Node * head ;
14
- Node * tail ;
12
+ Node * head ;
13
+ Node * tail ;
15
14
} DoublyLinkedList ;
16
15
17
- // Function to create a new node
18
- Node * createNode (int data ) {
19
- Node * newNode = (Node * )malloc (sizeof (Node ));
16
+ void incrementRefCount (Node * node ) {
17
+ if (node ) {
18
+ node -> ref_count ++ ;
19
+ }
20
+ }
21
+
22
+ void decrementRefCount (Node * node ) {
23
+ if (node ) {
24
+ node -> ref_count -- ;
25
+ if (node -> ref_count <= 0 ) {
26
+ free (node );
27
+ }
28
+ }
29
+ }
30
+
31
+ Node * createNode (void * data ) {
32
+ Node * newNode = (Node * )malloc (sizeof (Node ));
20
33
newNode -> data = data ;
21
34
newNode -> next = NULL ;
22
35
newNode -> prev = NULL ;
36
+ newNode -> ref_count = 1 ;
23
37
return newNode ;
24
38
}
25
39
26
- // Function to initialize an empty list
27
- DoublyLinkedList * createList () {
28
- DoublyLinkedList * list = (DoublyLinkedList * )malloc (sizeof (DoublyLinkedList ));
40
+ DoublyLinkedList * createDoublyLinkedList () {
41
+ DoublyLinkedList * list = (DoublyLinkedList * )malloc (sizeof (DoublyLinkedList ));
29
42
list -> head = NULL ;
30
43
list -> tail = NULL ;
31
44
return list ;
32
- }
45
+ }
46
+
47
+ Node * getHead (DoublyLinkedList * list ) {
48
+ return list -> head ;
49
+ }
50
+
51
+ Node * getTail (DoublyLinkedList * list ) {
52
+ return list -> tail ;
53
+ }
54
+
55
+ void prepend (DoublyLinkedList * list , void * data ) {
56
+ Node * newNode = createNode (data );
57
+ if (!list -> head ) {
58
+ list -> head = newNode ;
59
+ list -> tail = newNode ;
60
+ } else {
61
+ newNode -> next = list -> head ;
62
+ list -> head -> prev = newNode ;
63
+ list -> head = newNode ;
64
+ }
65
+ }
66
+
67
+ void append (DoublyLinkedList * list , void * data ) {
68
+ Node * newNode = createNode (data );
69
+ if (!list -> head ) {
70
+ list -> head = newNode ;
71
+ list -> tail = newNode ;
72
+ } else {
73
+ newNode -> prev = list -> tail ;
74
+ list -> tail -> next = newNode ;
75
+ list -> tail = newNode ;
76
+ }
77
+ }
78
+
79
+ void insertBetween (DoublyLinkedList * list , void * data , void * after ) {
80
+ Node * newNode = createNode (data );
81
+ Node * curr = list -> head ;
82
+
83
+ while (curr && curr -> data != after ) {
84
+ curr = curr -> next ;
85
+ }
86
+
87
+ if (curr ) {
88
+ newNode -> next = curr -> next ;
89
+ newNode -> prev = curr ;
90
+ if (curr -> next ) {
91
+ curr -> next -> prev = newNode ;
92
+ } else {
93
+ list -> tail = newNode ;
94
+ }
95
+ curr -> next = newNode ;
96
+ }
97
+ }
98
+
99
+ void deleteHead (DoublyLinkedList * list ) {
100
+ if (list -> head ) {
101
+ Node * temp = list -> head ;
102
+ list -> head = list -> head -> next ;
103
+ if (list -> head ) {
104
+ list -> head -> prev = NULL ;
105
+ } else {
106
+ list -> tail = NULL ;
107
+ }
108
+ decrementRefCount (temp );
109
+ }
110
+ }
111
+
112
+ void deleteTail (DoublyLinkedList * list ) {
113
+ if (!list -> tail ) {
114
+ return ;
115
+ }
116
+ if (list -> tail == list -> head ) {
117
+ decrementRefCount (list -> tail );
118
+ list -> head = NULL ;
119
+ list -> tail = NULL ;
120
+ } else {
121
+ Node * temp = list -> tail ;
122
+ list -> tail = list -> tail -> prev ;
123
+ list -> tail -> next = NULL ;
124
+ decrementRefCount (temp );
125
+ }
126
+ }
127
+
128
+ void deleteAny (DoublyLinkedList * list , void * target ) {
129
+ Node * curr = list -> head ;
130
+
131
+ while (curr && curr -> data != target ) {
132
+ curr = curr -> next ;
133
+ }
134
+
135
+ if (curr ) {
136
+ if (curr -> prev ) {
137
+ curr -> prev -> next = curr -> next ;
138
+ } else {
139
+ list -> head = curr -> next ;
140
+ }
141
+
142
+ if (curr -> next ) {
143
+ curr -> next -> prev = curr -> prev ;
144
+ } else {
145
+ list -> tail = curr -> prev ;
146
+ }
147
+
148
+ decrementRefCount (curr );
149
+ }
150
+ }
151
+
152
+ int size (DoublyLinkedList * list ) {
153
+ int count = 0 ;
154
+ Node * curr = list -> head ;
155
+ while (curr ) {
156
+ count ++ ;
157
+ curr = curr -> next ;
158
+ }
159
+ return count ;
160
+ }
161
+
162
+ void reverseList (DoublyLinkedList * list ) {
163
+ Node * curr = list -> head ;
164
+ Node * temp = NULL ;
165
+
166
+ while (curr ) {
167
+ temp = curr -> prev ;
168
+ curr -> prev = curr -> next ;
169
+ curr -> next = temp ;
170
+ curr = curr -> prev ;
171
+ }
172
+
173
+ if (temp ) {
174
+ list -> head = temp -> prev ;
175
+ }
176
+ }
177
+
178
+ Node * find (DoublyLinkedList * list , void * data ) {
179
+ Node * curr = list -> head ;
180
+ while (curr ) {
181
+ if (curr -> data == data ) {
182
+ return curr ;
183
+ }
184
+ curr = curr -> next ;
185
+ }
186
+ return NULL ;
187
+ }
188
+
189
+ void clear (DoublyLinkedList * list ) {
190
+ Node * curr = list -> head ;
191
+ Node * next ;
192
+ while (curr ) {
193
+ next = curr -> next ;
194
+ decrementRefCount (curr );
195
+ curr = next ;
196
+ }
197
+ list -> head = NULL ;
198
+ list -> tail = NULL ;
199
+ }
200
+
201
+ void printListForward (DoublyLinkedList * list ) {
202
+ Node * curr = list -> head ;
203
+ while (curr ) {
204
+ printf ("%p " , curr -> data );
205
+ curr = curr -> next ;
206
+ }
207
+ printf ("\n" );
208
+ }
209
+
210
+ void printListBackward (DoublyLinkedList * list ) {
211
+ Node * curr = list -> tail ;
212
+ while (curr ) {
213
+ printf ("%p " , curr -> data );
214
+ curr = curr -> prev ;
215
+ }
216
+ printf ("\n" );
217
+ }
218
+
219
+ int main () {
220
+ DoublyLinkedList * list = createDoublyLinkedList ();
221
+
222
+ int a = 1 , b = 2 , c = 3 , d = 4 ;
223
+ append (list , & a );
224
+ append (list , & b );
225
+ prepend (list , & c );
226
+ insertBetween (list , & d , & a );
227
+
228
+ printf ("List printed forward: " );
229
+ printListForward (list );
230
+
231
+ printf ("List printed backward: " );
232
+ printListBackward (list );
233
+
234
+ deleteHead (list );
235
+ printf ("List after deleting head: " );
236
+ printListForward (list );
237
+
238
+ deleteTail (list );
239
+ printf ("List after deleting tail: " );
240
+ printListForward (list );
241
+
242
+ deleteAny (list , & b );
243
+ printf ("List after deleting element b: " );
244
+ printListForward (list );
245
+
246
+ printf ("List size: %d\n" , size (list ));
247
+
248
+ reverseList (list );
249
+ printf ("List after reversing: " );
250
+ printListForward (list );
251
+
252
+ clear (list );
253
+ printf ("List after clearing: " );
254
+ printListForward (list );
255
+
256
+ free (list );
257
+
258
+ return 0 ;
259
+ }
0 commit comments