1052. Linked List Sorting (25)
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 12345Sample Output:
5 12345 12345 -1 00001 00001 0 11111 11111 100 22222 22222 1000 33333 33333 100000 -1
/**
first read in all the nums into the list
then find the head elements , and select it from the list
and insert it into the first place of another list2 , and
select from the list1 the element which head = x .end
*/
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std ;
#define Max 1001
struct node
{
int head, end ,key ;
bool operator < ( const node &b ) const
{
return key < b.key ;
}
} ;
vector<node>list1 ;
int Num , head ;
vector<node> list2 ;
int len = 0 ;
void getInput()
{
node temp ;
scanf("%d%d", &Num , &head ) ;
for ( int i = 0 ; i < Num ; i++ )
{
scanf("%d%d%d", &temp.head, &temp.key , &temp.end ) ;
list1.push_back( temp ) ;
}
}
int findLoc( int head )
{
int loc ;
for ( int i = 0 ; i < Num ; i++ )
{
if ( list1[i].head == head )
{
loc = i ;
return loc ;
}
}
}
void deal_ ()
{
int loc = findLoc( head ) ;
list2.push_back( list1[loc]) ;
while ( list1[loc].end != -1 )
{
loc = findLoc ( list1[loc].end ) ;
list2.push_back( list1[loc] );
}
sort ( list2.begin() , list2.end() ) ;
for ( int i = 0 ; i < list2.size() -1 ; i++ )
{
list2[i].end = list2[i+1].head ;
}
list2[list2.size() -1].end = -1 ;
}
void showList()
{
if ( list2.size() == 0)
{
printf("0") ;
return ;
}
printf("%d %05d\n", list2.size() , list2[0].head) ;
for ( vector<node>::iterator it = list2.begin() ; it != list2.end() ; it++ )
{
if ( it != list2.end() -1)
{
printf( "%05d %d %05d\n", it->head , it->key , it->end ) ;
}
else
{
printf("%05d %d %d", it->head, it->key , it->end ) ;
}
}
}
int main ( void )
{
getInput() ;
deal_() ;
showList() ;
return 0 ;
}
在這個地方需要注意的是,如果數組需要的空間很大的話,就應該將vector 等容器使用數組進行替換,不然會出錯的。
if ( list1.size() == 0 )
{
printf("%d %05d", Num , head ) ;
return ;
}
不對,這裏我忘了將 list1從 vector 修改成爲了 數組,所以這裏的判斷語句爲
if ( Num == 0 )
{
....
}
/**
first read in all the nums into the list
then find the head elements , and select it from the list
and insert it into the first place of another list2 , and
select from the list1 the element which head = x .end
*/
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std ;
#define Max 1001
struct node
{
int head, end ,key ;
bool operator < ( const node &b ) const
{
return key < b.key ;
}
} ;
struct node list1[100000] ;
int Num , head ;
struct node list2[100000] ;
int len = 0 ;
void getInput2()
{
node temp ;
int nextLoc ;
scanf("%d%d", &Num , &head ) ;
for ( int i = 0; i < Num ; i++ )
{
scanf("%d%d%d", &temp.head , &temp.key , &temp.end) ;
list1[temp.head] = temp ;
}
nextLoc = head ;
while ( nextLoc != -1 )
{
list2[len++] = list1[nextLoc] ;
nextLoc = list1[nextLoc].end ;
}
}
void deal2_()
{
sort( list2 , list2+len ) ;
for ( int i = 0 ; i < len -1 ; i++ )
{
list2[i].end = list2[i+1].head ;
}
list2[len-1].end = -1 ;
}
void showList2( )
{
printf("%d %05d\n", len ,list2[0].head ) ;
for ( int i = 0 ; i < len-1 ; i++ )
{
printf("%05d %d %05d\n", list2[i].head , list2[i].key , list2[i].end ) ;
}
printf("%05d %d -1", list2[len-1].head , list2[len-1].key ) ;
}
int main ( void )
{
getInput2() ;
deal2_() ;
showList2() ;
return 0 ;
}
/**
first read in all the nums into the list
then find the head elements , and select it from the list
and insert it into the first place of another list2 , and
select from the list1 the element which head = x .end
*/
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std ;
#define Max 1001
struct node
{
int head, end ,key ;
bool operator < ( const node &b ) const
{
return key < b.key ;
}
} ;
struct node list1[100000] ;
int Num , head ;
struct node list2[100000] ;
int len = 0 ;
void getInput2()
{
node temp ;
int nextLoc ;
scanf("%d%d", &Num , &head ) ;
if ( Num == 0 )
return ;
for ( int i = 0; i < Num ; i++ )
{
scanf("%d%d%d", &temp.head , &temp.key , &temp.end) ;
list1[temp.head] = temp ;
}
nextLoc = head ;
while ( nextLoc != -1 )
{
list2[len++] = list1[nextLoc] ;
nextLoc = list1[nextLoc].end ;
}
}
void deal2_()
{
sort( list2 , list2+len ) ;
for ( int i = 0 ; i < len -1 ; i++ )
{
list2[i].end = list2[i+1].head ;
}
list2[len-1].end = -1 ;
}
void showList2( )
{
if ( len == 0 )
{
printf("%d -1", len ) ;
return ;
}
printf("%d %05d\n", len ,list2[0].head ) ;
for ( int i = 0 ; i < len-1 ; i++ )
{
printf("%05d %d %05d\n", list2[i].head , list2[i].key , list2[i].end ) ;
}
printf("%05d %d -1", list2[len-1].head , list2[len-1].key ) ;
}
int main ( void )
{
getInput2() ;
deal2_() ;
showList2() ;
return 0 ;
}