c语言之单链表的创建及排序
今天对之前学习过的链表知识进行简单的总结顺便写点代码;创建一个链表有头插法跟尾插法两种,在下面代码中我们为结点分配的内存实在堆上分配的,因此需要我们手动释放,释放用free()函数
下面代码贴出具体代码:
#include <stdio.h>
#include <stdlib.h> struct person {
int age;
struct person *next;
}; struct person *insert_head(struct person *head, int age);
struct person *insert_tail(struct person *head, int age);
void destroy_list(struct person *head);
void show(struct person *head); int main()
{
struct person *head = NULL;
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
show(head); return ;
} /*头插法*/
struct person *insert_head(struct person *head, int age)
{
struct person *tmp = NULL; tmp = (struct person *)malloc(sizeof(struct person));
tmp->age = age;
tmp->next = NULL; if(NULL == head) {
return tmp;
}
tmp->next = head;
head = tmp; return head;
} /*尾插法*/
struct person *insert_tail(struct person *head, int age)
{
struct person *tmp = NULL;
struct person *find = NULL; tmp = (struct person *)malloc(sizeof(struct person));
tmp->age = age;
tmp->next = NULL; if(NULL == head) {
return tmp;
}
find = head;
while(find->next) {
find = find->next;
}
find->next = tmp; return head;
} /*销毁整个链表*/
void destroy_list(struct person *head)
{
struct person *tmp = NULL; tmp = head->next;
while(tmp) {
head->next = tmp->next;
free(tmp);
tmp = head->next;
}
} /*遍历链表*/
void show(struct person *head)
{
struct person *tmp = head;
while(tmp) {
printf("aeg is %d\n", tmp->age);
tmp = tmp->next;
}
}
上面的是创建了一个简单的链表,下面我们对链表进行简单的排序:
对于下面的链表排序:我们可以分为三步:
(1)在原链表中找到最小的
(2)从原链表摘下最小的
(3)一次插入到新链表
循环直到原链表为空
#include <stdio.h>
#include <stdlib.h> struct person {
int age;
struct person *next;
}; struct person *insert_head(struct person *head, int age);
struct person *insert_tail(struct person *head, int age);
struct person *insert_sort(struct person *head);
void destroy_list(struct person *head);
void show(struct person *head); int main()
{
struct person *head = NULL;
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
head = insert_tail(head, );
show(head);
printf("------------------------------\n");
head = insert_sort(head);
show(head);
} /*头插法*/
struct person *insert_head(struct person *head, int age)
{
struct person *tmp = NULL; tmp = (struct person *)malloc(sizeof(struct person));
tmp->age = age;
tmp->next = NULL; if(NULL == head) {
return tmp;
}
tmp->next = head;
head = tmp; return head;
} /*尾插法*/
struct person *insert_tail(struct person *head, int age)
{
struct person *tmp = NULL;
struct person *find = NULL; tmp = (struct person *)malloc(sizeof(struct person));
tmp->age = age;
tmp->next = NULL; if(NULL == head) {
return tmp;
}
find = head;
while(find->next) {
find = find->next;
}
find->next = tmp; return head;
} /*销毁整个链表*/
void destroy_list(struct person *head)
{
struct person *tmp = NULL; tmp = head->next;
while(tmp) {
head->next = tmp->next;
free(tmp);
tmp = head->next;
}
} #if 0
/*从大到小*/
struct person *insert_sort(struct person *head)
{
struct person *tmp = NULL;
struct person *newhead = NULL;
struct person *min = NULL;
struct person *min_pre = NULL; if(NULL == head || NULL == head->next)
return head; while(head) {
tmp = head;
min = head;
min_pre = NULL; /*step 1:find min*/
while(tmp->next) {
if(min->age > tmp->next->age) {
min_pre = tmp;
min = tmp->next;
}
tmp = tmp->next;
} /*step 2: cut min*/
if(min == head) {
head = head->next;
min->next = NULL;
}
else {
min_pre->next = min->next;
min->next = NULL;
} /*step 3: insert new list*/
if(NULL == newhead) {
newhead = min;
continue;
}
min->next = newhead;
newhead = min;
} return newhead;
} #else
/*从小到大*/
struct person *insert_sort(struct person *head)
{
struct person *tmp = NULL;
struct person *newhead = NULL;
struct person *newtail = NULL;
struct person *min = NULL;
struct person *min_pre = NULL; if(NULL == head || NULL == head->next)
return head; while(head) {
tmp = head;
min = head;
min_pre = NULL; /*step 1: find min*/
while(tmp->next) {
if(min->age > tmp->next->age) {
min_pre = tmp;
min = tmp->next;
}
tmp = tmp->next;
} /*step 2: cut min*/
if(min == head) {
head = head->next;
min->next = NULL;
}
else {
min_pre->next = min->next;
min->next = NULL;
} /*step 3: insert new list*/
if(NULL == newhead) {
newhead = min;
newtail = min;
continue;
}
newtail->next = min;
newtail = newtail->next;
} return newhead;
}
#endif /*遍历链表*/
void show(struct person *head)
{
struct person *tmp = head;
while(tmp) {
printf("aeg is %d\n", tmp->age);
tmp = tmp->next;
}
}
c语言之单链表的创建及排序的更多相关文章
- C语言写单链表的创建、释放、追加(即总是在最后的位置增加节点)
昨天周末给学妹讲了一些指针的知识,本来我对指针就是似懂非懂的状态,经过昨天一讲,我对指针的学习就更深刻了 果然给别人讲课也是学习的一个方法.加上最近复习数据结构,发现我的博客里没有链表的博文,所以趁这 ...
- C语言实现单链表-03版
在C语言实现单链表-02版中我们只是简单的更新一下链表的组织方式: 它没有更多的更新功能,因此我们这个版本将要完成如下功能: Problem 1,搜索相关节点: 2,前插节点: 3,后追加节点: 4, ...
- C/C++语言实现单链表(带头结点)
彻底理解链表中为何使用二级指针或者一级指针的引用 数据结构之链表-链表实现及常用操作(C++篇) C语言实现单链表,主要功能为空链表创建,链表初始化(头插法),链表元素读取,按位置插入,(有序链表)按 ...
- C语言实现单链表-02版
我们在C语言实现单链表-01版中实现的链表非常简单: 但是它对于理解单链表是非常有帮助的,至少我就是这样认为的: 简单的不能再简单的东西没那么实用,所以我们接下来要大规模的修改啦: Problem 1 ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- C语言实现单链表节点的删除(带头结点)
我在之前一篇博客<C语言实现单链表节点的删除(不带头结点)>中具体实现了怎样在一个不带头结点的单链表的删除一个节点,在这一篇博客中我改成了带头结点的单链表.代码演示样例上传至 https: ...
- java实现单链表的创建、增、删、改、查
文章目录 单链表的创建.增.删.改.查 1.增加一个节点 2.删除一个节点 3.修改某一个节点 5.遍历单链表 单链表的创建.增.删.改.查 双向链表的增删改查:https://blog.csdn.n ...
- 「C语言」单链表/双向链表的建立/遍历/插入/删除
最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ...
- go语言实现单链表
线性表包含两种存储方法:顺序存储结构和链式存储结构,其中顺序表的缺点是不便插入与删除数据. 单链表:每个结点包含两部分:数据域+指针域,上一个结点的指针指向下一结点,依次相连,形成链表.特别注意的是每 ...
随机推荐
- 如何取得ChipmunkConstraint实例对象的私有属性
在 如何用代码禁用SpriteBuilder中创建的关节 一篇中提到了要想禁用一个关节就需要将其无效化. 然后我们在重新创建新关节时,可以参考该关节的原始参数. 但是代码中只能直接访问到bodyA和b ...
- 2015/12/24:嵌入式C语言的位操作随笔
今晚是平安夜,首先祝大家平安夜快乐,明天是圣诞,祝大家圣诞快乐!! 好了,这周都特别有空,上班也非常轻松,基本就是看看内核驱动,学学安卓,没什么正事的开发活干.今晚,我们来总结一例在现实 ...
- 苹果IOS与谷歌 android系统的UI设计原则
一.苹果为IOS的界面设计提出了六大原则: 1.整体美学 整体美学指的是一个应用的表现和行为与它的功能完美集成,传达连贯的信息. 人们关心一个应用是否提供它承诺的功能,但他们也被应用的外观和行为强烈影 ...
- 利用JQuery直接调用asp.net后台方法
利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. [WebMethod] 命名空间 1.无参数的方法调用, 注意:1.方法一定要静态方法,而且要有[WebMethod] ...
- Useful Scripts for E-Business Suite Applications Analysts
In this Document Purpose Questions and Answers IMPORTANT: 1. How to find versions of files i ...
- 如何在SpriteBuilder中使用BM Font Label
开始不知道,还真有点小繁琐. mac系统上创建BM Font的工具有不少,我主要用hiero和GlyphDesigner:前者是java写的,后者是mac原生的,功能都差不多. 还有一个类似的工具bm ...
- shell脚本里面相互调用时路径不要用pwd获取
shellA调用shellB,如果shellB 里面需要使用路径作为变量,去寻找其它文件.那么要注意,不用pwd,其返回的是系统中用户当前所在位置的路径,也就是shellA的路径,这样就错了.应该用d ...
- vector向量容器的一些基本操作
#include <vector> #include <iostream> using namespace std; void print(vector<int>& ...
- Spring Boot定时任务应用实践
在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...
- winform 写App.config配置文件——IT轮子系列(八)
前言 在winform项目中,常常需要读app.config文件.如: var version = System.Configuration.ConfigurationManager.AppSetti ...