插入排序法
#include <iostream> #include <stdlib.h> using namespace std; typedef struct node* list; struct node { int Item; list next; }; void Display(list head) { list t = head; while (nullptr != t) { cout << t->Item << endl; t = t->next; } } int main(int argc, char *argv[]) { int N = atoi(argv[1]); struct node heada; list a = &heada; a->next = nullptr; list ax = a; for (int i=0; i<N; i++) { ax->next = (list)malloc(sizeof(struct node)); ax = ax->next; ax->Item = rand() % 100; ax->next = nullptr; } Display(a->next); struct node headb; list b = &headb; list bx = b; b->next = nullptr; list u; for (ax=a->next; ax != nullptr; ax = u) { u = ax->next; for (bx=b; bx->next != nullptr; bx = bx->next) { if (bx->next->Item < ax->Item) //倒序; { break; } } ax->next = bx->next; bx->next = ax; } cout << endl; Display(b->next); return 0; }
版权声明:
作者:朝雾之归乡
出处:http://www.cnblogs.com/cnpirate
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。