文章

246

粉丝

0

获赞

1179

访问

73.7k

头像
链表的插入排序 题解:
P5280
发布于2026年3月29日 15:15
阅读数 208

#include<iostream>
using namespace std;

int n;

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

void init(node *l)
{
    l->val=-5001;
    l->next=NULL;
}

void sort(node *l)
{
    int x;
    node *p=l->next;
    while(cin>>x)
    {
        node *q=new node;
        q->val=x;
        if(p->val<x)
        {
            q->next=p->next;
            p->next=q;
            p=q;
        }
        else
        {
            node *pre=l,*cur=l->next;
            while(cur!=NULL)
            {
                if(pre->val<=x&&cur->val>x)
                break;
                pre=cur;
                cur=cur->next;
            }
            q->next=pre->next;
            pre->next=q;
        }
    }
}

int main()
{
    cin>>n;
    node *head=new node;
    init(head);
    int x;
    cin>>x;
    node *p=new node;
    p->val=x;
    p->next=head->...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发