cJSON排序(双链表排序的另一种方式)

双链表排序算法

本文提供一种双链表排序方法,主要思路是:双链表的next,prev 指针不变,即链表的前后关系不变,只交换链表中的数据内容;如升序排序,从开始向后两两比较,若前面A比后面大B,交换AB的内容,保证了是升序,但不能保证B比A前面的内容小,所以要向前遍历,发现前面的比后面的大,交换数据;这样向后一次遍历完成,排序完成;
这样做的好处是:比网上交换指针的那种算法简单,思路清晰;
cJSON 是C语言处理 json的方法(cJSON.c和cJSON.h两个文件),具体用法可百度;
cJSON 组的数据其实就是一个双链表,所以用cJSON组json的数据作为双链表的数据(这样不用自己写双链表了);
cJSON 的节点数据:

/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

typedef struct cJSON {
	struct cJSON *next,*prev;	
	struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
	int type;					/* The type of the item, as above. */
	char *valuestring;			/* The item's string, if type==cJSON_String */
	int valueint;				/* The item's number, if type==cJSON_Number */
	double valuedouble;			/* The item's number, if type==cJSON_Number */
	char *string;				/* The item's name string, if this item is the chi
cJSON源码,为c语言编写,里面含有测试案例,需要用的自取。可以创建json字符串。 ## Usage ### Welcome to cJSON. cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file. JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state. As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it in one of two modes: Auto and Manual. Let's have a quick run-through. I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way. ### Building There are several ways to incorporate cJSON into your project. #### copying the source Because the entire library is only one C file and one header file, you can just copy `cJSON.h` and `cJSON.c` to your projects source and start using it. cJSON is written in ANSI C (C89) in order to support as many platforms and compilers as possible. #### CMake With CMake, cJSON supports a full blown build system. This way you get the most features. CMake with an equal or higher version than 2.8.5 is supported. With CMake it is recommended to do an out of tree build, meaning the compiled files are put in a directory separate from the source files. So in order to build cJSON with CMake on a Unix platform, make a `build` directory and run CMake inside it. ``` mkdir build cd build cmake .. ``` This will create a Makefile and a bunch of other files. You can then compile it: ``` make ``` And install it with `make install` if you want. By default it installs the headers `/usr/local/include/cjson` and the libraries to `/usr/local/lib`. It also installs files for pkg-config to make it ea
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值