// This program is suitable for running in turbo c ++. Many internal comments are for debugging and use by myself, so that the error is not removed.
/* Insert sorting for static connected tables */
# Define SIZE 100
# Define KeyType int
# Define MAXINT 30000
# Include <stdio. h>
# Include <stdlib. h>
# Include "iostream. h"
Int safe = 0;
Typedef struct {
KeyType key;
Int next;
} SNode;
Typedef struct {
SNode r [SIZE];
Int length;
} SLink;
Void outputkey (SLink * sl );
Void outputnext (SLink * sl );
Void Soutput (SLink * sl );
Void Arrange (SLink *);
Void main (){
SLink SL;
Int index, pre_index;
Int I;
Printf ("Now bigin to input the list: nFirst input the length :");
Scanf ("% d", & SL. length );
Printf ("please input % d elementsn", SL. length );
For (I = 1; I <= SL. length; I ++)
{Scanf ("% d", & (SL. r [I]. key ));
}
Printf ("Review the array :");
Outputkey (& SL );
SL. r [0]. key = MAXINT;
SL. r [0]. next = 1;
SL. r [1]. next = 0;
For (I = 2; I <= SL. length; I ++ ){
Pre_index = 0;
Index = SL. r [pre_index]. next;
Do {
// Cout <"I:" <I <"index:" <index <endl;
If (SL. r [I]. key <= SL. r [index]. key)
{SL. r [pre_index]. next = I;
SL. r [I]. next = index;
Break;
}
Else {
Pre_index = index;
Index = SL. r [index]. next;
}
Safe ++;
If (safe> 15) {cout <"Error occured! N "<endl; exit (0 );}
} While (index <I );
}/* End */
Cout <"final check :";
Outputkey (& SL );
Soutput (& SL );
Cout <"Final array :";
Arrange (& SL );
Outputnext (& SL );
} // End main
Void outputkey (SLink * sl ){
Int I;
For (I = 0; I <= sl-> length; I ++ ){
Printf ("% d", sl-> r [I]. next );
}
Printf ("n ");