PAT 1052. Linked List Sorting-map/qsort usage __ Algorithm Learning

Source: Internet
Author: User
Tags int size


Original title Link http://pat.zju.edu.cn/contests/pat-a-practise/1052

1052. Linked List sorting ( time limit) ms
Memory Limit 32000 KB
Code length limit 16000 B
The Standard of the Judgment procedure

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

Input Specification:

Each input file contains one test case. For each case, the contains a positive n (<) and the "head node" where n is 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 "node in memory", the Key is a integer in [ -105, +], and next is the address of the next Node. It is guaranteed this all the keys are distinct and there are no cycle in the linked list starting to the head node.

Output Specification:

For each test case, the output format is the same as this input, where N is the total number of nodes in the list A nd all the nodes must is 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 22222 22222 1000 33333
33333 100000-1
From the given node, find a chain. The key is then displayed in a small to large order.

The code is as follows:
#include <iostream> #include <fstream> #include <map> #include <vector> #include <iomanip

> Using namespace std;
Ifstream fin ("in.txt");
	#define CIN fin struct Node {int key;
	int next;

Node () {} node (int addr,int k) {key = K;next = addr;}};
	int cmp (const void* a,const void* B) {node* AA = (node*) A;
	node* BB = (node*) B;
Return aa->key-bb->key;
	int main () {int n;
	int head,addr;
	fin>>n>>head;		Map<int,node> m;
	Constructing an index set with address as key vector<node> V;
	node* node = new Node[n];
	int i;
		for (i=0;i<n;i++) {fin>>addr>>node[i].key>>node[i].next;
	M[addr]=node[i];
	int findkey = head;
		while (FindKey!=-1)//searching for the chain by index value {map<int,node>::const_iterator map_it = M.find (FindKey);
			if (Map_it!= m.end ()) {Node NewNode (FindKey, Map_it->second.key);
			V.push_back (NewNode);
		FindKey = map_it->second.next;
		}else {break;
	int size = V.size (); if (size==0) {cout<<0<< ' ' <<-1<<endl;
		System ("PAUSE");
	return 0;
	}//sort (V.begin (), V.end ());
	Qsort (& *v.begin (), size,sizeof (Node), CMP);
	
	cout<<size<< "" &LT;&LT;SETW (5) <<setfill (' 0 ') <<v.begin ()->next<<endl; for (i=0;i<size;i++) {COUT&LT;&LT;SETW (5) <<setfill (' 0 ') <<v[i].next<< ' <<v[i].key<
		< ';
		if (i!= size-1) {COUT&LT;&LT;SETW (5) <<setfill (' 0 ') <<v[i+1].next<<endl;
		}else {cout<<-1<<endl;
    } System ("PAUSE");
return 0;
 }






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.