zoukankan      html  css  js  c++  java
  • 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;
    
    }
    
     


  • 相关阅读:
    35.python之事件驱动模型
    33.python之操作系统,进程,线程
    VRChat之blender2.8版本设置
    VRChat模型制作及上传总篇(201912)
    linux常用指令
    java基础File的简单使用记录
    java 通过实现Comparable接口使用Collections.sort排序 及 利用set的特性对 list 进行去重
    [转发] java字符串-编码转换-工具类
    ssh服务不能远程时,使用telnet远程登录
    java中List遍历删除元素相关做法和注意事项
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4234437.html
Copyright © 2011-2022 走看看