PAT1052. Linked List Sorting

Source: Internet
Author: User

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

Input Specification:

Each input file contains the one test case. For each case, the first line contains a positive N (< +) and an address of the head node, where N was the total Numbe  R 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 that node in memory, a Key is a integer in [ -105, +], and next is the address of the next  Node. It is guaranteed, all the keys, distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

The output format is the same as that of the input, where N was the total number of nodes in the list A nd all the nodes must is sorted order.

Sample Input:

5 0000111111 100-100001 0 2222233333 100000 1111112345-1 3333322222 1000 12345

Sample Output:

5 1234512345-1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000-1
Idea: The use of vectors is a good way, another point to note is all invalid nodes. Be sure to make a special output
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <vector>5 using namespacestd;6 Const intMAX =100010;7 structInfo8 {9     intaddress;Ten     intdata; One     intNext; A };  - BOOLCMP (Info a,info B) - { the  -    returna.data<B.data; - } -  +  - intMainintargcChar*argv[]) + { A     intN,start; atscanf"%d%d",&n,&start); -Vector<info>inch(MAX); -Vector<info> out; -      for(intI=0; i<n;i++) -     { - Info node; inscanf"%d%d%d",&node.address,&node.data,&node.next); -         inch[node.address]=node; to     } +     intpt=start; -      while(pt!=-1) the     { *          out. push_back (inch[pt]); $pt=inch[Pt].next;Panax Notoginseng     } -Sort out. Begin (), out. End (), CMP); the     if( out. Size ()! =0) +printf"%d%05d\n", out. Size (), out[0].address); A     Else theprintf"%d-1\n", out. Size ()); +      for(intI=0;i< out. Size (); i++) -     { $         if(i== out. Size ()-1) $printf"%05d%d-1\n", out[I].address, out[I].data]; -         Else -printf"%05d%d%05d\n", out[I].address, out[I].data, out[i+1].address); the     } -     Wuyi     return 0; the}
View Code

PAT1052. Linked List Sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.