顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。而在顺序表中有定长顺序表和不定长顺序表,不定长顺序表通过malloc申请堆区的空间,在空间使用满了之后还可以在程序
中申请增加空间。
在写顺序表的时候,需要对结构体有一定的了解(这里就不做过多的结构体介绍)
一、创建不定长顺序表
不定长顺序表通过malloc函数实现
#define INIT_SIZE 10
typedef int ElemType;
typedef struct SqList
{
ElemType *data;
int length; // 当前存储数据的个数
int listsize; // 存储空间
}SqList, *SqListPtr;
二、顺序表所实现的功能(不定长)
1.判断顺序表是否为满
static int IsFull_SqDyn(SqListPtr sq) //判满
{
if (sq->data == NULL || sq->length == sq->listsize) return TRUE;
return FALSE;
}
2.顺序表增加空间
static int AppendSpace(SqListPtr sq) //申请空间
{
//1、申请更大的空间
ElemType *data = (ElemType *)malloc(sizeof(ElemType) * sq->listsize * 2);
if (data == NULL) return FALSE;
sq->listsize *= 2;
//2、拷贝原始空间的数据
for (int i = 0; i < sq->length; ++i)
{
data[i] = sq->data[i];
}
//3、释放原始空间
free(sq->data);
//4、将新空间的首地址存储到sq->data上
sq->data = data;
return TRUE;
}
3.顺序表初始化
int InitList_SqDyn(SqListPtr sq) // 初始化顺序表
{
if (sq == NULL) exit(0);
sq->length = 0;
sq->data = (int *)malloc(INIT_SIZE * sizeof(int));
if (sq->data == NULL) return FALSE;
sq->listsize = INIT_SIZE;
return TRUE;
}
4.插入
(1)位置插
int InsertList_SqDyn_Pos(SqListPtr sq, ElemType val, int pos) //位置插
{
if (sq == NULL) exit(0);
if (pos < 0 || pos > sq->length) return FALSE;
if (IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
for (int i = sq->length; i > pos; --i)
{
sq->data[i] = sq->data[i - 1];
}
sq->data[pos] = val;
sq->length++;
return TRUE;
}
(2)头插
int InsertList_SqDyn_Head(SqListPtr sq, ElemType val) //头插
{
if(sq == NULL) exit(0);
if(IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
int i = sq->length;
for(;i>0;i--)
{
sq->data[i] = sq->data[i-1];
}
sq->data[i] = val;
sq->length++;
return TRUE;
}
(3)尾插
int InsertList_SqDyn_Tail(SqListPtr sq, ElemType val) //尾插
{
if(sq == NULL) exit(0);
if(IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
sq->data[sq->length] = val;
sq->length++;
return TRUE;
}
5.删除
(1)位置删
int DeleteList_SqDyn_Pos(SqListPtr sq, int pos) //位置删
{
if (sq == NULL || sq->data == NULL) return FALSE;
if (pos < 0 || pos >= sq->length) return FALSE;
while (pos < sq->length - 1)
{
sq->data[pos] = sq->data[pos + 1];
pos++;
}
sq->length--;
return TRUE;
}
(2)头删
int DeleteList_SqDyn_Head(SqListPtr sqlist) //头删
{
if(sqlist == NULL) exit(0);
int i = 0;
for(;i<sqlist->length-1;i++)
{
sqlist->data[i] = sqlist->data[i+1];
}
sqlist->length--;
return TRUE;
}
(3)尾删
int DeleteList_SqDyn_Tail(SqListPtr sqlist) //尾删
{
if(sqlist == NULL) exit(0);
sqlist->length--;
return TRUE;
}
6.打印
void ShowList_SqDyn(SqListPtr sq) //打印
{
if (sq == NULL || sq->data == NULL) exit(0);
for (int i = 0; i < sq->length; ++i)
{
printf("%d ", sq->data[i]);
}
printf("\n");
}
7.清空顺序表
void ClearList_SqDyn(SqListPtr sqlist) //清空
{
if (sqlist == NULL) return;
sqlist->length = 0;
}
8.销毁顺序表(因为不定长顺序表用malloc函数申请空间,所以我们用free函数来释放所申请的空间)
void DestroyList_SqDyn(SqListPtr sqlist) //销毁
{
if (sqlist == NULL) return;
free(sqlist->data);
sqlist->data = NULL; // 防止野指针
sqlist->length = sqlist->listsize = 0;
}
#include
#include
#define FALSE 0
#define TRUE 1
#define INIT_SIZE 10
typedef int ElemType;
typedef struct SqList
{
ElemType *data;
int length; // 当前存储数据的个数
int listsize; // 存储空间
}SqList, *SqListPtr;
static int IsFull_SqDyn(SqListPtr sq) //判满
{
if (sq->data == NULL || sq->length == sq->listsize) return TRUE;
return FALSE;
}
static int AppendSpace(SqListPtr sq) //申请空间
{
//1、申请更大的空间
ElemType *data = (ElemType *)malloc(sizeof(ElemType) * sq->listsize * 2);
if (data == NULL) return FALSE;
sq->listsize *= 2;
//2、拷贝原始空间的数据
for (int i = 0; i < sq->length; ++i)
{
data[i] = sq->data[i];
}
//3、释放原始空间
free(sq->data);
//4、将新空间的首地址存储到sq->data上
sq->data = data;
return TRUE;
}
int InitList_SqDyn(SqListPtr sq) // 初始化顺序表
{
if (sq == NULL) exit(0);
sq->length = 0;
sq->data = (int *)malloc(INIT_SIZE * sizeof(int));
if (sq->data == NULL) return FALSE;
sq->listsize = INIT_SIZE;
return TRUE;
}
int InsertList_SqDyn_Pos(SqListPtr sq, ElemType val, int pos) //位置插
{
if (sq == NULL) exit(0);
if (pos < 0 || pos > sq->length) return FALSE;
if (IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
for (int i = sq->length; i > pos; --i)
{
sq->data[i] = sq->data[i - 1];
}
sq->data[pos] = val;
sq->length++;
return TRUE;
}
int InsertList_SqDyn_Head(SqListPtr sq, ElemType val) //头插
{
if(sq == NULL) exit(0);
if(IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
int i = sq->length;
for(;i>0;i--)
{
sq->data[i] = sq->data[i-1];
}
sq->data[i] = val;
sq->length++;
return TRUE;
}
int InsertList_SqDyn_Tail(SqListPtr sq, ElemType val) //尾插
{
if(sq == NULL) exit(0);
if(IsFull_SqDyn(sq) && !AppendSpace(sq))
{
return FALSE;
}
sq->data[sq->length] = val;
sq->length++;
return TRUE;
}
int DeleteList_SqDyn_Pos(SqListPtr sq, int pos) //位置删
{
if (sq == NULL || sq->data == NULL) return FALSE;
if (pos < 0 || pos >= sq->length) return FALSE;
while (pos < sq->length - 1)
{
sq->data[pos] = sq->data[pos + 1];
pos++;
}
sq->length--;
return TRUE;
}
int DeleteList_SqDyn_Head(SqListPtr sqlist) //头删
{
if(sqlist == NULL) exit(0);
int i = 0;
for(;i<sqlist->length-1;i++)
{
sqlist->data[i] = sqlist->data[i+1];
}
sqlist->length--;
return TRUE;
}
int DeleteList_SqDyn_Tail(SqListPtr sqlist) //尾删
{
if(sqlist == NULL) exit(0);
sqlist->length--;
return TRUE;
}
void ClearList_SqDyn(SqListPtr sqlist) //清空
{
if (sqlist == NULL) return;
sqlist->length = 0;
}
void DestroyList_SqDyn(SqListPtr sqlist) //销毁
{
if (sqlist == NULL) return;
free(sqlist->data);
sqlist->data = NULL; // 防止野指针
sqlist->length = sqlist->listsize = 0;
}
void ShowList_SqDyn(SqListPtr sq) //打印
{
if (sq == NULL || sq->data == NULL) exit(0);
for (int i = 0; i < sq->length; ++i)
{
printf("%d ", sq->data[i]);
}
printf("\n");
}
int main()
{
//以下为代码测试段
SqList sq;
if (!InitList_SqDyn(&sq))
{
printf("init error\n");
return 0;
}
for (int i = 0; i < 10; ++i)
{
InsertList_SqDyn_Pos(&sq, i, sq.length);
}
ShowList_SqDyn(&sq);
InsertList_SqDyn_Head(&sq, 200);
InsertList_SqDyn_Tail(&sq, 100);
ShowList_SqDyn(&sq);
DeleteList_SqDyn_Head(&sq);
DeleteList_SqDyn_Tail(&sq);
ShowList_SqDyn(&sq);
return 0;
}