写一个函数insert,用来向一个动态链表插入结点
#include<stdioh> struct student {1ong num; float score; struct student*next; }; int n; struct student *insert(struct student *head,struct student *stud) {struct student *p0,*p1,*p2; p1=head; //使p1指向第一个结点 p0=stud; ∥指向要插入的结点 if(head==NULL) ∥原来的链表是空表 (head=p0;p0->next=NULL;} //使P0指向的结点作为头结点 else {while((p0->num>p1->num)&&(p1->next!=NULL)) {p2=p1; //使P2指向刚才p1指向的结点 p1=p1->next; } //p1后移一个结点 if(p0->num<=p1->num) {if(head==p1)head=p0; //插到原来第一个结点之前 elsep2->next=p0; //插到p2指向的结点之后 p0->next=p1; } else {p1->next=p0; p0->next=NULL; //插到最后的结点之后 } } n=n+1; //结点数加1 return(head); }
#include<stdioh>
struct student
{1ong num;
float score;
struct student*next;
};
int n;
struct student *insert(struct student *head,struct student *stud)
{struct student *p0,*p1,*p2;
p1=head; //使p1指向第一个结点
p0=stud; ∥指向要插入的结点
if(head==NULL) ∥原来的链表是空表
(head=p0;p0->next=NULL;} //使P0指向的结点作为头结点
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1; //使P2指向刚才p1指向的结点
p1=p1->next;
} //p1后移一个结点
if(p0->num<=p1->num)
{if(head==p1)head=p0; //插到原来第一个结点之前
elsep2->next=p0; //插到p2指向的结点之后
p0->next=p1;
}
{p1->next=p0;
p0->next=NULL; //插到最后的结点之后
n=n+1; //结点数加1
return(head);
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题