-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml.c
107 lines (102 loc) · 2.12 KB
/
ml.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE f1 = NULL, f2 = NULL;
NODE res = NULL;
NODE getnode()
{
NODE new;
new = (NODE)malloc(sizeof(struct node));
if (new == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
printf("Enter the element: ");
scanf("%d", &new->info);
new->link = NULL;
return new;
}
void insert_rear(NODE *f)
{
NODE cur, temp;
temp = getnode();
if (*f == NULL)
{
*f = temp;
}
else
{
cur = *f;
while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
}
}
void display(NODE f)
{
NODE cur;
if (f == NULL)
{
printf("\nLinked list empty\n");
return;
}
cur = f;
printf("\nElements of linked list are: \n");
while (cur != NULL)
{
printf("%d\t", cur->info);
cur = cur->link;
}
printf("\n");
}
void main()
{
int n1, n2;
printf("Enter the number of nodes in first Linked List: ");
scanf("%d", &n1);
for (int i = 0; i < n1; i++)
{
insert_rear(&f1);
}
printf("Linked List 1 created\n");
printf("Enter the number of nodes in second Linked List: ");
scanf("%d", &n2);
for (int i = 0; i < n2; i++)
{
insert_rear(&f2);
}
printf("Linked List 2 created\n");
NODE cur = f1;
while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = f2;
n1 = n1 + n2;
for (int i = 0; i < n1 - 1; i++)
{
NODE cur1 = f1;
NODE cur2 = f1->link;
for (int j = 0; j < n1 - i - 1; j++)
{
if (cur2->info < cur1->info)
{
int temp = cur1->info;
cur1->info = cur2->info;
cur2->info = temp;
}
cur1 = cur1->link;
cur2 = cur2->link;
}
}
printf("Linked lists are concatenated successfully and sorted\n");
display(f1);
}