C语言数据结构问题,求高手解答下,谢谢了

2024-11-22 19:49:10
推荐回答(1个)
回答(1):

if (*a = NULL)
改成
if (*a == NULL)
即可。

#include
#include
typedef struct node {
int x;
struct node *next;
}NODE;

void input(NODE **a) {
NODE *p, *q;
int i;
printf("输入链表的元素,0表示结束\n");
*a = NULL;
while (1) {
scanf("%d", &i);
if (i == 0)
break;
p = (NODE*)malloc(sizeof(NODE));
p->x = i;
p->next = NULL;
if (*a == NULL) //这里==============
* a = q = p;
else {
q->next = p;
q = q->next;
}
}
}

void output(NODE *a) {
int i;
for (i = 0; a != NULL; i++, a = a->next) {
printf("%7d", a->x);
if ((i + 1) % 10 == 0)
printf("\n");
}
printf("\n");
}

void disa(NODE *a, NODE **b) {
NODE *r, *p, *q;
p = a;
r = *b = (a == NULL) ? NULL : a->next;
while (p != NULL && p->next != NULL) {
q = p->next;
p->next = q->next;
r->next = q;
r = q;
p = p->next;
}
r->next = NULL;
}

void main() {
NODE *a, *b;
input(&a);
printf("链表A的元素为:\n");
output(a);
disa(a, &b);
printf("链表A的元素(奇数序号节点)为:\n");
output(a);
printf("链表B的元素(偶数序号节点)为:\n");
output(b);
}