线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。
常见的线性表:顺序表、链表、栈、队列、字符串…
顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存 储。在数组上完成数据的增删查改
顺序表一般可以分为:
- 静态顺序表:使用定长数组存储。
- 动态顺序表:使用动态开辟的数组存储
顺序表数据必须是从头开始,依次储存
静态顺序表
//给少了不够用,给多了空间浪费
#define MAX_SIZE 10
typedef int SQDataType;
struct SeqList
{
SQDataType a[MAX_SIZE]; //类型与数组大小均能改变
int size;
};
typedef struct SeqList SL;
//增删查改等接口函数
//void SeqListInit(struct SeqList s1);
void SeqListInit(SL* ps);
#include "SeqList.h"
void SeqListInit(SL* ps)
{
memset(ps->a,0, sizeof(SQDataType) * MAX_SIZE);
ps->size = 0;
}
//尾插
void SeqListPushBack(SL* ps, SQDataType x)
{
if (ps->size >= MAX_SIZE)
{
printf("SeqList is full\n");
return;
}
ps->a[ps->size] = x;
ps->size++;
}
动态版本
typedef int SQDataType;
struct SeqList
{
SQDataType* a; //类型与数组大小均能改变
int size; //有效数据个数
int capacity;//容量
};
typedef struct SeqList SL;
//增删查改等接口函数
//void SeqListInit(struct SeqList s1);
void SeqListInit(SL* ps);
void SeqListPrint(SL* ps);
//尾插
void SeqListPushBack(SL* ps,SQDataType x);
//头插
void SeqListPushFront(SL* ps,SQDataType x);
//尾删
void SeqListPopBack(SL* ps );
//头删
void SeqListPopFront(SL* ps);
void SeqListInsert(SL* ps, int pos, SQDataType x);//任意位置删除
void SeqListErase(SL* ps, int pos);
int SeqListFind(SL* ps, SQDataType x);
void SeqListModify(SL* ps, int pos, SQDataType x);
void SeqListDestroy(SL* ps);
void SeqListInit(SL* ps)
{
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity) //满了就扩容
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SQDataType* tmp = realloc(ps->a, newcapacity * sizeof(SQDataType));
if (tmp == NULL)
{
printf("realloc failed\n");
exit(-1); //结束程序
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
}
void SeqListPushBack(SL* ps, SQDataType x) //尾插
{
SeqListCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SL* ps, SQDataType x)//头插
{
//1.初始条件
//2.结束条件
//3.迭代过程
SeqListCheckCapacity(ps);
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
void SeqListPopBack(SL* ps)//尾删
{
assert(ps->size > 0);
ps->size--;
}
void SeqListPopFront(SL* ps)//头删
{
assert(ps->size > 0);
int start = 1;
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
start++;
}
ps->size--;
}
void SeqListPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListInsert(SL* ps, int pos, SQDataType x)//任意位置插入
{
assert(pos < ps->size);
SeqListCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SL* ps, int pos)//任意位置删除
{
assert(pos < ps->size);
int start = pos;
while (ps->size > start)
{
ps->a[start-1] = ps->a[start];
start++;
}
ps->size--;
}
void SeqListDestroy(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
int SeqListFind(SL* ps, SQDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListModify(SL* ps, int pos, SQDataType x)
{
assert(pos < ps->size);
ps->a[pos] = x;
}
#include "SeqList.h"
void TestSeqList1()
{
SL s1 = { {0},0 };
SeqListInit(&s1);
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
//SeqListErase(&s1, 1);
SeqListInsert(&s1, 0, 88);
SeqListPrint(&s1);
SeqListDestroy(&s1);
}
int main()
{
TestSeqList1();
return 0;
}