-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempCodeRunnerFile.c
138 lines (121 loc) · 2.45 KB
/
tempCodeRunnerFile.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
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int elem;
struct node *link;
} * List;
List populateList(int A[], int size)
{
List L, *trav;
int x;
L = NULL;
trav = &L;
for (x = 0; x < size; x++)
{
*trav = (List)calloc(1, sizeof(struct node));
if (*trav != NULL)
{
(*trav)->elem = A[x];
trav = &(*trav)->link;
}
}
return L;
}
void displayList(List L, char msg[])
{
printf("\n%s:", msg);
if (L == NULL)
{
printf(" Empty");
}
else
{
while (L != NULL)
{
printf(" %d,", L->elem);
L = L->link;
}
}
}
List createSubList(List *A)
{
List *trav, head, trav2;
if (*A != NULL)
{
int large;
head = trav2 = *A;
*A = head->link;
large = head->elem;
for (trav = A; *trav != NULL;)
{
if (large < (*trav)->elem)
{
large = (*trav)->elem;
trav2->link = *trav;
*trav = (*trav)->link;
trav2 = trav2->link;
}
else
{
trav = &(*trav)->link;
}
}
trav2->link = NULL;
}
return head;
}
void merge2Lists(List *out, List *in)
{
List *outTrav, *inTrav, hold;
outTrav = out;
inTrav = in;
while (*inTrav != NULL)
{
if ((*outTrav)->elem > (*inTrav)->elem)
{
hold = *inTrav;
*inTrav = (*inTrav)->link;
hold->link = *outTrav;
*outTrav = hold;
}
outTrav = &(*outTrav)->link;
}
}
int main()
{
int arr[] = {10, 5, 30, 40, 2, 4, 9};
// int arr[] = {21, 15, 1, 6, 9, 0, 17, 100, 20, 5, 33, 65, 2, 10, 89};
int num = sizeof(arr) / sizeof(int);
// task 1
printf("Task 1");
List original = populateList(arr, num);
displayList(original, "Original");
List sublist;
// printf("\n\nTask 2");
// while (original != NULL)
// {
// sublist = createSubList(&original);
// displayList(sublist, "Sublist");
// displayList(original, "Original");
// }
List outlist = NULL;
// printf("\n\nTask 3");
// sublist = createSubList(&original);
// merge2Lists(&original, &sublist);
// displayList(sublist, "Sublist");
// displayList(original, "Outlist");
// displayList(original, "Original");
printf("\n\nTask 4");
while (original != NULL)
{
displayList(original, "Original");
sublist = createSubList(&original);
displayList(original, "Original");
displayList(sublist, "SubList");
merge2Lists(&outlist, &sublist);
displayList(outlist, "Output");
}
// displayList(sublist, "sorted");
return 0;
}