2121数据结构实验之链表六:有序链表的建立

数据结构实验之链表六:有序链表的建立
Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。
Input
第一行输入整数个数N;
第二行输入N个无序的整数。
Output
依次输出有序链表的结点值。
Sample Input
6
33 6 22 9 44 5
Sample Output
5 6 9 22 33 44
Hint
不得使用数组!
Source

think:排序的话无外乎选择和冒泡两种用链表来实现比较容易,链表的优点是可以跳跃的进行连接,数组的话不可以这样,但是由于不能知道前面的数,所以这也是它的一个缺点,但是可以仅仅进行数值的更换来实现相邻的或者间隔好远的数值的交换
以下是选择和冒泡两种

/*
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node*next;
};
struct node*create(int n)
{
    struct node*head,*tail,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        p=(struct node*)malloc(sizeof(struct node));
        p->data=t;
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
//选择排序:
struct node*arrange(struct node*head)
{
    struct node*t,*p,*q;
    int m=0;
    p=head->next;
    q=p->next;
    while(p!=NULL)
    {
        t=p;
        q=p->next;//关键的一步,如果没有这一步的话,
        //重新开始排的时候q还在最后面那
        while(q!=NULL)
        {
            if(q->data>=t->data)
                q=q->next;
            else
            {
                t=q;
                q=q->next;
            }
        }
        m=p->data;
        p->data=t->data;
        t->data=m;
        p=p->next;
    }
    return head;
}
//选择排序的核心是两层循环,第一层是对每一个数进行操作,第二层是固定住一个数,然后利用这个数找到最小的数,之后再进行交换
void print(struct node*head)
{
    struct node*p;
    p=head->next;
    while(p!=NULL)
    {
        if(p==head->next)
        {
            printf("%d",p->data);
        }
        else printf(" %d",p->data);
        p=p->next;
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    struct node*head,*head1;
    head=create(n);
    head1=arrange(head);
    print(head1);
    return 0;
}*/

//冒泡排序:
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node*next;
};
struct node*create(int n)
{
    struct node*head,*tail,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(int i=1;i<=n;i++)
    {
        int t;
        scanf("%d",&t);
        p=(struct node*)malloc(sizeof(struct node));
        p->data=t;
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
struct node*arrange(struct node*head,int n)
{
    struct node*p,*q;
    int m;
    for(int i=1;i<=n-1;i++)
    {
        p=head->next;
        q=p->next;
        while(p->next!=NULL)//两个变量一起跑的时候是这样写的
        {
            if(q->data<p->data)//其上的数字进行排序
            {
                m=p->data;
                p->data=q->data;
                q->data=m;
            }
            p=p->next;
            q=q->next;
        }
    }
    return head;
}
//冒泡排序的核心是相邻的两个数进行交换,使得每个数按照大小从后往前进行归位,第一个数就不用归位了,所以要跑N-1趟
void print(struct node*head)
{
    struct node*p;
    p=head->next;
    while(p!=NULL)
    {
        if(p==head->next)
        {
            printf("%d",p->data);
        }
        else printf(" %d",p->data);
        p=p->next;
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    struct node*head,*head1;
    head=create(n);
    head1=arrange(head,n);
    print(head1);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值