编写一个简单的C语言链表插入程序!

来源:百度知道 编辑:UC知道 时间:2024/05/12 11:27:18
有高人帮俺编写一个简单的C语言链表插入程序吗!
要求简单易懂,最好注释多一些,如采用再追加20积分。
有什么问题也可加本人QQ:87864242

程序我已经写出来了,就是有些地方还是看不懂,有谁能帮我加点注释吗,越多越好!

struct student * insert(struct student *head,struct student *stu)
{
struct student *p0,*p1,*p2;
p1 = head; //让p1指向头节点
p0 = stu; //p0指向要插入的节点

if(head == NULL)
{
head = p0; //使p0指向节点作为头指针
p0->next = NULL;
}
else
{
//插入
while((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1; //使p2指向p1指向的值
p1 = p2->next; //p1移到下一个节点
}

if(p0->num <= p1->num)
{
if(head == p1)
{
head = p0;
}
else
{
p2->next = p0;
}
p0->next = p1;
}

p->next->prior->=s;
s->next=p->next;
p->next=s;
s->prior=p;
很久了,也忘的差不多了.

我是新手 这个是谭浩强书上的链表插入,希望你能看懂

Struct student
{long num;
float score;
struct srudent *next;
};
/*定义结构体 student*/

struct student *insert(struct student *head , struct student *stud)
/*对结构体的插入函数 stud为插入的结点*/
{
struct srudent *p0,*p1,*p2; /*定义三个指针 p0, p1, p2 */
p1=head; /*p1指向 head */
p0=stud; /*p0 指向 srud */
if(head==null) /* 如果head 是空值 也就是结构体里没有数据*/
{head=p0;p0->next=null;} /*head指向p0,p0的末指针是空值 p0是stud */
Else
{while(( p0->num > p1>-num)&&(p1->next!=null)) /* 在p1后插入p0 */
{p0=p1; /* 指向指向刚才p1指向的结点 */
p1=p1->next;} /* p1后移一个结点 */
if(p0->num < p1->num) /*在p1前插入p0*/
if(head==p1) head=p0;/* 如果p1是第一个结点,则在p1 前插入p*/
p0>-next =p1;
else p2->next=p0;/* 在p2 和