65,210
社区成员
发帖
与我相关
我的任务
分享
#include<iostream.h>
#include<conio.h>
#define LIST_INIT_SIZE 100
#define LISTICREMENT 10
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
void InitList_Sq(SqList &L)
{
L.elem=new int(LIST_INIT_SIZE);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
void ListInsert_Sq(SqList &L,int i,int e)
{
int *p,*q;
if(i<1||i>L.length+1)
cout<<"ERROR"<<endl;
if(L.length>=L.listsize)
{
int *newbase;
newbase=new int(L.listsize+LISTICREMENT);
if(!newbase)cout<<"ListInsert fail!"<<endl;
L.elem=newbase;
L.listsize+=LISTICREMENT;
}
q=&(L.elem[i]);
for(p=&(L.elem[L.length]);p>=q;--p) *(p+1)=*p;
*q=e;
++L.length;
}
void shelli(SqList &L,int d)
{
for (int i=d+1;i<=L.length;++i)
if(L.elem[i]<=L.elem[i-d])
{
L.elem[0]=L.elem[i];
for(int j=i-d;j>0&&L.elem[0]<=L.elem[j];j-=d)
L.elem[j+d]=L.elem[j];
L.elem[j+d]=L.elem[0];
}
}
void shells(SqList &L,int t)
{
for(int k=t;k>=1;k=k/2)
shelli(L,k);
}
void main()
{
SqList Sq;
InitList_Sq(Sq);
int i,x,n;
cout<<"how much elem the list have?"<<endl;
cin>>n;
cout<<"please input the elem you want push into the list!"<<endl;
for(i=1;i<=n;i++)
{
cin>>x;
ListInsert_Sq(Sq,i,x);
}
for(i=1;i<=n;i++) cout<<Sq.elem[i]<<" ";
cout<<endl;
shells(Sq,n/2);
cout<<endl;
for(i=1;i<=n;i++) cout<<Sq.elem[i]<<" ";
cout<<endl;
}