单链表求范围内素数c语言,求问大神一道链表题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值