实现顺序表的逆置

#include<stdio.h>   //头文件,输入输出操作主要使用

#include<stdlib.h>  //头文件,包含本程序中的malloc,free system函数

#define MAXSIZE 10	//宏定义,定义常量MaxSize

#define OK 1

#define ERROR 0

#define OVERFLOW -2



typedef int ElemType;  //定义类型ElemType,本例中使用的数据元素类型为int型

//顺序表类型SqList的定义



typedef struct List

{

	ElemType *elem;	//存放顺序表的元素

	int length;				//顺序表的实际长度

}SqList;

InitList(SqList &L)

{

    L.elem=(ElemType *)malloc(MAXSIZE * sizeof(ElemType));   //为顺序表分配空间

    if(!L.elem) exit(OVERFLOW);       //存储分配失败

    L.length=0;	            	  //空表长度为0

    return OK;

}

void DestroyList(SqList &L)

{

	if (L.elem)  free(L.elem);

}

int ListEmpty(SqList L)

{

	if(L.length==0)

		return 1;

	else

		return 0;

}

int ListLength(SqList L)

{

	return L.length;

}

int GetElem(SqList L,int i,ElemType &e)

{

	if (i<1 || i>L.length)	//无效的i值返回0

		return ERROR;

	else

	{

		e=L.elem[i-1];		//取元素值并返回1

		return OK;

	}

}

void DisplayList(SqList L)

{

	if(L.length==0)

		return;

	else

	{

		for(int i=0;i<L.length;i++)

			printf("%d  ",L.elem[i]);

		printf("\n");

	}

}

int LocateElem(SqList &L,ElemType e)	

{

	int i;

	for (i=0;i< L.length;i++)

      if (L.elem[i]==e) return i+1;                

    return 0;

}

int ListInsert(SqList &L,ElemType e,int i)

{

	int j;

	if(i<1 || i>L.length+1) return ERROR;	       //i值不合法

    if(L.length==MAXSIZE) return ERROR;   //当前存储空间已满     

    for(j=L.length-1;j>=i-1;j--) 

       L.elem[j+1]=L.elem[j];    //插入位置及之后的元素后移

    L.elem[i-1]=e;                     //将新元素e放入第i个位置

    ++L.length;		    //表长增1

    return OK;

}

int ListDelete(SqList &L,int i,ElemType &e)

{

	int j;

    if((i<1)||(i>L.length)) return ERROR;	 //i值不合法

    e=L.elem[i-1];

    for (j=i;j<=L.length-1;j++)                   

      L.elem[j-1]=L.elem[j];   //被删除元素之后的元素前移

    --L.length;               	      //表长减1

    return OK;

}

//逆置

void ReverseList(SqList &L){

	int t;

	for(int i = 0; i < L.length/2; i++){

		t = L.elem[i];

		L.elem[i] = L.elem[L.length-1-i];

		L.elem[L.length-1-i] = t;

		

	} 

} 







void menu()     //菜单函数,显示实验中的主菜单

{

	printf("----------顺序表的定义和基本操作----------\n");

	printf("-         1  初始化顺序表                -\n");

	printf("-         2  销毁顺序表                  -\n");

	printf("-         3  判断表是否为空              -\n");

	printf("-         4  求表长                      -\n");

	printf("-         5  输出顺序表内容              -\n");

	printf("-         6  按位置取值                  -\n");

	printf("-         7  按值查找                    -\n");

	printf("-         8  插入数据元素                -\n");

	printf("-         9  删除数据元素                -\n");

	printf("-         10 顺序表的逆置                -\n");

	printf("-         0  退出                        -\n");

	printf("请输入菜单对应的数字:");

}



int main()				//主函数
{
	SqList L;			//实参:顺序表L的指针

	ElemType e;			//实参:用来接收或上传顺序表元素的值

	int i,k;			//实参:i用来接收或提交顺序表元素的位置,k是菜单选择编号

	for(;;)

	{

		menu();			//调用菜单函数

		scanf("%d",&k); 

		switch(k)

		{

		case 1: InitList(L);

				printf("顺序表L已初始化,当前表长为%d。\n",ListLength(L));				

				system("pause");		//系统函数,暂停界面,显示结果,按任意键继续

				system("cls");			//系统函数,清除屏幕上的运行结果和上一次输出的菜单

				break;

		case 2: DestroyList(L);

				printf("顺序表L已销毁。\n");				

				system("pause");

				system("cls");

				break;

		case 3:	if(ListEmpty(L))

					printf("当前顺序表为空。\n");

				else

					printf("当前顺序表非空。\n");

				system("pause");

				system("cls");

				break;

		case 4:	printf("当前顺序表表长为:%d\n",ListLength(L));

				system("pause");

				system("cls");

				break;

		case 5:	printf("当前顺序表中的数据为:");

				DisplayList(L);

				system("pause");

				system("cls");

				break;

		case 6:	printf("请输入查找位置1-%d:",ListLength(L));

				scanf("%d",&i);

				if(GetElem(L,i,e))

					printf("%d位置上的数据是%d\n。",i,e);

				else

					printf("没有找到,请核实查找位置是否正确。\n");

				system("pause");

				system("cls");

				break;

		case 7:	printf("请输入要查找的数据:");

				scanf("%d",&e);

				if(i=LocateElem(L,e))

					printf("%d是顺序表中的第%d个数据。\n",e,i);

				else

					printf("%d不存在此顺序表中。\n",e);

				system("pause");

				system("cls");

				break;

		case 8:	printf("请输入要插入的数据:");

				scanf("%d",&e);

				printf("请输入要插入的位置(1-%d):",ListLength(L)+1);

				scanf("%d",&i);

				if(ListInsert(L,e,i))

					printf("插入成功!\n");

				else

					printf("插入失败,请核实插入位置。\n");

				system("pause");

				system("cls");

				break;

		case 9:	printf("请输入要删除的数据位置(1-%d):",ListLength(L));

				scanf("%d",&i);

				if(ListDelete(L,i,e))

					printf("删除成功!你删除的数据是:%d\n",e);

				else

					printf("删除失败,请核实插入位置。\n");

				system("pause");

				system("cls");

				break;

		case 10: DisplayList(L);
		         ReverseList(L);
		         printf("顺序表L的逆置\n");

		         DisplayList(L);
		         system("pause");
				 system("cls");
				 break;
				 
		case 0: return 0;		//输入0的时候退出主
	
	}
	}
}




  • 13
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W.wda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值