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 ;
}