单链表的创建,删除,插入,显示,排序
2008-04-25 21:22
423 查看
#include <stdio.h>
#include <malloc.h>
struct info
...{
int num;
int score;
};
struct student
...{
struct info infor;
struct student *next;
};
int n;
struct student *creat(void)
...{
struct student *head, *p1, *p2;
n = 0;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
scanf("%d, %d", &p1->infor.num, &p1->infor.score);
head = NULL;
while (p1->infor.num != 0 )
...{
n++;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
scanf("%d, %d", &p1->infor.num, &p1->infor.score);
}
p2->next = NULL;
return head;
}
void show(struct student *head)
...{
struct student *p;
printf(" Now, These %d records are: ", n);
p = head;
if (head != NULL)
...{
do
...{
printf("%d %d ", p->infor.num, p->infor.score);
p = p->next;
}while (p != NULL);
}
}
struct student *insert(struct student *head, struct student *stud)
...{
struct student *p1, *p2, *p3;
p2 = head;
p1 = stud;
if (head == NULL)
...{
head = p1;
p1->next = NULL;
}
else
...{
while (p1->infor.num > p2->infor.num && p2->next != NULL)
...{
p3 = p2;
p2 = p2->next;
}
if (p1->infor.num <= p2->infor.num)
...{
if (head == p2)
...{
head = p1;
}
else
...{
p3->next = p1;
}
p1->next = p2;
}
else
...{
p2->next = p1;
p1->next = NULL;
}
}
n++;
return head;
}
struct student *del(struct student *head, int num)
...{
struct student *p1, *p2;
if (head == NULL)
...{
printf(" , list null ");
return head;
}
p1 = head;
while (p1->infor.num != num && p1->next != NULL)
...{
p2 = p1;
p1 = p1->next;
}
if (p1->infor.num == num)
...{
if (p1 == head)
...{
head = p1->next;
}
else
...{
p2->next = p1->next;
}
printf(" delete: %d ", num);
n--;
}
if (p1 == NULL)
...{
printf(" %d not been found ", num);
}
return head;
}
struct student *sort(struct student *head)
...{
struct student *p1, *p2;
struct info temp;
if (head == NULL)
...{
printf(" list null ");
return head;
}
p1 = head;
p2 = p1->next;
while (p1 != NULL)
...{
while (p2 != NULL)
...{
if (p1->infor.score > p2->infor.score)//可以设置为根据成绩排序或学好排序
...{
temp = p1->infor;
p1->infor = p2->infor;
p2->infor = temp;
}
p2 = p2->next;
}
p1 = p1->next;
if (p1->next == NULL)
...{
break;
}
else
...{
p2 = p1->next;
}
}
return head;
}
int main()
...{
struct student *head, *stu;
int num;
printf("input records: ");
head = creat();
show(head);
printf(" input the deleted number:");
scanf("%d", &num);
while (num != 0)
...{
head = del(head, num);
show(head);
printf(" input the deleted number:");
scanf("%d", &num);
}
printf(" input the inserted record:");
stu = (struct student *)malloc(sizeof(struct student));
scanf("%d, %d", &stu->infor.num, &stu->infor.score);
while (stu->infor.num != 0)
...{
head = insert(head, stu);
show(head);
printf(" input the inserted record:");
stu = (struct student *)malloc(sizeof(struct student));//重新分配一次内存,以免覆盖掉上一次的节点
scanf("%d, %d", &stu->infor.num, &stu->infor.score);
}
head = sort(head);
show(head);
return 0;
}
相关文章推荐
- C++单链表的动态创建,查找,遍历,删除,插入,添加,排序
- 链表的创建 查找 排序 插入 删除 逆序 长度 显示
- 单链表的创建、插入、删除、排序以及逆置
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 单链表的创建、测长、打印、插入、删除、排序及逆置
- java实现单链表的初始化,创建,删除,插入,查找,排序,同项删除,退出等功能
- 单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等
- 单链表的创建删除排序插入逆向打印各种操作
- C语言单链表的创建、插入、查找、删除、求长、排序、遍历
- 单链表创建-遍历-排序-插入-删除-逆序操作
- 单链表操作,创建,遍历,插入,删除,排序等操作
- 链表初解(一)——单链表的创建、删除、插入、测长、排序、逆置
- 线性表---单链表(创建、插入、删除、排序、测长和打印输出)
- 单链表的创建,删除,插入,排序
- c语言单向列表的创建、插入、删除、排序和遍历。。。
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 【程序员面试宝典】数据结构基础一单链表:创建|求长|插入|删除|排序|打印|逆置
- 链表:插入,删除,显示,排序
- java语言编写链表的基本操作(链表的创建,插入,删除,打印,排序)
- 数据结构之单链表——带有节点的单链表的创建、插入和删除(C/C++)