用c语言编写算法将一个顺序表A(有多个元素且任何元素不为0)分拆成两个顺序表,使A 中大于0的元素存放在B 中,小于0的元素存放在C 中。
时间: 2023-05-18 14:05:43 浏览: 90
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void split(SqList A, SqList *B, SqList *C) {
int i, j, k;
j = k = 0;
for (i = 0; i < A.length; i++) {
if (A.data[i] > 0) {
B->data[j++] = A.data[i];
} else {
C->data[k++] = A.data[i];
}
}
B->length = j;
C->length = k;
}
int main() {
SqList A = {{-1, 2, 3, -4, 5, -6}, 6};
SqList B = {{0}, 0};
SqList C = {{0}, 0};
split(A, &B, &C);
printf("B: ");
for (int i = 0; i < B.length; i++) {
printf("%d ", B.data[i]);
}
printf("\nC: ");
for (int i = 0; i < C.length; i++) {
printf("%d ", C.data[i]);
}
return 0;
}
阅读全文