该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
include
#include
struct student
{
int data;
struct student *next;
};
void main()
{
int n=0;
struct student *head,*p1,*p2,*p,*q;
p1=p2=(struct student *) malloc (sizeof(struct student));
scanf("%d",&p1->data);
head=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *) malloc (sizeof(struct student));
scanf("%d",&p1->data);
}
p2->next=NULL;
printf("input the number to insert:\n");
p=(struct student *) malloc (sizeof(struct student));
scanf("%d",p->data);
p1=head;
while((p->data>p1->data)&&(p1->next!=NULL))
{
q=p1;
p1=p1->next;
}
if(p->data<=p1->data)
{
q->next=p;
p->next=p1;
}
n=n+1;
p=head;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}
输入一组数据,然后输入一个数插入到原链表,不用考虑大于第一个节点和小于最后一个节点的情况,我现在只要实现这个功能。不管我输入什么数,都只会按原样输出,如先输入1,3,5。输入2插入,可是输出是1,3,5。高手看看那错了