将顺序表逆置
#include<iostream>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define OVERfLOW -2
#define OK 1
using namespace std;
typedef int Status;
typedef char ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;
void revelist(Sqlist);
Status InitList_Sq(Sqlist &);
int main(){
Sqlist L;
InitList_Sq(L); //创建一个顺序表
cout<<"input a series of numbers"<<endl;
cout<<"ctrl+z to end."<<endl;
char a,i=0;
while(cin>>a){
L.elem[i]=a;
L.length++;
i++;
if(L.length>=LIST_INIT_SIZE) {
ElemType *newbase=0;
newbase=(ElemType*)realloc(L.elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem)exit(OVERfLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT; //增加存储容量
}
}
revelist(L);
for(int ix=0;ix<L.length;ix++)
cout<<L.elem[ix];
cout<<endl;
}
Status InitList_Sq(Sqlist &L){
//构造一个线性空表 L
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 revelist(Sqlist L){
for(int i=0,j=L.length-1;i<j;i++,j--)
swap(L.elem[i],L.elem[j]);
}