There is a serious problem with my code. What should I do? Can you help me modify it?
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
typedef struct Llist *List;
struct Llist{
int data;
struct Llist *next;
};
List readL(int num);
void attach(int num, List* rear);
List crt_pare(List a, List b);
int compare(int a, int b);
void print_result(List P);
int main()
{
int n, m;
List PtrL1, PtrL2, PtrL3;
while(scanf("%d%d", &n, &m), n && m){
PtrL1 = readL(n);
PtrL2 = readL(m);
PtrL3 = crt_pare(PtrL1, PtrL2);
print_result(PtrL3);
}
return 0;
}
List readL(int num)
{
List ptrL, rearL, temp;
int r_n;
ptrL = (List)malloc(sizeof(struct Llist));
ptrL ->next = NULL;
rearL = ptrL;
while(num--){
scanf("%d", &r_n);
attach(r_n, &rearL);
}
temp = ptrL;
ptrL = ptrL->next;
free(temp);
return ptrL;
}
List crt_pare(List a, List b)
{
List c_1, c_2, c_3, c_temp;
int a_flag = 0, b_flag = 0;
List PtrL;
c_1 = a; c_2 = b;
c_3 = (List)malloc(sizeof(struct Llist));
c_3 ->next = NULL;
PtrL = c_3;
while(c_1 || c_2){
if(c_1 ->next)
a_flag = 1;
if(c_2 ->next)
b_flag = 1;
if(a_flag >0 && b_flag >0)
a_flag = 0; b_flag = 0;
switch(compare(c_1 ->data, c_2 ->data)){
case 1: attach(c_2 ->data, &PtrL);
if(b_flag == 0)
c_2 = c_2 ->next;
else
c_2 ->data = INT_MAX;
break;
case 0: attach(c_1 ->data, &PtrL);
if(a_flag == 0)
c_1 = c_1 ->next;
else
c_1 ->data = INT_MAX;
break;
}
if(a_flag >0 && b_flag >0)
break;
}
c_temp = c_3;
c_3 = c_3 ->next;
free(c_3);
return c_3;
}
void attach(int num, List* prear){
List PtrL;
PtrL = (List)malloc(sizeof(struct Llist));
PtrL ->next = NULL;
PtrL ->data = num;
(*prear) ->next = PtrL;
*prear = PtrL;
}
int compare(int a, int b)
{
if(a >= b)
return 1;
else
return 0;
}
void print_result(List P)
{
if (!P)
printf("^v^!!!, error, <..>");
while(P){
printf("%d ", P->data);
P = P ->next;
}
printf("n");
}
Input
Multiple sets of data, each with three rows; the first row has n and m lengths for sequences A and B, the second row has n elements for A, and the third row has m elements for B (elements separated by Spaces). The input ends when n=0 and m=0.
Output
For each set of data output a row, for the merged sequence, each data separated by a space.
For example, enter Copy
5 5
1, 3, 5, 7, 9
2, 4, 6, 8, 10
5 6
1, 2, 2, 3, 5
2, 4, 6, 8, 10, 12
0 0
The example outputs Copy
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 2, 2, 3, 4, 5, 6, 8, 10, 12
user24504088 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.