用dev写的,题目要求是要创建一个有序递增的链表,然后插入一个数据后仍然有序。
点了编译和运行,就只出现0错误0警告那个框,但就是不运行。代码如下:
#include"stdio.h"
#include"stdlib.h"
typedef struct node{
int data;
struct node *next;
}node;
void Linklist(node *L){
L=(node *)malloc(sizeof(node));
L->next=NULL;
}
node *insert(node *L){
node *s,*r;
int i;
L->next=NULL;
r=L;
for(i=0;i<10;i=i+2){
s=(node *)malloc(sizeof(node));
s->data=i+1;
r->next=s;
r=s;
}
r->next=NULL;
return L;
}
node *output(node *L){
node *p=L->next;
while(p!=NULL){
printf("%d",p->data);
p=p->next;
}
return L;
}
void add(node *L,int e){
node *p=L,*s;
s=(node *)malloc(sizeof(node));
while(p!=NULL){
if(p->data<=e){
s->data=e;
s->next=p->next;
p->next=s;
}
else p=p->next;
}
}
int main(){
node L;
int e;
Linklist(&L);
insert(&L);
printf("初始化链表为:\n");
output(&L);
printf("请输入要插入的数据:\n");
scanf("%d",&e);
add(&L,e);
output(&L);
}
点个采纳吧!
你写那个会有问题,我也不知道怎么该,然后就给你写了一个新的,代码如下:
void add(node *L, int e) {
node *p = L->next, *s;
node *q = L;
s = (node *)malloc(sizeof(node));
s->data = e;
while (p != NULL) {
/*if (p->data <= e) {
s->data = e;
s->next = p->next;
p->next = s;
} else p = p->next;*/
if (p->data <= e) {
q = p;
p = p->next;
} else {
break;
}
}
s->next = q->next;
q->next = s;
}
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632