链表操作程序(完整)C语言
链表操作程序(完整)C语言.txt女人谨记:一定要吃好玩好睡好喝好。一旦累死了,就别的女人花咱的钱,住咱的 房,睡咱的老公,泡咱的男朋友,还打咱的娃。#include
#include
#include
/*定义结构体*/
struct student
{
int num;
float score;
struct student *next;
};
/*创建一个只有头结点的空链表*/
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof (struct student) );
if(head==NULL) //小心别漏这个
{
printf("申请头结点失败!\n");
return NULL;
}
head->next=NULL;
return head;
}
/*将s指向的结点插入链表,使链表保持升序,并返回头结点*/
struct student *insert(struct student *head,struct student *s)
{
struct student *p=head;
while(p->next!=NULL&&s->score>p->next->score)//特别注意&&左右不能写反,若s最大,最后p->next=NULL,p->next->score运行出错
p=p->next;
if(p->next==NULL) //s->score最大的情况 //其实两种情况可以并在一块写
{
p->next=s; //连接结点
s->next=NULL; //p->next就等于NULL
}
else
{
p->next=s; //连接结点
s->next=p->next;
}
return head ;
}
/*查找符合条件的结点,并返回指向该结点的指针*/
struct student *search(struct student *head)
{
struct student *p=head->next;
int num;
printf("请输入要查找学生的学号:\n");
scanf("%d",&num);
while(p!=NULL&&p->num!=num) //特别注意两条件不能写反,若写反最后p指向NULL时p->num找不到 运行出错
p=p->next;
if(p==NULL) //特别注意两个if不能调换,若调换最后p指向NULL时p->num运行出错
{
printf("找不到符合条件的结点!!!");
return NULL;//查找不到返回空指针
}
if(p->num==num)
{
printf("找到符合条件的结点\n该结点为%d\t%f",p->num,p->score);
return p;//返回查找到的指针
}
}
/*输出链表各结点的值,也称对链表的遍历*/
void print(struct student *head)
{
struct student *p;
printf(" 链表如下: \n");
p=head->next;
while(p!=NULL)
{
printf("%d\t%.1f\n",p->num,p->score);
p=p->next;
}
}
/*释放链表*/
void free_list(struct student *head)
{
struct student *p=head ;
printf("释放链表:\n");
while(p!=NULL)
{
head=head->next;
free(p);
p=head;
}
printf("释放链表成功!\n");
}
/*删除链表中值为num的结点,并返回链表的首指针*/
struct student *delete_note(struct student *head,int num_x)
{
struct student *p1=head->next , *p2=head ;
while(p1!=NULL&&p1->num!=num_x)