Fast sorting & insert sorting of static linked lists

Source: Internet
Author: User

// 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 ");

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.