插入排序法

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章