//C程序设计第四版(谭浩强)
//章节:第九章 用户自己建立数据类型
//题号:9.8
//题目:写一个函数insert,用来向一个动态链表插入结点
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *insert(struct node *head,struct node *p)
{ //将p指向的结点插入链表,并使链表保持有序
struct node *p1,*p2;
if(head==NULL) //如果原链表为空
{
head=p;
p->next=NULL;
return head;
}
p2=p1=head;
while((p->data)>(p1->data)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if((p->data)<=(p1->data))
{
p->next=p1;
if(head==p1) //插在链表首部
head=p;
else
p2->next=p1; //插在链表中间
}
else
{
p1->next=p; //插在链表尾结点之后
p->next=NULL;
}
return head;
}
9.8写一个函数insert,用来向一个动态链表插入结点
猜你喜欢
转载自blog.csdn.net/weixin_44589540/article/details/86687307
今日推荐
周排行