#include
const int MAX = 20; //设定这个顺序表的最大空间为20,也就是最多可以存20个int数
typedef int dataType; //数据类型为int型
typedef struct seqlist
{
dataType a[MAX]; //声明这条顺序表存储数据的工具————数组(一段连续的地址空间,所以我们称这种方式为顺序存储)
int size; //数据元素个数
}SeqList;
//初始化顺序表
void listInitiate(SeqList *seqlist)
{
seqlist->size = 0; //刚创建顺序表之后的数据个数为0
}
//往顺序表中放10个数
void listOriginal(SeqList *seqlist)
{
for (int i = 0; i < 10; i++)
{
seqlist->a[i] = i*4;
seqlist->size++; //多了一个数据元素,所以size得+1;
}
}
//将顺序表逆置
SeqList* listReserve(SeqList* seqlist)
{
int t;
for (int i = 0; i < seqlist->size / 2; i++)
{
t = seqlist->a[i];
seqlist->a[i] = seqlist->a[seqlist->size - 1 - i];
seqlist->a[seqlist->size - 1 - i] = t;
}
return seqlist;
}
//专门用来遍历输出的函数
void listPrint(SeqList *seqlist)
{
printf("顺序表:");
for (int i = 0; i < seqlist->size; i++)
printf("%d ", seqlist->a[i]);
printf("\n");
}
int main()
{
SeqList seqlist; //定义两条顺序表
printf("长度为奇数时:\n");
listInitiate(&seqlist); //初始化
listOriginal(&seqlist); //往链表里面放十个数
listPrint(&seqlist); //输出原顺序表
printf("就地逆置后的");
listReserve(&seqlist); //逆置顺序表
listPrint(&seqlist); //输出逆置后的顺序表
printf("\n长度为偶数时:\n");
seqlist.a[seqlist.size++] = -1;//往顺序表尾添加一个数据,使之长度变成偶数
listPrint(&seqlist);
printf("就地逆置后的");
listReserve(&seqlist);
listPrint(&seqlist);
return 0;
}[/prism]
代码编译器:Dev-C++