#include <iostream>
#include <stdio.h>//输入输出头文件
#include <stdlib.h>//用于申请动态内存分布的头文件
#include <string>//字符串头文件
#define LIST_INIT_SIZE 100//线性表存储空间的初始分配量
#define LISTINITCREMENT 10//线性表存储空间的分配量
#define LIST_INCREAMENT 10//扩展线性表存储空间的大小
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
using namespace std;
/*
1、1.创建一个线性表LA顺序结构,并构造该顺序表,其元素与一维数组a[] = { 16, 6, 2, 9, 11, 8, 1 }元素相同。
2、2.打印“题1”所创建的顺序表LA。
3、3.在顺序线性表LA中查找第1个与任意给定值e相等的元素位置,找到返回其在L中的位序,否则返回0。
4、4.在顺序线性表LA中查找第i个元素,并将第i个元素存储在变量e中。
5、5.在顺序线性表LA中第i(1 <= i <= 8)个位置之前插入新的元素e(e的值为任意整数)。
6、6.删除顺序线性表LA中第i(1 <= i <= 7)个元素, 并用e返回其值。
7、7.求顺序线性表LA的表长
8、8.判断顺序线性表LA是否为空
9、9.清空顺序线性表LA
10 10.销毁顺序线性表LA
*/
typedef struct//1、创建一个线性表的结构
{
ElemType *elem;//存储空间基址
int length;//当前长度
int listsize;//当前分配的存储空间(以sizeof(ElemType)为单位)
}Sqlist;
Status InitList_Sq(Sqlist &LA)//1、初始化顺序表
{
LA.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));//建立一个空的线性表LA
if (!LA.elem)//存储分配失败
return ERROR;
LA.length = 0;//空表长度
LA.listsize = LIST_INIT_SIZE;//初始存储容量
return OK;
}
//1、构造内容为{16,6,2,9,11,8,1}的顺序表
Status CreatSqlist(Sqlist &LA, ElemType a[], int Length)
{
int i;
for (i = 0; i < Length; i++)
{
LA.elem[i]=a[i];
LA.length++;
}
return OK;
}
//2、显示顺序表的内容
void DisSqlist(Sqlist LA)
{
int i;
for (i = 0; i < LA.length; i++)
{
printf("%d ", LA.elem[i]);
}
printf("\n");
}
//查找元素在线性表中第1个元素的位置,并用e返回
Status LocateSqlist02(Sqlist LA,ElemType &e)
{
int a;
for (a = 1; a < LA.length; a++)
{
if (LA.elem[a] == LA.elem[0])
{
e = a;
printf("与第1个元素的相同是顺序表下标为%d\n", e);
}
}
return 0;
}
//查找元素在线性表中a的位置,并用e返回
Status LocateSqlist(Sqlist LA, int a, ElemType &e)
{
if (a < 1 || a >= LA.length)
{
printf("位置不合理\n");
return ERROR;
}
e = LA.elem[a - 1];
return OK;
}
//查找顺序表中值为e的元素的下标
Status LocateSqlist01(Sqlist LA, ElemType e)
{
int i;//i是下标
for (i = 0; i < LA.length; i++)
{
if (LA.elem[i] == e)
return i + 1;//返回的是位置
}
return -1;
}
//在第i个位置之前插入新的元素e
Status InsetSqlist(Sqlist &LA, int n, ElemType e)
{
//判断插入位置是否合理
if (n<1 || n>LA.length+1)
return ERROR;
//判断是否有空间插入数据
if (LA.length >= LA.listsize)
{
realloc(LA.elem, (sizeof(ElemType)*(LA.length + LIST_INCREAMENT)));
if (!LA.elem)
return ERROR;
LA.listsize += LIST_INCREAMENT;
}
//移动元素位置
for (int j = LA.length - 1; j >= n - 1; j--)
{
LA.elem[j + 1] = LA.elem[j];
}
//在第i个位置插入数据
LA.elem[n - 1] = e;
LA.length++;
}
//删除线性表中第i个元素
Status DeleteSqlist(Sqlist &LA, int i, ElemType &e)//i是值我们要删除元素的下标值,e是返回的元素
{
ElemType *p= LA.elem;
ElemType *q = LA.elem + LA.length - 1;
if ((i<1) || (i>LA.length))//如果i不合法
{
printf("无法在表中寻找到该值!\n");
return ERROR;
}
p = &(LA.elem[i - 1]);//p为被删除元素的位置
e = *p;//表尾元素的位置
for (++p; p <= q; ++p)
*(p - 1) = *p;//被删除元素之后的元素左移
--LA.length;
return OK;
}
//判断表长是否为空
Status LenSqlist01(Sqlist LA)
{
if (!LA.length)
printf("当前顺序表为空!\n");
printf("当前顺序表为不为空!\n");
return OK;
}
//判断表长
Status LenSqlist(Sqlist LA)
{
if (LA.elem)
return LA.length;
}
//清空顺序表(有修改顺序表顺序)
Status ClearSqlist(Sqlist &LA)
{
if (!LA.elem)
return ERROR;
LA.length = 0;
return 0;
}
//销毁顺序表
Status DestorySqlist(Sqlist &LA)
{
if (!LA.elem)
{
return ERROR;
}
free(LA.elem);
return OK;
}
int main()
{
ElemType e;
ElemType a[] = { 16, 6, 2, 9, 11, 8, 1 };
Sqlist LA;
InitList_Sq(LA);//建立一个空的顺序表
CreatSqlist(LA, a, sizeof(a)/ sizeof(a[0]));//给顺序表赋值
DisSqlist(LA);//显示顺序表
printf("查找元素在线性表中第1个元素的位置:\n");
LocateSqlist02(LA, e); //查找元素在线性表中第1个元素的位置,并用e返回
LocateSqlist(LA, 3, e);//查找元素在线性表中i的位置,并用e返回
printf("查找顺序表中第3个元素是:\n");
printf("%d\n", e);//输出e的值
printf("查找顺序表中第9个元素是:\n");
LocateSqlist(LA, 9, e);//查找元素在线性表中i的位置,并用e返回
printf("查找顺序表中元素为11的下标是:\n");
printf("%d\n",LocateSqlist01(LA,11));//查找顺序表中值为e的元素的下标
//DelSqlist(LA, 11);//
printf("在第6个数字之前插入新的元素e\n");
InsetSqlist(LA, 5, e);//在第i个元素插入e的值
DisSqlist(LA);
DeleteSqlist(LA, 4,e);//删除元素
printf("删除第4个元素后顺序表为:\n");
DisSqlist(LA);
printf("该表长为:%d\n", LenSqlist(LA));//判断表长
printf("清空后表长为:\n");
ClearSqlist(LA);//清空顺序表
DisSqlist(LA);//清空后再显示顺序表的内容为:
printf("当前顺序表表长是否为空:\n");
LenSqlist01(LA);//判断表长是否为空
DestorySqlist(LA);//销毁顺序表
system("pause");
return 0;
}
//删除顺序表中的某个元素并用e返回其值
//是自己照着书写的错误示范
//Status DelSqlist(Sqlist &LA, ElemType e)
//{
// int i;
// for (i = 0; i < LA.length-1; i++)
// {
// if (LA.elem[i] == e)
// for (; i< LA.length-1;i++)
// LA.elem[i] = LA.elem[i + 1];
// }
// return OK;
//}
//#define Status int
//#define ElemType int
//为什么这样不可以的原因是:宏定义只能给常量起别名
//typdefine是给数据类型起别名
顺序表涉及内容:
/*
1、1.创建一个线性表LA顺序结构,并构造该顺序表,其元素与一维数组a[] = { 16, 6, 2, 9, 11, 8, 1 }元素相同。
2、2.打印“题1”所创建的顺序表LA。
3、3.在顺序线性表LA中查找第1个与任意给定值e相等的元素位置,找到返回其在L中的位序,否则返回0。
4、4.在顺序线性表LA中查找第i个元素,并将第i个元素存储在变量e中。
5、5.在顺序线性表LA中第i(1 <= i <= 8)个位置之前插入新的元素e(e的值为任意整数)。
6、6.删除顺序线性表LA中第i(1 <= i <= 7)个元素, 并用e返回其值。
7、7.求顺序线性表LA的表长
8、8.判断顺序线性表LA是否为空
9、9.清空顺序线性表LA
10 10.销毁顺序线性表LA
11、11在第i个位置之前插入新的元素e
*/