链表答案错误,编程过程没有出现错误
/*指针实验2_20题 填空*/#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct member)
struct member
{ int num;
struct member *next;
}; /*建立结构体*/
typedef struct member Member;
/*插入一个新的结点*/
void insertup(Member *head, Member *newp)
{
Member *pre,*suc;
pre=head;
suc=head->next;
while (suc!=NULL)
{
if (suc->num>=newp->num)
{
pre->next=newp;
newp->next=suc;
}
pre=suc;
suc=suc->next;
}
newp->next=suc;
pre->next=newp;
}
/*从小到大建立一个动态链表*/
struct member *creat()
{
Member *head,*p1,*p2;
int n=0;
p1=p2=(Member *)malloc(LEN);
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(Member *)malloc(LEN);
scanf("%d",&p1->num);
}
p2->next=NULL;
return (head);
}
/*输出链表*/
void print(Member *head)
{
Member *p;
printf("排序后输出为:\n");
p=head;
if(head!=NULL)
do{
printf("%5d",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void main()
{
struct member a;
struct member *head,*newp;
newp=&a;
printf("请输入一组按升序排列的数,以0作为结束的标志:\n");
head=creat();
printf("输入一个要插入的结点:\n");
scanf("%d",&newp->num);
insertup(head,newp);
print(head);
}