秩序チェーンテーブルの作成

1422 ワード

データ構造実験のチェーンテーブル6:秩序チェーンテーブルの構築
TimeLimit: 1000ms Memory limit: 65536K
タイトルの説明
N個の無秩序な整数を入力して、1つの秩序チェーンテーブルを創立して、チェーンテーブルの中のノードは数値の非降順に並べて、この秩序チェーンテーブルを出力します.
入力
1行目は整数個数Nを入力する.
2行目にN個の無秩序な整数を入力します.
しゅつりょく
順序付きチェーンテーブルのノード値を順次出力します.
サンプル入力
6
336 22 9 44 5
サンプル出力
56 9 22 33 44
#include <bits/stdc++.h>
#define RR freopen("input.txt","r",stdin)
#define WW freopen("ouput.txt","w",stdout)

using namespace std;

struct node
{
    int data;
    node *next;
};

void insret(node *head,node *q)
{
    node *p,*r;
    r=head;
    p=head->next;
    while(p)
    {
        if(q->data<p->data)
        {
            q->next=p;
            r->next=q;
            break;
        }
        p=p->next;
        r=r->next;
    }
    if(!p)
    {
        q->next=p;
        r->next=q;
    }
}

void Output(node *head)
{
    node *p;
    p=head->next;
    while(p)
    {
        if(p!=head->next)
            cout<<" ";
        cout<<p->data;
        p=p->next;
    }
    cout<<endl;
}
int main()
{
    node *head,*q;
    int n;
    head=new node;
    head->next=NULL;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        q=new node;
        cin>>q->data;
        insret(head,q);
    }
    Output(head);
    return 0;
}