题目内容:
用程序实现“顺序表”的以下 *** 作:
①初始化、取值、查找、插入、删除、清空、退出...——存为“头文件”;
②主函数调用以上 *** 作——用switch。
个人解法如下:
1.自定义的头文件
#include
#define Maxsize 100
#define Succeed 1
#define Error 0
#define Overflow -1
typedef int Status;
typedef int ElemType;
typedef struct
{
int* elem;
int length;
}SqList;
Status InitList_Sq(SqList& L)
{
L.elem = (int*)malloc(sizeof(int) * Maxsize);
if (!L.elem)
return Overflow;
L.length = 0;
return Succeed;
}
Status GetElem_Sq(SqList& L, int i, ElemType e)
{
if (i<1 || i>L.length)
return Error;
e = L.elem[i - 1];
return Succeed;
}
Status ListInsert_Sq(SqList& L, int i, ElemType e)
{
if (i<1 || i>L.length+1)
return Error;
if (L.length == Maxsize)
return Overflow;
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;
++L.length;
return Succeed;
}
Status ListDelete_Sq(SqList& L, int i)
{
if (i<1 || i>L.length)
return Error;
if (L.length == Maxsize)
return Overflow;
for (int j = i - 1; j < L.length - 1; j++)
L.elem[j] = L.elem[j + 1];
--L.length;
return Succeed;
}
int LocateElem_Sq(SqList& L, ElemType e)
{
for (int i = 0; i < L.length; i++)
if (L.elem[i] == e)
return i + 1;
return 0;
}
void CreateList_Sq(SqList& L)
{
int n, i;
printf("\n请输入数据元素个数:");
scanf("%d", &n);
printf("\n请输入%d个数据元素:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &L.elem[i]);
L.length++;
}
}
void PrintList_Sq(SqList& L)
{
if (!L.elem)
printf("\n顺序表不存在!");
else if (L.length)
{
printf("\n该顺序表的元素有:\n");
for (int i = 0; i < L.length; i++)
printf("%d\n", L.elem[i]);
}
else
printf("\n顺序表不存在!");
}
void DestroyList_Sq(SqList& L)
{
if (L.elem)
free(L.elem);
L.elem = NULL;
}
void ClearList_Sq(SqList& L)
{
L.length = 0;
}
2.主文件(注意主文件内调用的头文件名须与自定义的头文件名相同)
#define _CRT_SECURE_NO_WARNINGS //防报错
#include
#include"SqList.h"
int main()
{
system("color 3");
SqList TargetList;
ElemType Value;
int Location, Location2, Choice;
if (InitList_Sq(TargetList) == Succeed)
{
printf("顺序表初始化成功!\n");
while (1)
{
system("cls");
printf("0--------结束\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("\n请选择你所需要的 *** 作项:\n");
scanf("%d", &Choice);
switch (Choice)
{
case 0:
exit(0);
case 1:
CreateList_Sq(TargetList);
case 2:
PrintList_Sq(TargetList);
case 3:
printf("\n请输入欲获取的元素的位置:\n");
scanf("%d", &Location);
if (GetElem_Sq(TargetList, Location, Value) == Succeed)
printf("获取的元素的值为:%d\n", Value);
else
printf("获取失败,请检查输入的元素的位置!\n");
break;
case 4:
printf("\n请输入欲查找的元素的值:\n");
scanf("%d", &Value);
if (Location2 = LocateElem_Sq(TargetList, Value))
printf("查找的元素的位置为:%d\n", Location2);
else
printf("查找失败,请检查输入的元素的值!\n");
break;
case 5:
printf("\n请输入欲插入的元素的值:\n");
scanf("%d", &Value);
printf("请输入欲插入的元素的位置:\n");
scanf("%d", &Location);
if (GetElem_Sq(TargetList, Location, Value) == Succeed)
PrintList_Sq(TargetList);
else
printf("插入失败,请检查输入的元素的位置!\n");
break;
case 6:
printf("\n请输入欲删除的元素的位置:\n");
scanf("%d", &Location);
if (ListDelete_Sq(TargetList, Location) == Succeed)
PrintList_Sq(TargetList);
else
printf("删除失败,请检查输入的元素的位置!\n");
break;
case 7:
DestroyList_Sq(TargetList);
case 8:
ClearList_Sq(TargetList);
default:
printf(" *** 作项不存在,请重新选择!\n");
}system("pause");
}
printf("缓存已被清除!\n");
}
else
printf("顺序表初始化失败!\n");
return 0;
}
运行结果:
本人拙作,请大佬们点评。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)