Skip to content

Commit 66b5d40

Browse files
committed
doubly list done
1 parent b8a2116 commit 66b5d40

File tree

1 file changed

+241
-14
lines changed

1 file changed

+241
-14
lines changed

c/LinkedList/doubly/list.c

+241-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,259 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
// Node structure
54
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;
99
} Node;
1010

11-
// Doubly Linked List structure
1211
typedef struct DoublyLinkedList {
13-
Node* head;
14-
Node* tail;
12+
Node *head;
13+
Node *tail;
1514
} DoublyLinkedList;
1615

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));
2033
newNode->data = data;
2134
newNode->next = NULL;
2235
newNode->prev = NULL;
36+
newNode->ref_count = 1;
2337
return newNode;
2438
}
2539

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));
2942
list->head = NULL;
3043
list->tail = NULL;
3144
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

Comments
 (0)