/**********************************************************************
程序作者:冰糖
章 节:第一次上机
题 号:2.1 顺序
题 目:设元素值为整型的线性表L,分别采用顺序结构和链式结构存储,
编写程序,实现线性表的就地逆置(习题集P18 2.21, 2.22)。
最后修改日期:2019年10月19日
***********************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int Position;
typedef int ElemType;
typedef void Status;
typedef struct
{
ElemType* elem;
Position length;
}List;
Status InitList(List &L);
Status ReverseList(List &L);
int main()
{
List L;
int i,t=0;
InitList(L);
for(i=0;i<5;i++)
{
L.elem[i]=t+1;
t++;
L.length++;
}
for(i=0;i<L.length;i++)
{
printf("%d\n",L.elem[i]);
}
printf("************************************逆转后:\n");
ReverseList(L);
for(i=0;i<L.length;i++)
{
printf("%d\n",L.elem[i]);
}
return 0;
}
Status InitList(List &L)
{
L.elem=(ElemType*)malloc(MAXSIZE*(sizeof(ElemType)));
if(!L.elem)
{
printf("分配内存失败!");
exit(0);
}
L.length=0;
}
Status ReverseList(List &L)
{
int i;
for(i=0;i<L.length/2;i++)
{
L.elem[i]=L.elem[i]+L.elem[L.length-i-1];
L.elem[L.length-i-1]=L.elem[i]-L.elem[L.length-i-1];
L.elem[i]=L.elem[i]-L.elem[L.length-i-1];
}
}
数据结构 上机1 2.1 顺序结构实现线性表的就地逆置
最新推荐文章于 2024-02-06 23:51:45 发布