c语言:链表排序, 链表反转
下面将实现链表排序的排序和遍历显示功能:
所定义的链表结构如下: head -> p1 -> p2 ->p3 ->....->pn;
head的本身不作为数据节点,head->data保存结点个数.
insert_data(NODE* head) 在head之后插入新增的数据;
show_link_list(NODE* head)显示节点个数和每个节点的数据;
clear_link_list(NODE* head)删除并清空数据节点(不删除头结点);
FUNC_sort_list(NODE* head)对链表进行排序,传的head本身仅仅只作为一个起始地址,并不参与排序.
(比如,如果不传head, 传入head->next, 则从head->nxt->next开始进行排序)
FUNC_invert_link_list(NODE* head), 对链表数据节点进行倒序(反转),传的head本身仅仅只作为一个起始地址,并不参与倒序.
(比如,如果不传head, 传入head->next, 则从head->nxt->next开始进行倒序)
FUNC_sort_list链表排序的实现: h -> p -> .... -> pren -> pn -> ..
核心是
{ 1. 比较以及节点位置交换swap(p , pn) pren->next= pn->next; pn->next= h->next; h->next= pn; 2. 移动参考系
} 最容易错的地方: 临界节点的判定. FUNC_invert_link_list链表反转的实现: http://www.cnblogs.com/mylinux/p/4632243.html
//..............0-9..... #include <stdio.h>
#include <stdlib.h> #define PR printf typedef struct node
{
long data;
struct node* next;
} NODE, *LIST; NODE* insert_data(NODE* head)
{
PR("please input the integer datas and end with q:\n"); while()
{
char ch[];
scanf("%s", ch); if(ch[]=='q' || ch[] =='Q'){
break;
}else{
long indata = strtol(ch, , );
NODE* temp = NULL; temp = (NODE*) malloc (sizeof(struct node));
temp->data = indata;
temp->next = NULL; temp->next = head->next;
head->next = temp;
head->data++;
}
}
return head;
} void show_link_list(NODE* head)
{
NODE* p = NULL;
if(NULL == head || NULL == head->next) //........, ...;
{
return;
}
printf("the count of data in link = %d, they are :\n", head->data);
p = head->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
} void clear_link_list(NODE* head)
{
if(NULL == head || NULL == head->next)
{
return;
}
NODE* pd = head->next;
for(pd = head->next ; pd != NULL; pd =head->next)
{
head->next = pd->next;
free(pd);
}
}
NODE* FUNC_invert_link_list(NODE* head)
{
if(head == || head->next == ){
return ;
}
NODE* h = head->next; NODE* xpre = h;
NODE* x = h->next; for(; xpre->next != ; x = xpre->next)
{
xpre->next = x->next;
x->next = h;
h = x;
} head->next= h;
printf("FUNC_invert_link_list completed!\n");
return head;
}
void FUNC_sort_list(NODE* head)
{
if(NULL == head || NULL == head->next || head->next->next == NULL)
{
return;
}
NODE* h = head;
NODE* p = h->next;
NODE* pren = h->next;
NODE* pn = pren->next; while( p->next != NULL)
{
while(pn != NULL )
{
if(p->data < pn->data)
{
pren->next= pn->next;
pn->next= h->next;
h->next= pn;
//printf(" swapped!\n");
} else{
pren = pren->next;
}
pn = pren->next;
p = h->next;
} h = h->next;
p = h->next;
pren = h->next;
pn = pren->next;
}
printf("FUNC_sort_list completed!\n");
} void FUNC_bub_sort_list(NODE* head)
{
if(NULL == head || NULL == head->next || head->next->next == NULL)
{
return;
} NODE* tail = NULL;
NODE* pre = head;
NODE* p = pre->next;
#define TRUE 1
#define FALSE 0
int is_sorted = FALSE;//未排好 while(pre->next != tail && !is_sorted)
{
while(p->next != tail)
{
is_sorted = TRUE;
if(p->next->data > p->data) //如果发生数据交换,说明没有排好
{
NODE* pn = p->next;
p->next = pn->next;
pn->next = p;
pre->next = pn;
is_sorted = FALSE;
}
pre = pre->next;
p = pre->next;
} tail = p;
pre = head;
p = pre->next;
}
printf("FUNC_bub_sort_list completed!\n");
} void main(void)
{
NODE* head = (NODE*)malloc(sizeof(NODE));
head->next = NULL;
head->data = ; insert_data(head);
show_link_list(head); FUNC_sort_list(head);
show_link_list(head); FUNC_invert_link_list(head);
show_link_list(head); FUNC_bub_sort_list(head);
show_link_list(head); clear_link_list(head);
free(head); }
/*
please input the integer datas and end with q:
12 21 13 133 14 41 15 51 q
the count of data in link = 8, they are :
51 15 41 14 133 13 21 12 FUNC_sort_list completed!
the count of data in link = 8, they are :
133 51 41 21 15 14 13 12 FUNC_invert_link_list completed!
the count of data in link = 8, they are :
12 13 14 15 21 41 51 133 FUNC_bub_sort_list completed!
the count of data in link = 8, they are :
133 51 41 21 15 14 13 12
Press any key to continue . . .
*/
在sort函数里面,
如果某层循环的结束条件写错可能导致段错误,
如果参考系的移动出错可能导致段错误或者第二次的排序不对.
c语言:链表排序, 链表反转的更多相关文章
- 算法基础~链表~排序链表的合并(k条)
算法基础~链表~排序链表的合并(k条) 1,题意:已知k个已排序链表头结点指针,将这k个链表合并,合并后仍然为有序的,返回合并后的头结点. 2,方法之间时间复杂度的比较: 方法1(借助工具vector ...
- C语言 删除排序链表中的重复元素
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2示例 2: 输入: 1->1->2->3-> ...
- C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
//链表的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include< ...
- 链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述
关于数据结构等的学习,以及学习算法的感想感悟,听了郝斌老师的数据结构课程,其中他也提到了学习数据结构的或者算法的一些个人见解,我觉的很好,对我的帮助也是很大,算法本就是令人头疼的问题,因为自己并没有学 ...
- c语言之单链表的创建及排序
今天对之前学习过的链表知识进行简单的总结顺便写点代码:创建一个链表有头插法跟尾插法两种,在下面代码中我们为结点分配的内存实在堆上分配的,因此需要我们手动释放,释放用free()函数 下面代码贴出具体代 ...
- Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素
/* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...
- C语言实现单链表,并完成链表常用API函数
C语言实现单链表,并完成链表常用API函数: 1.链表增.删.改.查. 2.打印链表.反转打印.打印环形链表. 3.链表排序.链表冒泡排序.链表快速排序. 4.求链表节点个数(普通方法.递归方法). ...
- C语言实现通用链表初步(一)
注意:本文讨论的是无头单向非循环链表. 假设不采用Linux内核链表的思路,怎样用C语言实现通用链表呢? 一种常用的做法是: typedef int element_t; struct node_in ...
- c语言版单链表
1 //c语言单链表 2 #include <stdio.h> 3 #include <stdlib.h> 4 typedef struct Node 5 { 6 int da ...
随机推荐
- Kqueue与epoll机制
首先介绍阻塞与非阻塞:阻塞是个什么概念呢?比如某个时候你在等快递,但是你不知道快递什么时候过来,而且你没有别的事可以干(或者说接下来的事要等快递来了才能做):那么你可以去睡觉了,因为你知道快递把货送来 ...
- graph isomorphism 开源算法库VFlib, Nauty
VFlib 开源算法库网站:http://www.cs.sunysb.edu/~algorith/implement/vflib/implement.shtml Nauty 开源算法库网站:http: ...
- 1 初级.net web工程师,在工作中都做些什么
初级.Net Web工程师,在工作中都做些神马? 职责 初级.Net Web工程师的主要职责,就是按比较详细的要求去完成代码. 比较详细的要求是指:一般会把页面式样.功能的描述.数据库结构.性能要 ...
- Nginx 开启 debug 日志的办法
译序:一般来讲,Nginx 的错误日志级别是 error,作为 Nginx 用户来讲,你设置成 info 就足够用了. 但有时有些难以挖掘的 bug,需要看到更详细的 debug 级别 ...
- PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门
XPAth技术 XPath的设计的核心思想,可以通过xpath迅速简介的定位到你希望查找的节点.主要目的是描述节点相对其他节点的位置,可以取得所有符合条件的节点,成为[位置路径]. Xapth主要用来 ...
- 免费利用网页版谷歌翻译实现任意语言转换php版
本文源发布地址: http://ourgarden.cn/2013/07/20/%E5%85%8D%E8%B4%B9%E5%88%A9%E7%94%A8%E7%BD%91%E9%A1%B5%E7%89 ...
- STL之vector详解
一.vector容器的自增长 首先,我们知道vector容器是由数组做出来的:它具备了数组的优缺点. 数组的优点: 操作数据,读取速度很快,因为有下标: 数组的缺点: 分配之后不能在改变大小: #in ...
- 【Android】ScrollView+GridView 显示问题
在使用Android的ScrollView里面嵌套GridView时,设置android:layout_height="wrap_content"属性,运行界面的效果不会出现全部数 ...
- 小猪猪逆袭成博士之C++基础篇(二) 常量、处理类型、自定义头文件
小猪猪逆袭成博士之C++基础篇(二) const .auto. decltype 上一章我们介绍了一些常用的类型和常见的问题,下面再介绍一些学习的时候不是特别常用但是在实际工程中很有用的一些东西. 一 ...
- HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量
题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...