-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_dll.c
More file actions
168 lines (139 loc) · 3.18 KB
/
Copy path10_dll.c
File metadata and controls
168 lines (139 loc) · 3.18 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
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
struct node *prev;
} Node;
typedef struct dll
{
Node *head;
Node *tail;
} DLL;
Node *create_node(int data)
{
Node *new_node = malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
}
DLL *insertAfterPosition(DLL *list, int position, int data)
{
Node *new_node = create_node(data);
if (list->head == NULL)
{
list->head = new_node;
list->tail = new_node;
return list;
}
int i = 0;
Node *curr = list->head;
while (curr != NULL && i < position)
{
i++;
curr = curr->next;
}
// Add to end
if (curr == NULL)
{
list->tail->next = new_node;
new_node->prev = list->tail;
list->tail = new_node;
}
else
{
Node *prev = curr->prev;
if (prev != NULL)
{
prev->next = new_node;
}
else
{ // Index 0
list->head = new_node;
}
new_node->prev = prev;
new_node->next = curr;
curr->prev = new_node;
}
return list;
}
DLL *sort(DLL *list)
{
if (list->head == NULL)
{
return list;
}
Node *head = list->head;
while (head != NULL)
{
Node *head2 = list->head;
while (head2 != NULL)
{
if (head->data > head2->data)
{
int temp = head->data;
head->data = head2->data;
head2->data = temp;
}
head2 = head2->next;
}
head = head->next;
}
return list;
}
DLL *concatenate(DLL *l, DLL *l1, DLL *l2)
{
l1->tail->next = l2->head;
l2->head->prev = l1->tail;
l->head = l1->head;
l->tail = l2->tail;
return l;
}
void display(DLL *list)
{
Node *head = list->head;
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main(void)
{
DLL *list = (DLL *)malloc(sizeof(DLL));
list->head = NULL;
list->tail = NULL;
list = insertAfterPosition(list, 0, 10);
list = insertAfterPosition(list, 1, 15);
list = insertAfterPosition(list, 2, 40);
list = insertAfterPosition(list, 3, 42);
list = insertAfterPosition(list, 1, 20); // Testing insertion in the middle
printf("Original List: ");
display(list);
list = sort(list);
printf("Sorted List: ");
display(list);
DLL *list1 = (DLL *)malloc(sizeof(DLL));
list1->head = NULL;
list1->tail = NULL;
list1 = insertAfterPosition(list1, 0, 31);
list1 = insertAfterPosition(list1, 1, 31);
list1 = insertAfterPosition(list1, 2, 34);
list1 = insertAfterPosition(list1, 3, 34);
list1 = insertAfterPosition(list1, 1, 32); // Testing insertion in the middle
printf("Original List: ");
display(list1);
DLL *conc = (DLL *)malloc(sizeof(DLL));
conc->head = NULL;
conc->tail = NULL;
conc = concatenate(conc, list, list1);
printf("Concatenated list: ");
display(conc);
printf("Sorted list: ");
conc = sort(conc);
display(conc);
return 0;
}