# include<stdio.h>
# include<malloc.h>
struct list
{
int data;
struct list *next;
};
struct list *creat();
void print(struct list *head);
void insert(struct list *head,struct list *pnew);
void main()
{
struct list *head,*p1;
int m;
head=creat();
printf("\n");
printf("Enter the number of you want:\n");
scanf("%d",&m);
p1=(struct list*)malloc(sizeof(struct list));
if (p1==NULL)
{
printf("NO MEMORY!\n");
return ;
}
p1->data=m;
p1->next=NULL;
insert(head,p1);
printf("\n");
return;
}
struct list *creat()
{
struct list *head,*p,*rear;
int x;
head=(struct list*)malloc(sizeof(struct list));
rear=head;
scanf("%d",&x);
puts("input the list end with '0':\n");
while(x)
{
p=(struct list*)malloc(sizeof(struct list));
p->data=x;
rear->next=p;
rear=p;
scanf("%d",&x);
}
rear->next=NULL;
puts("the list you input is: ");
print(head->next);
return head->next;
}
void print(struct list *head)
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
void insert(struct list *head,struct list *pnew)
{
struct list *p;
p=head;
for (p=head->next;p->next!=NULL;p=p->next)
if ((pnew->data)>(p->data))
{
pnew->next=p->next;
p->next=pnew;
}
print(head);
printf("\n");
}
假设我输入1 2 3 4 5 6
首先是打印这些数1 2 3 4 5 6
我要输入一个8
然后打印插入这个数之后的数1 2 3 4 5 6 8
可我的程序好像执行不出来。请问是那里的问题。