c语言 在单链表中插入一个数。为什么这样编译没有问题但是运行不了
#include<stdio.h>#include<stdlib.h>#include<string.h>#defineLsizeof(structstatus)type...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L sizeof(struct status)
typedef struct status
{
int data;
struct status *next;
}status;
int n;
status *creat(status *h)
{
struct status *p1,*p2;
p1=p2=(struct status *)malloc(L);
scanf("%d",&p1->data);
p1->next=NULL;
while(p1->data!=0)
{
if(h==NULL) h=p1;
else p2->next=p1;
p2=p1;
p1=(struct status *)malloc(L);
scanf("%d",&p1->data);
}
p2->next=NULL;
return(h);
}
status insert(struct status *h,int i,int x)
{
struct status *p,*s;
s=(struct status *)malloc(L);
s->data=x;
n=0;p=h;
while(p&&(n<i-1))
{
p=p->next;
++n;
}
if(!p||(n>i-1))
printf("ERROR!");
else s->next=p->next;
p->next=s;
return(*h);
}
void print(struct status *h)
{
struct status *q;
printf("Now the numbers are: ");
q=h;
if(h!=NULL)
do
{printf("%d ",q->next);
q=q->next;
}while(q!=NULL);
}
int main()
{
struct status *creat(struct status*);
struct status insert(struct status*,int,int);
void print(struct status *);
int i,x;
struct status *h;
printf("The numbers are:");
creat(h);
printf("The number you wanna insert and where:");
scanf("%d %d",&x,&i);
insert(h,i,x);
print(h);
return 0;
} 展开
#include<stdlib.h>
#include<string.h>
#define L sizeof(struct status)
typedef struct status
{
int data;
struct status *next;
}status;
int n;
status *creat(status *h)
{
struct status *p1,*p2;
p1=p2=(struct status *)malloc(L);
scanf("%d",&p1->data);
p1->next=NULL;
while(p1->data!=0)
{
if(h==NULL) h=p1;
else p2->next=p1;
p2=p1;
p1=(struct status *)malloc(L);
scanf("%d",&p1->data);
}
p2->next=NULL;
return(h);
}
status insert(struct status *h,int i,int x)
{
struct status *p,*s;
s=(struct status *)malloc(L);
s->data=x;
n=0;p=h;
while(p&&(n<i-1))
{
p=p->next;
++n;
}
if(!p||(n>i-1))
printf("ERROR!");
else s->next=p->next;
p->next=s;
return(*h);
}
void print(struct status *h)
{
struct status *q;
printf("Now the numbers are: ");
q=h;
if(h!=NULL)
do
{printf("%d ",q->next);
q=q->next;
}while(q!=NULL);
}
int main()
{
struct status *creat(struct status*);
struct status insert(struct status*,int,int);
void print(struct status *);
int i,x;
struct status *h;
printf("The numbers are:");
creat(h);
printf("The number you wanna insert and where:");
scanf("%d %d",&x,&i);
insert(h,i,x);
print(h);
return 0;
} 展开
2个回答
展开全部
两处错误。 你看下注释。
另外,建议加一个销毁链表释放内存的函数。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define L sizeof(struct status)
typedef struct status
{
int data;
struct status *next;
}status;
int n;
status *creat(status *h)
{
struct status *p1,*p2;
p1=p2=(struct status *)malloc(L);
scanf("%d",&p1->data);
p1->next=NULL;
while(p1->data!=0)
{
if(h==NULL) h=p1;
else p2->next=p1;
p2=p1;
p1=(struct status *)malloc(L);
scanf("%d",&p1->data);
}
p2->next=NULL;
return(h);
}
status insert(struct status *h,int i,int x)
{
struct status *p,*s;
s=(struct status *)malloc(L);
s->data=x;
n=0;p=h;
while(p&&(n<i-1))
{
p=p->next;
++n;
}
if(!p||(n>i-1))
printf("ERROR!");
else s->next=p->next;
p->next=s;
return(*h);
}
void print(struct status *h)
{
struct status *q;
printf("Now the numbers are: ");
q=h;
if(h!=NULL)
do
{printf("%d ",q->data);//打印data
q=q->next;
}while(q!=NULL);
printf("\n");
}
int main()
{
struct status *creat(struct status*);
struct status insert(struct status*,int,int);
void print(struct status *);
int i,x;
struct status *h=NULL;//初始化为NULL
printf("The numbers are:");
h=creat(h);//赋值返回值为h
printf("The number you wanna insert and where:");
scanf("%d %d",&x,&i);
insert(h,i,x);
print(h);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询