这一系列博客仅为自己学习记录只用,不具有太多参考价值,只为了在日后回头观望时,温故而知新,继续前行。
顺序表–“顺序存储结构的线性表”,用数组的形式保存,其特点有以下几点:
1.在计算机中,用一组地址连续的存储单元存储数据元素
2.由于内存分配是连续的,只需要知道表头,与元素相对于表头的距离,即可定位相应元素:
p为元素在线性表中的逻辑序号,则p-1为元素在线性表中的物理序号。
即:elem[p-1],为线性表中相对表头距离为p的元素。
3.线性表内存分配方式是,在初始化时即分配预定大小的内存;为实现线性表动态分配内存,当之前所分配的内存不足时,即需要重新分配足够大的内存,并且要保证已存在数据的安全。
线性表的基本定义与操作:
#ifndef SEQLIST_H_INCLUDE
#define SEQLIST_H_INCLUDE
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define SEQ_INIT_SIZE 100
#define SEQ_INCREMENT 10
typedef int ElemType;
//顺序表的结构体,包括:
//顺序表元素类型的指针,指向顺序表存储空间的首地址
//顺序表长度
//当前分配给顺序表的存储空间
typedef struct
{
ElemType *elem;
int length;
int seqSize;
}SeqList;
//顺序表,相关运算函数
bool initSeqList(SeqList *L); //initSeqList,初始化
bool newsizeSeqList(SeqList *L); //newsizeSeqList,更新存储空间
bool isemptySeqList(const SeqList *L); //isemptySeqList,是否为空
int lenSeqList(const SeqList *L); //lenSeqList,顺序表长度
bool appendSeqList(SeqList *L, const ElemType e); //appendSeqList,添加元素
bool insertSeqList(SeqList *L, int p, const ElemType e);//insertSeqList,插入元素
bool deleteSeqList(SeqList *L, int p); //deleteSeqList,删除元素
bool elemSeqList(const SeqList *L, int p); //elemSeqList,取元素
bool locatSeqList(const SeqList *L, const ElemType e); //locateSeqList,按值查找
void outputSeqList(const SeqList *L); //outputSeqList,遍历输出
#endif //SEQLIST_H_INCLUDE
#include "seqList.h"
//初始化顺序表
bool initSeqList(SeqList *L)
{
L->elem = (ElemType *)malloc(SEQ_INIT_SIZE * sizeof(ElemType));
if (L->elem == NULL)
return false;
L->length = 0;
L->seqSize = SEQ_INIT_SIZE;
return true;
}
//更新存储空间
bool newsizeSeqList(SeqList *L)
{
ElemType *newL = NULL;
newL = (ElemType *)realloc(L->elem, L->seqSize + SEQ_INCREMENT); //如果空间不够,重新分配空间
if (!newL)
return false;
L->elem = newL; //表指针指向新分配的地址
L->seqSize = L->seqSize + SEQ_INCREMENT; //顺序表存储空间加SEQ_INCREMENT 10
return true;
}
//判断是否为空
bool isemptySeqList(const SeqList *L)
{
if (!L->length)
return true;
return false;
}
//获取顺序表长度
int lenSeqList(const SeqList *L)
{
return L->length;
}
//表尾添加元素
bool appendSeqList(SeqList *L, const ElemType e)
{
int p = L->length; //添加元素在表中的位置
if (L->length < L->seqSize)
L->elem[p] = e;
else{
if (!newsizeSeqList(L))
return false;
L->elem[p] = e;
}
L->length = L->length + 1; //表长加1
return true;
}
//插入元素
bool insertSeqList(SeqList *L, int p, const ElemType e)
{
if (p < 1 || p > L->length + 1)
return false;
p = p - 1; //将逻辑序号转化为物理序号
if (L->length < L->seqSize){ //判断空间是否足够
for (int i = L->length; i > p; i--) //p位置及其之后的所有元素位置后移1位
L->elem[i] = L->elem[i - 1];
L->elem[p] = e;
}
else{
if (!newsizeSeqList(L))
return false;
for (int i = L->length; i > p; i--)
L->elem[i] = L->elem[i - 1];
L->elem[p] = e;
}
L->length++;
return true;
}
//删除元素
bool deleteSeqList(SeqList *L, int p)
{
if (p < 1 || p > L->length || isemptySeqList(L))
return false;
p = p - 1;
while (p < L->length){
L->elem[p] = L->elem[p + 1];
p++;
}
L->length--;
return true;
}
//取元素
bool elemSeqList(const SeqList *L, int p)
{
if (p < 1 || p > L->length || isemptySeqList(L))
return false;
p = p - 1;
printf("所取得元素为%d\n", L->elem[p]);
return true;
}
//按值查找
bool locatSeqList(const SeqList *L, const ElemType e)
{
int p = 0;
while (p < L->length && L->elem[p] != e)
p++;
if (p >= L->length)
return false;
printf("所查找元素在第%d个\n", p + 1);
return true;
}
//遍历输出
void outputSeqList(const SeqList *L)
{
if (isemptySeqList(L)){
printf("顺序表为空!\n");
//rewind(stdin);
//getchar();
}
int len = L->length;
printf("顺序表元素是:\t");
for (int i = 0; i < len; i++)
printf("%d ", L->elem[i]);
printf("\n");
//rewind(stdin);
//getchar();
}