-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge 2 sorted linked lists.cpp
168 lines (141 loc) · 2.89 KB
/
merge 2 sorted linked lists.cpp
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
/* 2.Merge two linked list which are in sorted order */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node *merge(struct node *head1, struct node *head2);
struct node *Array_to_linked_list(int *arr, int len);
void dotest();
int checkoutput(int );
struct test
{
int input1[100];
int len1;
int input2[100];
int len2;
int output[200];
int len3;
}testcases[8] = {
/*1*/ { { 1, 3, 5 }, 3, { 2, 4, 6 }, 3, { 1, 2, 3, 4, 5, 6 }, 6 },
/*2*/ { { 1, 2, 3 }, 3, { 4, 5, 6, 7, 8 }, 5, { 1, 2, 3, 4, 5, 6, 7, 8 }, 8 },
/*3*/ { { 4, 5, 6 }, 3, { 1, 2, 3 }, 3, { 1, 2, 3, 4, 5, 6 }, 6 },
/*4*/ { { 2, 4, 6 }, 3, { 1, 7, 9 }, 3, { 1, 2, 4, 6, 7, 9 }, 6 },
/*5*/ { { 1, 1, 1, 1, 1 }, 5, { 1, 1, 1 }, 3, { 1, 1, 1, 1, 1, 1, 1, 1 }, 8 },
/*6*/ { NULL, 1, { 1 }, 1, { 1 }, 1 },
/*7*/ { { 1 }, 1, NULL, 1, { 1 }, 1 },
/*8*/ { NULL, 1, NULL, 1, NULL, 1 }
};
struct node
{
int data;
struct node *next;
};
struct node * createNode(int n1) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data= n1;
newNode->next = NULL;
return newNode;
}
void add(struct node **head, int p){
struct node *temp = *head;
if (*head == NULL){
(*head) = createNode(p);
(*head)->next = NULL;
}
else{
while (temp->next != NULL){
temp = temp->next;
}
temp->next = createNode(p);
temp->next->next = NULL;
}
}
void main()
{
dotest();
getch();
}
void dotest()
{
int i, count = 8;
for (i = 0; i < count; i++)
{
if (checkoutput(i) == 1)
printf("TESTCASE %d Valid\n", i + 1);
else
printf("TESTCASE %d Valid\n", i + 1);
}
}
int checkoutput(int i)
{
int j, count = 2, flag = 1;
struct node *head, *temp;
struct node *ptr1=Array_to_linked_list(testcases[i].input1, testcases[i].len1);
struct node *ptr2=Array_to_linked_list(testcases[i].input2, testcases[i].len2);
head = merge(ptr1,ptr2);
for (j = 0, temp = head; j < testcases[i].len3; j++, temp = temp->next)
{
if (testcases[i].output[j] != temp->data)
{
flag = 0;
break;
}
}
return flag;
}
struct node *Array_to_linked_list(int *arr, int len)
{
int j;
struct node *temp = (struct node*)malloc(sizeof(struct node)), *head = NULL;
if (arr == NULL || len == 0)
return NULL;
else
{
temp->next = NULL;
for (j = 0; j <=len-1; j++)
{
add(&head,arr[j]);
}
return head;
}
}
struct node *merge(struct node *head1, struct node *head2)
{
struct node *i = head1, *j = head2, *temp, *first, *temp2;
if (i == NULL)
return j;
else if (j == NULL)
return i;
else
{
if (i->data > j->data)
{
first = j;
j = i;
i = first;
}
else
first = i;
while (1)
{
if (i->next->data > j->data)
{
temp = i->next;
i->next = j;
temp2 = j->next;
j->next = temp;
i = j;
j = temp2;
}
else
i = i->next;
if (i->next == NULL)
{
i->next = j;
break;
}
if (j == NULL)
break;
}
return first;
}
}