【数据结构——顺序表】

编程入门 行业动态 更新时间:2024-10-26 06:32:38

【<a href=https://www.elefans.com/category/jswz/34/1769880.html style=数据结构——顺序表】"/>

【数据结构——顺序表】

文章目录

  • 1. 线性表
    • 2.1 线性表的定义
    • 2.2 线性表的抽象数据类型
    • 3.1 顺序表
      • 1. 顺序表的结构代码
      • 2. 初始化
      • 3. 输出顺序表元素
      • 4. 插入
      • 5. 删除
      • 6.定位
      • 7. 查找
      • 8. 清空列表
      • 9. 运行截图
    • 完整代码

1. 线性表

2.1 线性表的定义

线性表(List):零个或多个数据元素的有限序列。

2.2 线性表的抽象数据类型

ADT 线性表(List)
Data线性表的数据对象集合为{a1,a2,……,an},每个元素的类型均为DataType。其中,除第一个元素a1外,每一个元素有且只有一个直接前驱元素,除了最后一个元素an外,每一个元素有且只有一个直接后继元素。数据元素之间的关系是一对一的关系。
OperationInitList(*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相同的数据元素eLa_len = ListLength(*La);	//求线性表的长度Lb_len = ListLength(Lb);for(i = 1; i <= Lb_len; ++i){GetElem(Lb,i,&e);		//取Lb中第i个数据元素赋值给eif(!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", &paraListPtr->length);printf("The address of data: %ld\r\n", &paraListPtr->data);printf("The address of actual data: %ld\r\n", &paraListPtr->data[0]);printf("The address of second data: %ld\r\n", &paraListPtr->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();
}

恩师相关文章链接

更多推荐

【数据结构——顺序表】

本文发布于:2024-02-05 08:58:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1673958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据结构   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!