链表插入排序法———排序问题


排序问题

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入10个整数,将它们从小到大排序后输出,并给出现在每个元素在原来序列中的位置。

输入

输入数据有一行,包含10个整数,用空格分开。

输出

输出数据有两行,第一行为排序后的序列,第二行为排序后各个元素在原来序列中的位置。

示例输入

1 2 3 5 4 6 8 9 10 7

示例输出

1 2 3 4 5 6 7 8 9 10
1 2 3 5 4 6 10 7 8 9
 
 
这虽然是个很简单的问题,但是练一练链表的插排还是不错的。。。。。。。
 
 
 
 
 
 
 
 
#include <stdio.h>
#include <stdlib.h>

struct linshi
{
    int num;
    int no;
    struct linshi *next;
};

struct linshi *head = NULL;
struct linshi *ls = NULL;
struct linshi *p = NULL;
struct linshi *q = NULL;

int main()
{
    int n=10,i=0;
    head=(struct linshi *)malloc(sizeof(struct linshi)*1);
    head->next=NULL;
    p=(struct linshi *)malloc(sizeof(struct linshi)*1);
    p->next=NULL;
    q=(struct linshi *)malloc(sizeof(struct linshi)*1);
    q->next=NULL;
    i=0;
    while(n--)
    {
        i++;
        ls=(struct linshi *)malloc(sizeof(struct linshi)*1);
        ls->next=NULL;
        scanf("%d",&ls->num);
        ls->no=i;
        if(n==9)
        {
            head->next=ls;
            ls->next=NULL;
        }
        else
        {
            q=head;
            p=head->next;
            while(p!=NULL)
            {
                if(ls->num<p->num)
                {
                    q->next=ls;
                    ls->next=p;
                    break;
                }
                q=p;
                p=p->next;
            }
            if(p==NULL)
            {
                q->next=ls;
                q=ls;
                q->next=NULL;
            }
        }
    }
    p=head->next;
    while(p!=NULL)
    {
        printf("%d",p->num);
        if(p->next!=NULL)
            printf(" ");
        p=p->next;
    }
    printf("\n");
    p=head->next;
   while(p!=NULL)
    {
        printf("%d",p->no);
        if(p->next!=NULL)
            printf(" ");
        p=p->next;
    }
    return 0;
}


 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值