scanf("d% d%", &(p1->coefficient), &(p1->exponent));
scanf 格式字符串應為 "%d %d",寫成 "d% d%" 導致無法匹配輸入,緩衝區數據不被消費,循環條件 p1->coefficient != -1 永遠滿足 (未讀取到新值),進入死循環/死機。
scanf("%d %d", &(p1->coefficient), &(p1->exponent));
#include 缺少頭文件名 (應為 stdio.h、stdlib.h/malloc.h)。malloc 返回值未檢查。printf("d% d%") 同樣格式錯誤。while(head != null) 應為 while(p != null),否則無限循環。#include <stdio.h>
#include <stdlib.h>
typedef struct Faction {
int coefficient;
int exponent;
struct Faction *next;
} Faction;
Faction* creat() {
Faction *head = NULL, *tail = NULL, *p;
int coef, exp;
while (scanf("%d %d", &coef, &exp) == 2) {
if (coef == -1 && exp == -1) break;
p = (Faction*)malloc(sizeof(Faction));
if (!p) { perror("malloc"); exit(1); }
p->coefficient = coef;
p->exponent = exp;
p->next = NULL;
if (!head) head = p;
else tail->next = p;
tail = p;
}
return head;
}
void Print(Faction *head) {
Faction *p = head;
while (p) {
printf("%d %d\n", p->coefficient, p->exponent);
p = p->next;
}
}