-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem26.c
More file actions
114 lines (97 loc) Β· 2.49 KB
/
problem26.c
File metadata and controls
114 lines (97 loc) Β· 2.49 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
#include <stdio.h>
#include <stdlib.h>
// Define structure for a doubly linked list node
typedef struct DListNode {
int value;
struct DListNode* prev;
struct DListNode* next;
} DListNode;
// Create a new node with the given value
DListNode* createNode(int value) {
DListNode* newNode = (DListNode*)malloc(sizeof(DListNode));
newNode->value = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Append a new node to the end of the list
void appendNode(DListNode** head, DListNode** tail, int value) {
DListNode* newNode = createNode(value);
if (*head == NULL) {
*head = *tail = newNode;
} else {
(*tail)->next = newNode;
newNode->prev = *tail;
*tail = newNode;
}
}
// Swap the values of two nodes
void swapNodes(DListNode* a, DListNode* b) {
int temp = a->value;
a->value = b->value;
b->value = temp;
}
// Bubble sort to sort the doubly linked list
void sortList(DListNode* head) {
if (!head) return;
int swapped;
DListNode* current;
DListNode* lastSorted = NULL;
do {
swapped = 0;
current = head;
while (current->next != lastSorted) {
if (current->value > current->next->value) {
swapNodes(current, current->next);
swapped = 1;
}
current = current->next;
}
lastSorted = current;
} while (swapped);
}
// Print the doubly linked list in <-> format
void printList(DListNode* head) {
while (head) {
printf("%d", head->value);
if (head->next) {
printf(" <-> ");
}
head = head->next;
}
printf("\n");
}
// Free the memory allocated for the list
void freeList(DListNode* head) {
while (head) {
DListNode* temp = head;
head = head->next;
free(temp);
}
}
int main() {
int n;
printf("Enter the number of nodes: ");
if (scanf("%d", &n) != 1 || n < 1) {
printf("Invalid number of nodes.\n");
return 0;
}
DListNode* head = NULL;
DListNode* tail = NULL;
printf("Enter %d space-separated integers:\n", n);
for (int i = 0; i < n; i++)
{
int value;
if (scanf("%d", &value) != 1) {
printf("Invalid input. Exiting.\n");
freeList(head);
return 0;
}
appendNode(&head, &tail, value);
}
sortList(head);
printf("Sorted output: ");
printList(head);
freeList(head);
return 0;
}