• Linked List Sorting (链表)


    Linked List Sorting (链表)

     

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

     

    Input Specification:

     

    Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.

     

    Then N lines follow, each describes a node in the format:

     

    Address Key Next

     

    where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

     

    Output Specification:

     

    For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

     

    Sample Input:

    5 00001

    11111 100 -1

    00001 0 22222

    33333 100000 11111

    12345 -1 33333

    22222 1000 12345

    Sample Output:

    5 12345

    12345 -1 00001

    00001 0 11111

    11111 100 22222

    22222 1000 33333

    33333 100000 -1

     

     

    坑点:1、可能是空链表  2、并不是每个节点都在链表上的


    #include <iostream>
    
    #include <vector>
    
    #include<iomanip>
    
    #include <algorithm>
    
    using namespace std;
    
     
    
    struct node
    
    {
    
      int add;
    
      int data;
    
      int next;
    
    };
    
     
    
    node a[100000],b[100000];
    
     
    
    bool cmp(node n1,node n2)
    
    {
    
       return n1.data<n2.data;
    
    }
    
     
    
    int main()
    
    {
    
     
    
          int len,fir,add,data,next;
    
          int i;
    
          while(cin>>len)
    
          {
    
     
    
                cin>>fir;
    
          
    
     
    
                for(i=0;i<len;i++)
    
                {
    
                   cin>>add>>data>>next;
    
                   node n;
    
                   n.add=add;
    
                   n.data=data;
    
                   n.next=next;
    
                   a[add]=n;
    
                }
    
     
    
                if(fir==-1)
    
                {
    
                  cout<<"0 -1"<<endl;
    
                  continue;
    
                }
    
            i=0;
    
                while(fir!=-1)
    
                {
    
                   b[i].add =a[fir].add;
    
                   b[i].data =a[fir].data;
    
                   b[i].next =a[fir].next;
    
                   fir = a[fir].next;
    
                   ++i;
    
                }
    
              int len=i;
    
             sort(b,b+len,cmp);
    
     
    
                for(i=0;i<len-1;i++)
    
                {
    
                    b[i].next=b[i+1].add;
    
                }
    
               
    
     
    
                b[len-1].next=-1;
    
     
    
                cout<<len<<" "<<setfill('0')<<setw(5)<<b[0].add<<endl;
    
                for(i=0;i<len;i++)
    
                {
    
                     if(b[i].next==-1)
    
                            cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<-1<<endl;
    
                      else
    
                            cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<setfill('0')<<setw(5)<<b[i].next<<endl;
    
     
    
                }
    
          }
    
     
    
      return 0;
    
    }
    
     


  • 相关阅读:
    axis2学习笔记
    一个奇怪的数组越界报错
    zk实现分布式锁
    springBoot配置双数据源
    oracle查询
    maven项目打包
    linux修改yum源为阿里云
    kafka入门
    大话设计模式读书笔记(中介者模式)
    大话设计模式读书笔记(职责链模式)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4234437.html
Copyright © 2020-2023  润新知