1.绪论
线性表
顺序表九大操作
直接上代码
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define MaxSize 100
#define ElemType int
#define Status int
#define True 1;
#define false 0;
using namespace std;
//顺序表结构
typedef struct
{
ElemType data[MaxSize];//元素
int length; //顺序表当前长度
}SqList; //给顺序表取个别名
/************************九大基本操作************************************/
//1.初始化顺序表,顺序表长度初始化为0
Status InitList(SqList &L)
{
if (L.length != 0)
{
L.length = 0;
return True;
}
else
{
return True;
}
}
//2.清空顺序表
Status ClearList(SqList &L)
{
L.length = 0;
return True;
}
//3.判断表是否为空,如果为空,返回True,否则返回false
Status isListEmpty(SqList L)
{
if (L.length == 0)
{
return True;
}
else
{
return false;
}
}
//4.返回书虚表中数据元素的个数,也就是顺序表长度
Status ListLengt(SqList L)
{
return L.length;
}
//5.返回顺序表中第i个数据元素的值
Status GetElem(SqList L, int i, ElemType e)
{
//判断i的位置是否合法
if (i > L.length || i < 1)
{
printf("输入的i值不合法!\n");
return false;
}
if (L.length == 0)
{
printf("顺序表为空,没有元素!\n");
return false;
}
e = L.data[i-1];
return e;
}
//6.返回顺序表中第一个和e相等元素的位置
Status LocateElem(SqList L, ElemType e)
{
int i;
if (L.length == 0)
{
printf("该顺序表为空!\n");
return false;
}
for ( i = 0; i < L.length; i++)
{
if (L.data[i] == e)
{
break;
}
}
if (i >= L.length)
{
printf("该顺序表中没有和%d相同的元素\n", e);
}
return i+1;
}
//7.在顺序表i的位置插入数据e
Status ListInsert(SqList &L, int i, ElemType e)
{
//判断插入位置i是否合法
if (i > L.length + 1 || i < 1)
{
printf("i的位置不合法!\n");
return false;
}
//判断顺序表长度是否超出最大长度
if (L.length == MaxSize)
{
printf("顺序表长度已满,无法继续插入!\n");
return false;
}
for (int j = L.length; j >= i; j--)
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;
L.length++;
return True;
}
//8.删除顺序表的第i个元素,并返回被删除元素位置的值
Status ListDel(SqList &L, int i, ElemType e)
{
//判断删除位置是否合法
if (i > L.length || i < 1)
{
printf("删除的位置不合法!\n");
return false;
}
e = L.data[i - 1];
for (int j = i; j < L.length; j++)
{
L.data[j - 1] = L.data[j];
}
L.length--;
return e;
}
//9.遍历整个顺序表,并输出顺序表的元素
Status ListTraverse(SqList L)
{
printf("顺序表的元素为:\n");
for (int i = 0; i < L.length; i++)
{
printf("%d\t", L.data[i]);
}
printf("\n");
return True;
}
//菜单选项
void menu()
{
printf("\t\t\t\t1.初始化 \t\t\t2.清空\n");
printf("\t\t\t\t3.判断表是否为空\t\t4.返回顺序表长度\n");
printf("\t\t\t\t5.第i个位置的值\t\t\t6.查找和e相等元素的位置\n");
printf("\t\t\t\t7.插入 \t\t\t8.删除\n");
printf("\t\t\t\t9.遍历\n");
}
//主函数测试
int main()
{
int choice;
SqList L;
ElemType e = 0;
while (1)
{
menu();
int choice;
printf("请输入菜单选项:");
scanf_s("%d", &choice);
switch (choice)
{
case 1:
{
int i = InitList(L);
if (i == 1)
{
printf("初始化成功!\n");
}
break;
}
case 2:
{
int i = ClearList(L);
if (i == 1)
{
printf("清空成功!\n");
}
break;
}
case 3:
{
int i = isListEmpty(L);
if (i == 1)
{
printf("该表为空!\n");
}
else
{
printf("该表不为空!\n");
}
break;
}
case 4:
{
int len = ListLengt(L);
printf("该顺序表的长度为:%d", len);
break;
}
case 5:
{
int pos;
printf("请输入要查找的位置:");
scanf_s("%d", &pos);
int value = GetElem(L, pos, e);
printf("该位置的元素为:%d", value);
break;
}
case 6:
{
int value;
printf("请输入要匹配的元素:");
scanf_s("%d", &value);
int i = LocateElem(L, value);
printf("该元素在表中得位置为:%d\n", i);
break;
}
case 7:
{
int pos, value;
printf("请输入要插入的位置:");
scanf_s("%d", &pos);
printf("请输入要插入的值:");
scanf_s("%d", &value);
int i = ListInsert(L, pos, value);
if (i == 1)
{
printf("插入成功!\n");
}
break;
}
case 8:
{
int pos,value = 0;
printf("请输入要删除的位置:");
scanf_s("%d", &pos);
int i = ListDel(L, pos, value);
printf("被删除的值为:%d\n", i);
break;
}
case 9:
{
ListTraverse(L);
break;
}
default:
break;
}
}
}
运行结果:
总结:就我个人在写的过程中,其实感觉难点主要在于添加和删除的地方对插入位置和删除位置的判断,这点一定要理解,在添加的时候,因为元素是从第一个开始的,因此添加的下标不能小于1,而考虑最好的情况在最后一个之后插入,添加的下标可以是L.length+1,如果超出这个值,两个元素就会有空格,就不满足顺序表的定义:数据要存储在一片连续的内存里。删除的时候也是差不多,但是不同点在于删除时下标i最大只能是L.length,因为表中就只有这么多数据。