Http://zhidao.baidu.com/question/394830820.html? Oldq = 1 & from = commentto # reply-box-968647135
Problem:
Training requirements 1. Linked List Operation. Requirements: Linear tables H1 and H2 are arranged in ascending order, andAlgorithmMerging them into a linked list table H3 requires that the H3 elements also be sorted in ascending order from small to large. Specific implementation: connect the linear table H1 = (90,123,145, 98,123,146,234,366,) and H2 = (,) into a new linear table and sort the output in ascending order. Algorithm idea: scan the elements H1 and H2 in sequence, compare the values of the current element, and assign the elements with smaller values to H3 until a linear table is scanned, then, assign the remainder part of the unfinished linked list to H3. Use the chained storage structure to perform the preceding operations.
Linked List Implementation:
Package Com. Jim. test;
// Linked List
Class Link {
Node head = Null ;
Node point = Null ;
Node newnode = Null ;
Public Int Count = 0; // Statistical Value
// Insert
Public Void Addnode ( Int T ){
Newnode = New Node ();
If (Head = Null ){
Head = newnode;
} Else {
Point = head;
While (Point. Next! = Null ){
Point = point. Next;
}
Point. Next = newnode;
}
Point = newnode;
Point. vlaue = T;
Point. Next =Null ;
Count ++;
}
// Return Value
Public Int Getvalue ( Int I ){
If (Head = Null | I <0 | I> count)
Return -999999;
Int N;
Node temp = Null ;
Point = head;
For (N = 0; n <= I; n ++ ){
Temp = point;
Point = point. Next;
}
Return Temp. vlaue;
}
}
// Node
Class Node {
Int Vlaue;
Node next;
}
Public Class Test {
Public Static Void Main (string [] ARGs ){
// H1 = (90,123,145)
Link link1 = New Link ();
Link1.addnode (23 );
Link1.addnode (45 );
Link1.addnode (67 );
Link1.addnode (89 );
Link1.addnode (90 );
Link1.addnodes (123 );
Link1.addnodes (145 );
Link link2 = New Link ();
// H2 = (98,123,146,234,366)
Link2.addnode (1 );
Link2.addnode (34 );
Link2.addnode (65 );
Link2.addnode (88 );
Link2.addnode (98 );
Link2.addnode( 123 );
Link2.addnode( 146 );
Link2.addnode( 234 );
Link2.addnode( 366 );
// H3
Link link3 = New Link ();
Int I = 0, j = 0;
While (I <link1.count & J <link2.count ){
// Scan the elements through H1 and H2 in sequence, compare the values of the current element, and assign the elements with smaller values to H3.
While (Link1.getvalue (I) <= link2.getvalue (j )){
Link3.addnode (link1.getvalue (I ));
I ++;
}
While (Link1.getvalue (I)> link2.getvalue (j )){
Link3.addnode (link2.getvalue (j ));
J ++;
}
// The linked list H1 has been traversed, and the remaining content of the linked list H2 is assigned to the linked list H3.
If (I = link1.count-1 ){
While (J <link2.count ){
Link3.addnode (link2.getvalue (j ));
J ++;
}
}
// The linked list H2 has been traversed, and the remaining content of the linked list H1 is assigned to the linked list H3.
If (J = link2.count-1 ){
While (I <link1.count ){
Link3.addnode (link1.getvalue (I ));
I ++;
}
}
}
// Print h3
For ( Int K = 0; k <link3.count; k ++ ){
System. Out. Print (link3.getvalue (k) + ",");
}
}
}