插入排序法

#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;
}

 

posted @ 2018-04-26 14:36  朝雾之归乡  阅读(298)  评论(0编辑  收藏  举报