文章目录
1. 线性表
2.1 线性表的定义
线性表(List):零个或多个数据元素的有限序列。
2.2 线性表的抽象数据类型
ADT 线性表(List)
Data
线性表的数据对象集合为{a1,a2,……,an},每个元素的类型均为DataType。其中,除第一个元素a1外,每一个元素有且只有一个直接前驱元素,除了最后一个元素an外,每一个元素有且只有一个直接后继元素。数据元素之间的关系是一对一的关系。
Operation
InitList(*L):初始化操作,建立一个空的线性表L。
ListEmpty(L):若线性表为空,返回true,否则返回false。
ClearList(*L):将线性表清空。
GetElem(L,i,*e):将线性表L中的第i个位置元素值返回给e。
LocateElem(L,e):在线性表L中查找与给定值e相等的元素,如果查找成功,返回该元素在表中序号表示成功;否则,返回0表示失败。
ListInsert(*L,i,e):在线性表L中的第i个位置插入新元素e。
ListDelete(*L,i,*e):删除线性表L中第i个位置元素,并用e返回其值。
ListLength(L):返回线性表L的元素个数。
endADT
/*将所有的在线性表Lb中但不在La中的数据元素插入到La中*/
void unionL(SqList*La,Sqlist Lb)
{
int La_len,Lb_len,i;
ElemType e; //声明与La和Lb相同的数据元素e
La_len = ListLength(*La); //求线性表的长度
Lb_len = ListLength(Lb);
for(i = 1; i <= Lb_len; ++i)
{
GetElem(Lb,i,&e); //取Lb中第i个数据元素赋值给e
if(!LocateElem(*La,e)) //La中不存在和e相同数据元素
ListInsert(La,++La_len,e); //插入
}
}
3.1 顺序表
顺序表:采用顺序存储结构的线性表简称为顺序表,顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
顺序表的特点:只要确定了起始位置,表中任一元素的地址都通过下列公式得到:LOC(ai)=LOC(a1)+(i-1)*L 1≤i≤n 其中,L是元素占用存储单元的长度。
线性表(a1, a2, …… ,an)的顺序存储示意图如下。
帆哥说:“No code no bb!”,所以直接show you my code!
1. 顺序表的结构代码
#define MAX_LENGTH 20 //存储空间初始分配量
typedef int ElemType; //ElemType类型根据实际情况而定,此处为int
typedef struct
{
int length; //线性表当前长度
ElemType data[MAX_LENGTH]; //数组,存储数据元素d.
}SqList,*SqListPtr;
顺序存储结构需要三个属性:
- 存储空间的起始位置:数组data,它的存储位置就是存储空间的存储位置
- 线性表的最大存储容量:数组长度MAX_LENGTH
- 线性表当前长度:length
2. 初始化
SqListPtr sequentialListInit(int paraData[], int paraLength)
{
SqListPtr resultPtr = (SqListPtr)malloc(sizeof(SqList));
for (int i = 0; i < paraLength; ++i)
{
resultPtr->data[i] = paraData[i];
}
resultPtr->length = paraLength;
return resultPtr;
}
3. 输出顺序表元素
void outputList(SqListPtr paraList) {
for(int i = 0; i < paraList->length; ++i)
{
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
4. 插入
void sequentialListInsert(SqListPtr paraListPtr, int paraPosition, int paraValue)
{
if (paraListPtr->length >= MAX_LENGTH)
{
printf("Cannot insert element: list full.\n");
return;
}
if (paraPosition < 0)
{
printf("Cannot insert element: negative position unsupported.");
return;
}
if (paraPosition > paraListPtr->length)
{
printf("Cannot insert element: the position %d is bigger than the list length %d.\n", paraPosition, paraListPtr->length);
return;
}
for (int i = paraListPtr->length; i > paraPosition; --i)
{
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
paraListPtr->data[paraPosition] = paraValue;
paraListPtr->length ++;
}
5. 删除
int sequentialListDelete(SqListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("Invalid position: %d.\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->length)
{
printf("Cannot delete element: the position %d is beyond the list length %d.\n", paraPosition, paraListPtr->length);
return -1;
}
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->length; ++i)
{
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
paraListPtr->length--;
return resultValue;
}
6.定位
int locateElement(SqListPtr paraListPtr, int paraValue)
{
for (int i = 0; i < paraListPtr->length; ++i)
{
if (paraListPtr->data[i] == paraValue)
{
return i;
}
}
return -1;
}
7. 查找
int getElement(SqListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("Invalid position: %d.\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->length)
{
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->length);
return -1;
}
return paraListPtr->data[paraPosition];
}
8. 清空列表
void clearList(SqListPtr paraListPtr)
{
paraListPtr->length = 0;
}
9. 运行截图
完整代码
#include <stdio.h>
#include <malloc.h>
#define MAX_LENGTH 20 //存储空间初始分配量
typedef int ElemType; //ElemType类型根据实际情况而定,此处为int
typedef struct
{
int length; //线性表当前长度
ElemType data[MAX_LENGTH]; //数组,存储数据元素d.
}SqList,*SqListPtr;
/*
* 输出顺序表.
*/
void outputList(SqListPtr paraList) {
for(int i = 0; i < paraList->length; ++i)
{
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
/*
* 输出顺序表的内存地址.
*/
void outputMemory(SqListPtr paraListPtr)
{
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of length: %ld\r\n", ¶ListPtr->length);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}
//初始化顺序表
SqListPtr sequentialListInit(int paraData[], int paraLength)
{
SqListPtr resultPtr = (SqListPtr)malloc(sizeof(SqList));
for (int i = 0; i < paraLength; ++i)
{
resultPtr->data[i] = paraData[i];
}
resultPtr->length = paraLength;
return resultPtr;
}
//插入
void sequentialListInsert(SqListPtr paraListPtr, int paraPosition, int paraValue)
{
if (paraListPtr->length >= MAX_LENGTH)
{
printf("Cannot insert element: list full.\n");
return;
}
if (paraPosition < 0)
{
printf("Cannot insert element: negative position unsupported.");
return;
}
if (paraPosition > paraListPtr->length)
{
printf("Cannot insert element: the position %d is bigger than the list length %d.\n", paraPosition, paraListPtr->length);
return;
}
for (int i = paraListPtr->length; i > paraPosition; --i)
{
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
paraListPtr->data[paraPosition] = paraValue;
paraListPtr->length ++;
}
/*
* 测试.
*/
void sequentialInsertTest()
{
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialInsertTest begins. ----\r\n");
// 初始化.
SqListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 插入到第一个.
printf("Now insert to the first, the list is: ");
sequentialListInsert(tempList, 0, 8);
outputList(tempList);
// 插入到最后一个.
printf("Now insert to the last, the list is: ");
sequentialListInsert(tempList, 6, 9);
outputList(tempList);
// 在尾节点前插入.
printf("Now insert beyond the tail. \r\n");
sequentialListInsert(tempList, 8, 9);
printf("The list is:");
outputList(tempList);
//插入位置
for (i = 0; i < 5; ++i)
{
printf("Inserting %d.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- sequentialInsertTest ends. ----\n");
}
//删除顺序表节点
int sequentialListDelete(SqListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("Invalid position: %d.\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->length)
{
printf("Cannot delete element: the position %d is beyond the list length %d.\n", paraPosition, paraListPtr->length);
return -1;
}
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->length; ++i)
{
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
paraListPtr->length--;
return resultValue;
}
//测试
void sequentialDeleteTest()
{
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialDeleteTest begins. ----\n");
// 初始化.
SqListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 删除第一个节点.
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
// 删除最后一个节点.
printf("Now delete the last, the list is: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
// 删除第二个节点 .
printf("Now delete the second, the list is: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
// 删除第五个节点.
printf("Now delete the 5th, the list is: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
// 删除第-6个节点(测试非法情况).
printf("Now delete the (-6)th, the list is: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- sequentialDeleteTest ends. ----\n");
outputMemory(tempList);
}
//定位
int locateElement(SqListPtr paraListPtr, int paraValue)
{
for (int i = 0; i < paraListPtr->length; ++i)
{
if (paraListPtr->data[i] == paraValue)
{
return i;
}
}
return -1;
}
//查找
int getElement(SqListPtr paraListPtr, int paraPosition)
{
if (paraPosition < 0)
{
printf("Invalid position: %d.\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->length)
{
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->length);
return -1;
}
return paraListPtr->data[paraPosition];
}
//清除顺序表
void clearList(SqListPtr paraListPtr)
{
paraListPtr->length = 0;
}
int main()
{
sequentialInsertTest();
sequentialDeleteTest();
}