/* List Insertion Sorting.
* Implementation history:.
* 2013-09-15, Mars Fu, first version.
*/ #include "stdafx.h" #include "list_insertion.h" int
init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes)
{
if (hd == NULL) return 0;
if (max_cnt < 0) return 0; hd->node = NULL; nodes->cur = hd;
nodes->hd = hd;
memset(nodes->cur, 0, sizeof(struct s_clist));
nodes->nodes_cnt = 0;
nodes->max_nodes_cnt = max_cnt; debug("nodes->max_nodes_cnt %d \r\n", nodes->max_nodes_cnt); return 1;
} int
insert_list_sort(struct s_node *node, struct s_nodes *nodes, cmp_func cmp)
{
struct s_clist *tmp;
struct s_clist *next; if (node == NULL) return 0;
if (nodes->cur == NULL) return 0; next = (struct s_clist *)malloc(sizeof(struct s_clist));
if (next == NULL) {
debug("malloc list failed \r\n");
return 0;
} next->node = node; tmp = nodes->hd->pre; if (tmp == NULL) {
tmp = nodes->hd;
tmp->next = next;
next->pre = tmp;
next->next = nodes->hd;
nodes->hd->pre = next;
}
else { while (tmp != NULL) {
if (tmp->node == NULL) break;
if (cmp(tmp->node->key, node->key) < 0) break;
tmp = tmp->pre;
} next->next = tmp->next;
tmp->next->pre = next;
tmp->next = next;
next->pre = tmp;
} nodes->cur = nodes->hd->pre;
nodes->nodes_cnt++; return 1;
} int
is_list_full(struct s_nodes *nodes)
{
return (nodes->nodes_cnt >= nodes->max_nodes_cnt);
} int
get_list_cnt(struct s_nodes *nodes)
{
return nodes->nodes_cnt;
} int
delete_item_from_list(struct s_node *node, struct s_nodes *nodes)
{
struct s_clist *cur; if(node == NULL) return 0; cur = nodes->hd;
cur = cur->next;
while(cur->node != NULL) {
if(cur->node != node) {
cur = cur->next;
continue;
} cur->pre->next = cur->next;
cur->next->pre = cur->pre; cur->next = NULL;
cur->pre = NULL;
free(cur->node);
free(cur); nodes->nodes_cnt--; return 1;
} return 0;
} #ifdef LIST_INSERTION_DEBUG static int
int_increasing_cmp(void* lv, void* rv)
{
int tmp_lv, tmp_rv; tmp_lv = *(int*)lv;
tmp_rv = *(int*)rv; return (tmp_lv - tmp_rv);
} static void
debug_func(void*src, int n, int h)
{
int i; debug("%d-sort:\r\n----\r\n", h);
for (i = 0; i < n; ++i) {
debug("%d ", *((int*)src + i));
}
debug("\r\n----\r\n");
} int
main(int argc, char* argv[])
{
int i;
int cnt;
const int int_items[] = { 503, 87, 512, 61, 908, 170, 897, 275,
653, 426, 154, 509, 612, 677, 765, 703
};
int ret;
struct s_clist hd;
struct s_nodes nodes;
struct s_node *node;
struct s_clist *cur; debug("[Testing list insertion sort].. \r\n"); ret = init_list(&hd, 1000, &nodes);
if (!ret) goto END; cnt = sizeof(int_items)/sizeof(int_items[0]); debug("src database:\r\n----\r\n");
for (i = 0; i < cnt; ++i) {
debug("%d ", int_items[i]);
}
debug("\r\n----\r\n"); for (i = 0; i < cnt; ++i) {
node = (struct s_node*)malloc(sizeof(struct s_node));
if (node == NULL) goto END; node->key = (void*)(&int_items[i]);
ret = insert_list_sort(node, &nodes, int_increasing_cmp);
if (!ret) {
debug("failed. \r\n");
goto END;
}
} debug("dst database:\r\n----\r\n");
cur = nodes.hd->next;
while(cur->node != NULL) {
debug("%d ", *(int*)cur->node->key);
cur = cur->next;
} debug("\r\n----\r\n"); debug("\r\n"); debug("[Testing list insertion sort].. done. \r\n"); END:
while(1);
return 0;
} #endif /* LIST_INSERTION_DEBUG */
#ifndef __LIST_INSERTION_H__
#define __LIST_INSERTION_H__ #define LIST_INSERTION_DEBUG typedef int(*cmp_func)(void*, void*);
typedef void (*dbg_func)(void*, int, int); struct s_node{
void* key; void *data;
}; struct s_clist{
struct s_clist *pre;
struct s_clist *next;
struct s_node *node;
}; struct s_nodes
{
struct s_clist *hd;
int nodes_cnt;
int max_nodes_cnt; struct s_clist *cur;
}; int init_list(struct s_clist *hd, int max_cnt, struct s_nodes *nodes); int insert_list_sort(struct s_node *node, struct s_nodes *nodes); int is_list_full(struct s_nodes *nodes); int get_list_cnt(struct s_nodes *nodes); int delete_item_from_list(struct s_node *node, struct s_nodes *nodes); #endif /* __LIST_INSERTION_H__ */
#pragma once

#include <windows.h>
#ifdef _WIN32
#define msleep(x) Sleep(x)
#endif #include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h> #define MY_DEBUG
#ifdef MY_DEBUG
#define debug printf
#else
#define debug(x,argc, __VA_ARGS__) ;
#endif /* MY_DEBUG */ #define F_S() debug("[%s]..\r\n", __FUNCTION__)
#define F_E() debug("[%s]..done. \r\n", __FUNCTION__)

Enjoy :)

Mars

Sep 15th, 2013

Foundation Sorting: Single List Insertion Sort的更多相关文章

  1. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  2. Codeforces Round #212 (Div. 2) C. Insertion Sort

    C. Insertion Sort Petya is a beginner programmer. He has already mastered the basics of the C++ lang ...

  3. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  4. Foundation Sorting: Quicksort

    /* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...

  5. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  6. 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

    Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. [Algorithms] Insertion sort algorithm using TypeScript

    Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...

  9. 洛谷 SP9722 CODESPTB - Insertion Sort

    洛谷 SP9722 CODESPTB - Insertion Sort 洛谷传送门 题目描述 Insertion Sort is a classical sorting technique. One ...

随机推荐

  1. ROS验证publisher和subscriber

    在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确. 首先运行roscore roscore 在使用ca ...

  2. git代码提交流程

    1.进入我的项目文件夹所在目录: 2.git status 查看我修改过的文件: 3.git add -A 将修改的文件全部添加, git add 文件名  只添加指定的文件名: 4.git comm ...

  3. windows下安装testlink

    因为项目中一直没有使用任何测试用例管理工具,如果需要的时候都是个人写在的excle里各自保存,因为没有系统的记录当时测试方法和测试用例,每次需要再次测试已有的功能时,因为时间太长,而往往记不得当时是怎 ...

  4. Category、Extension

    Category,分类,类目.主要作⽤用是为没有源代码的添加方法,例系统自带的NSString. 通过Category添加的方法会成为原类的一部分.从⽽而达到扩展一 个类的功能.   Category ...

  5. 黑马12期day01之html&css

    html注释:<!-- --> html中不支持空格.回车.制表符都会被解析成一个空格 <pre></pre>标签内以上三个会被正常解析. <font> ...

  6. 专访CEO何朝曦:深信服高速成长的秘诀

    在深信服公司深圳总部的办公室里,要迅速找到几位高管的工位远远不如找一位女员工的座位那样容易. 深信服CEO何朝曦先生 公司里虽然女孩很少,但几乎每位女员工的工位上都有一盆绿植.相比之下,从公司CEO何 ...

  7. Mysql 学习记录

    ( xampp 的mysql 与 直接用 dnf 安装的 mysql 有冲突! ) 1. 数据库基本知识: 一张表的行 又称为 记录 一张表的列 又称为 字段 表结构:所有字段,规定了你的每一条记录所 ...

  8. 假设将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法

    假设将synthesize省略,而且我们自己实现setter和getter方法时,系统就不会生成相应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和 ...

  9. Google Maps 学习笔记(二)地图天气预报服务 2014.06.04

    地图天气预报服务:一,获取天气预报信息:二,解析天气预报信息:三,在地图上加载天气预报信息: Yahoo!提供的天气预报服务采用流行的RSS输出结果,接口地址如下: http://weather.ya ...

  10. objective-C学习笔记(十一)类别和扩展

    类别 类别是对外的,外部都可以访问 类别是在没有源代码或者基于某些特定场合的情况下,为一个类增加功能(方法).或者用于给一个特别大的类进行分割. 命名规则:类名+扩展方法,如NSString 可以添加 ...