最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
快速排序&静态链表的插入排序
时间:2022-07-02 10:57:30 编辑:袖梨 来源:一聚教程网
//本程序适合在turbo c++ 运行,内部的许多注释是本人在调试使用的,以跟踪错误所在并未去掉
/*静态连表插入排序*/
#define SIZE 100
#define KeyType int
#define MAXINT 30000
#include
#include
#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:"< 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;
}
} /*end for*/ void outputkey(SLink* sl){
safe++;
if(safe>15){cout<<"Error occured!n"<
cout<<"final check:";
outputkey(&SL);
Soutput(&SL);
cout<<"Final array:";
Arrange(&SL);
outputnext(&SL);
}//end main
int i;
for(i=0;i<=sl->length;i++){
printf("%d ",sl->r[i].next);
}
printf("n");相关文章