//顺序表的就地逆置
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW 0
#define OK 1
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;//定义结构类型
Sqlist L;
Status InitList_Sq()
{
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)) ;
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}//顺序表初始化操作
void Input()
{
int i=0,n;
printf("Plese input datas except 0\n");
printf("Type 0 for stopping input!\n");
scanf("%d",&n);
while(n!=0&&i<100){
L.elem[i]=n;
L.length++;
i++;
scanf("%d",&n);
}
printf("The contents of the sqlist are\n");
for(i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
}//输入数据
void Reverse(Sqlist *L)
{
int i,temp;
int low=0;
int high=L->length-1;
for(i=0; i<L->length/2;i++){
temp=L->elem[low];
L->elem[low]=L->elem[high];
L->elem[high]=temp;
low++;
high--;
}
}//就地逆置
void Output(Sqlist L)
{
printf("The contents of the reversed sqlist are\n");
int i;
for(i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
printf("\n");
}//输出数据
void Insert()
{
int i,j,x;
printf("Please input the data that you want to intert:");
scanf("%d",&x);
for(i=0;i<L.length;i++)
{
if(x<L.elem[i])
{
for(j=L.length;j==i;j--)
{
L.elem[j]=L.elem[j-1];
}
L.elem[i]=x;
}
}
printf("The new contents of the sqlist are:");
for(i=0;i<L.length;i++)
printf("%d ",L.elem[i]);
printf("\n");
}//插入一个x后仍有序
int main(){
InitList_Sq();
Input();
Reverse(&L);
Output(L);
Insert();
}
转载于:https://my.oschina.net/hfancy/blog/644398