#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR -1
#define MAX_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct sqlist
{
ElemType Elem_array[MAX_SIZE];
int length;
}Sqlist;
//插入运算
void PrintList(Sqlist *L)
{
int i;
for(i = 0;i<L->length;i++)
{
printf("%d\n",L->Elem_array[i]);
}
}
Status CreatList(Sqlist *L)
{
int len;
int i;
//ElemType q;
printf("请输入想要创建表的长度:");
scanf("%d",&len);
if(len<0||len>MAX_SIZE)
return ERROR;
L->length = len;
for(i = 0;i<len;i++)
{
scanf("%d",&L->Elem_array[i]);
}
}
Status ReverseList(Sqlist *L)
{
int i;
ElemType temp;
if(L->length==0)
{
printf("表为空!\n");
return ERROR;
}
else
{
for(i = 0;i<L->length/2;i++)
{
temp = L->Elem_array[i];
L->Elem_array[i] = L->Elem_array[L->length-i-1];
L->Elem_array[L->length-i-1]=temp;
}
}
}
void main()
{
Sqlist l;
l.length = 0;
//int l->Elem_array[MAX_SIZE];
//InsertElem(&l,1,2);
//InsertElem(&l,2,2);
//InsertElem(&l,3,2);
CreatList(&l);
printf("线性表的值如下:\n");
PrintList(&l);
ReverseList(&l);
printf("线性表逆置之后的值如下:\n");
PrintList(&l);
}
上述程序运行结果如下:
我在编程序时犯的一个低级错误:
就是在交换数据时,写成了下面的程序:
L->Elem_array[i] = temp;
temp = L->Elem_array[L->length-i-1];
L->Elem_array[L->length-i-1] = L->Elem_array[i];
把交换顺序弄反了。