关于链表插入的困惑
#include<stdio.h>#include<conio.h>
#include<stdlib.h>
struct student
{
int num;
int score;
struct student * next;
};
typedef struct student STU;
STU *create(void)
{
STU*head=NULL;
STU*tail=NULL;
head=(STU*)malloc(sizeof(STU));
if(head==NULL)
{
printf("头结点内存申请失败");//什么情况会失败
return NULL;//不理解
}
head->next=NULL;
tail=head;
STU*pnewelement=NULL;
int n,s;
while(1)
{
printf("请输入学号和成绩\n");
scanf("%d%d",&n,&s);
if(n>0 &&s>0)
{
pnewelement=(STU*)malloc(sizeof(STU));
if(pnewelement==NULL){
printf("头结点内存申请失败");
return NULL;
}
pnewelement->num=n;
pnewelement->score=s;
pnewelement->next=NULL;
tail->next=pnewelement;
tail=pnewelement;
}
else
break;
}
pnewelement=head;
head=head->next;
free(pnewelement);
return head;
}
void disp(STU *head)
{
STU*p=head;
while(1)
{
if(p==NULL)
return;
printf("(学号:%d,成绩:%d)\n",p->num,p->score);
p=p->next;
}
}
STU*insert(STU*head)
{
int n,s;
STU*einsert=NULL,*E1=NULL,*E2=NULL;
E1=head;
while(1)
{
printf("请输入待插入同学的学号和成绩\n");
scanf("%d%d",&n,&s);
if(n>0 &&s>0)
{
einsert=(STU*)malloc(sizeof(STU));
if(einsert==NULL)
{
printf("结点内存申请失败");
return NULL;
}
einsert->num=n;
einsert->score=s;
while(n>E1->num &&E1->next!=NULL)
{
E2=E1;//这个是什么意思!!??
E1=E1->next;//不理解这里
}
if(n<=E1->num)
{
if(head==E1)
{
einsert->next=E1;
head=einsert;
}
else
{
E2->next=einsert;
einsert->next=E1;
}
}
else
{
E1->next=einsert;
einsert->next=NULL;
}
}
else
break;
}
return head;
}
void main(void)
{
STU* head=NULL;
head=create();
printf("--------------------\n");
disp(head);
printf("--------------------\n");
head=insert(head);
printf("--------------------\n");
disp(head);
getch();
}
红色中的那个怎么理解