该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
//结构体
typedef struct linklist {
int data;
linklist *next;
} *LINKLISTP, LINKLIST;
//头
LINKLISTP llhead;
//在某个节点插入
int linklist_cha(LINKLISTP * current, int t) {
printf("试着插了!\n");
LINKLISTP newll = (LINKLISTP) malloc(sizeof(LINKLIST));
if (newll == NULL) {
printf("申请空间失败");
}
newll->next = NULL;
newll->data = t;
if (*current == NULL) {
printf("当前为空,插入!\n");
*current = newll;
} else {
printf("当前不为空,插入!\n");
if (*current == llhead && t < llhead->data) {
printf("换头,插入!\n");
newll->next = llhead;
llhead = newll;
}
else {if ((*current)->next != NULL) {
printf("下一个不为空,插入!\n");
LINKLISTP templl = (*current)->next;
newll->next = templl;
}
printf("下一个为空,插入!\n");
(*current)->next = newll;}
}
}
//判断往哪插入
LINKLISTP linklist_add(int t, LINKLISTP * LLP) {
// LINKLISTP templl = LLP;
if (*LLP == NULL) {
printf("linklist_add判断为空\n");
linklist_cha(LLP, t);
// return *LLP;
} else {
if ((*LLP)->next == NULL || t < (*LLP)->next->data) {
printf("linklist_add判断比下一个小\n");
linklist_cha(LLP, t);
} else {
printf("linklist_add判断比下一个大\n");
linklist_add(t, &((*LLP)->next));
}
}
}
//判断是否素数
int is_prime(int t) {
if (t < 1)
return 0;
if (t == 1 || t == 2)
return 1;
for (int i = t - 1; i > 1; i--) {
if (t % i == 0) {
return 0;
}
}
return 1;
}
//历遍输出
int traverseLinkList(LINKLISTP LLP) {
if (LLP != NULL) {
printf("一个:");
printf("%i ", LLP->data);
traverseLinkList(LLP->next);
}
}
int main() {
int temp;
int go = 1;
while (go == 1) {
printf("请输入第一个结点的值:");
scanf("%i", &temp);
if (is_prime(temp) == 1) {
// llhead = linklist_add(temp, llhead);
linklist_add(temp, &llhead);
} else {
go = 0;
printf("不是素数!\n");
traverseLinkList(llhead);
}
}
}