-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascll3.c
84 lines (69 loc) · 1.91 KB
/
ascll3.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
#include<stdio.h>
#include<stdlib.h>
// Definition of the node structure
typedef struct node {
int data;
struct node *next;
} node;
// Function to create a new node and insert it at the beginning of the linked list
void createnode(node **head) {
int data;
node* newnode = (node*)malloc(sizeof(node));
if (newnode == NULL) { // Check for malloc failure
printf("Memory allocation failed!\n");
return;
}
printf("Enter value: ");
scanf("%d", &data);
newnode->data = data;
newnode->next = *head; // Point new node to the current head
*head = newnode; // Move head to point to the new node
}
// Function to sort the linked list in ascending order using data swapping
void sortAscending(node **head) {
if (*head == NULL) {
printf("Empty linked list.\n");
return;
}
node *p, *q;
int temp;
// Bubble sort algorithm to sort the linked list
for (p = *head; p != NULL; p = p->next) {
for (q = p->next; q != NULL; q = q->next) {
if (p->data > q->data) {
// Swap data between nodes p and q
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
}
// Function to print the linked list
void printll(node *head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
printf("Linked list: ");
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
int num = 1;
node *head = NULL;
// Keep inserting nodes while the user wants
while (num) {
createnode(&head);
printf("Want to insert more? Type 1 for yes, 0 for no: ");
scanf("%d", &num);
}
// Sort the list in ascending order
sortAscending(&head);
// Print the sorted linked list
printll(head);
return 0;
}