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 ...
随机推荐
- shell实例浅谈之六文件特定行打印的多种方法
一.问题 Sed和AWK在处理文件方面有很强的优势,还有head和tail等文件处理工具的使用,grep也可实现文本的搜索.上述命令都可以在后面直接加文件名,不需要在前面使用cat添加管道,cat会影 ...
- poj 1150 The Last Non-zero Digit
/** 大意: 求A(n,m)的结果中从左到右第一个非零数 思路: 0是由2*5的得到的,所以将n!中的2,5约掉可得(2的数目比5多,最后再考虑进去即可) 那n!中2 的个数怎么求呢? int ge ...
- 「数据结构」:模拟指针(simulated pointer)
模拟指针,也就是清华严老师<数据结构-C语言描述>中的静态链表,静态链表的引用是使用一段连续的存储区还模拟指针的功能,可以有效的利用一段连续内存进行一定范围内可变的子链表的空间分配,此数据 ...
- boost::thread用法
最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...
- AFNetworking GET和POST请求
GET请求 代码展示: 在storyBoard中每个请求关联一个Button #pragma mark - get请求 - (IBAction)getRequest:(id)sender { // 参 ...
- ThinkPHP - CURD增删改查 - 实例 - 搜索功能
模板代码: /** * 搜索数据 * @return 无返回值 */ public function search(){ //判断并接收参数 //姓名 if ( isset($_POST['usern ...
- iOS系统自带的 UIAlertView 自动旋转的实现
这里主要解析 UIAlertView 的几个关键功能的实现: 随着设备屏幕的旋转而旋转: Alert弹出框,使用UIWindow来实现,就是说,不用依赖于当前显示在最前面的UIView. 实现源码参考 ...
- ViewPager实现广告自动轮播核心代码(Handler+Thread)
ViewPager数据源是4个线性布局,每个布局里面充满一张高度固定.宽度充满父布局的图片.有4个小圆点跟随ViewPager滑动.轮播原本我是用Timer+TimerTask的,但是问题颇多,很是郁 ...
- Latex(一)公式自动编号与自动引用
在进行latex引用时,有两种办法: 一,被动引用. 如有这样一段代码: $$ x^2+y^2= z^2.\eqno(1.1) $$ In this paper, we investigated (1 ...
- UVa401 Palindromes
#include <stdio.h>#include <string.h> char mirror(char c){ static const char m[] = &q ...