1.线性表概念
线性表是n个具有相同特性的数据元素的有限数列。线性表是一种在实际中广泛使用的数据结构,常见的线性表有:书虚表,链表,栈,队列,字符串等。
线性表在逻辑上是线性结构,也就是说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理存储时,通常以数组和链式结构的形式存储。
2.顺序表概念
顺序表是用一段物理地址连续的存储单元依此存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般分为<1>静态顺序表:使用定长数组存储。
<2>动态顺序表:使用动态开辟的数组存储。
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致其开辟的空间大小固定,开多了浪费,开少了不够用。所以现实中基本使用的都是动态顺序表,根据需要的分配空间大小,所以接下来我将以实现动态顺序表为主展开。
3.动态顺序表的实现
首先,对于动态顺序表的实现,我将设立三个文件(一个头文件,一个函数实现源文件,还有一个测试源文件),这里是要实现的是各种函数接口图:
然后是测试源文件的使用。我将分享我在接口函数实现的各种思路,希望大家能够从中得到启发。
1.顺序表的初始化,(这里以整形数据为使用主体),初始化即设置有效数字为0,容量为4个字节,同时定义动态内存开辟的数组首元素为顺序表的首元素地址。
2.顺序表的打印,即要考虑遍访数组的每个元素,这里我用的是for循环思路实现
然后是顺序表的自我增容,首先其开始条件是当有效数字的个数与容量大小相等时,迭代条件是每次以顺序表原有大小的二倍进行扩增,结束条件是开辟成功,并且让顺序表的地址成功转移到新开辟的空间上。
3.顺序表的尾插,头插,首先其思路是找到该顺序表的结尾/开头,再判断空间是否足够(运用顺序表的自我增容函数判断),再进行数的插入。
然后是顺序表在某个位置的数的插入,首先其思路是找到要插入数的位置(相对于顺序表首元素的位置),然后将该位置后的所有元素向后移动一个单位,再将其数放入该空位置中,首先要判断首先容量是否足够。
4.顺序表在某个位置的数的删除,首先思路是找到要删除数的位置(相对顺序表首元素的位置),然后将该位置之后的所有元素全部向左移动一个单位,将其挤掉。
5.顺序表的尾删,头删,首先思路是找到该顺序表的结尾/开头,前者找到顺序表的倒数第二的位置后直接删除,后者则是将除第一个元素外的其他元素全部左移,从而“挤掉第一个元素”。
6.顺序表的查找,首先思路是用遍访的思路,即依此从左到右依次查找并计数,该数即是顺序表数据的下表。
7.顺序表的摧毁,即释放动态开辟的内存空间,并把容量大小和有效数字设置为0。
8.顺序表具体数的修改。这里并不推荐直接在text.c(测试源文件)中进行对顺序表的元素的修改,而是通过创建修改函数,引用修改函数接口的形式去实现修改目的,这样可以很大程度的避免顺序表的越界访问的错误及时发现。
9.顺序表游戏菜单,同时我们在初始化顺序表后要插入一定数量的数以便我们进行操作,这里需要一个do while语句,用来将各种函数接口衔接好菜单内容,最后别忘了销毁顺序表,实现动态内存空间的释放。以下是全部代码的实现:
//Seqlist.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size; // 存储有效数据个数
int capacity; // 空间大小
}SL;
// 管理数据 -- 增删查改
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
void SLPushBack(SL*ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
void SLModify(SL* ps, int pos, SLDataType x);
int SLFind(SL* ps, SLDataType x);
//seqlist.c
#include"SeqList.h"
void SLInit(SL* ps)
{
ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
if (ps->a == NULL)
{
perror("malloc failed");
exit(-1);
//return;
}
ps->size = 0;
ps->capacity = 4;
}
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
void SLDestroy(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLPushBack(SL* ps, SLDataType x)
{
SLCheckCapacity(ps);
/*ps->a[ps->size] = x;
ps->size++;*/
SLInsert(ps, ps->size, x);
}
void SLPopBack(SL* ps)
{
/*assert(ps->size > 0);
ps->size--;*/
assert(ps);
SLErase(ps, ps->size - 1);
}
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLInsert(ps, 0, x);
/*SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0]=x;
ps->size++;*/
}
void SLPopFront(SL* ps)
{
assert(ps);
/*assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;*/
SLErase(ps, 0);
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
int begin = pos + 1;
while(begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
printf("找到了\n");
return i;
}
}
return -1;
}
//text.c
#include"SeqList.h"
//void TestSeqList2()
//{
// SL sl;
// SLInit(&sl);
// SLPushBack(&sl, 1);
// SLPushBack(&sl, 2);
// SLPushBack(&sl, 3);
// SLPushBack(&sl, 4);
// SLPushBack(&sl, 5);
// SLPrint(&sl);
//
// SLPushFront(&sl, 10);
// SLPushFront(&sl, 20);
// SLPushFront(&sl, 30);
// SLPushFront(&sl, 40);
// SLPrint(&sl);
//
// SLPushBack(&sl, 1);
// SLPushBack(&sl, 2);
// SLPushBack(&sl, 3);
// SLPushBack(&sl, 4);
// SLPushBack(&sl, 5);
// SLPushBack(&sl, 6);
// SLPushBack(&sl, 6);
// SLPushBack(&sl, 0);
// SLPushBack(&sl, 0);
// SLPrint(&sl);
// SLInsert(&sl,1,7);
// SLPrint(&sl);
// SLErase(&sl,10);
// SLPrint(&sl);
// SLDestroy(&sl);
//
//}
void menu()
{
printf("****************************************\n");
printf("1、头插 2、头删\n");
printf("3、尾插 4、尾删\n");
printf("5、修改 6、查找\n");
printf("7、打印 -1、退出\n");
printf("****************************************\n");
}
int main()
{
/*TestSeqList2();*/
SL sl;
SLInit(&sl);
int option = 0;
do
{
menu();
scanf("%d", &option);
if (option == 1)
{
printf("请依次输入你要插入的数据个数和数据\n");
int n = 0;
scanf("%d", &n);
int x = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
SLPushBack(&sl, x);
}
SLPrint(&sl);
}
else if (option == 2)
{
SLPopFront(&sl);
SLPrint(&sl);
}
else if (option == 7)
{
SLPrint(&sl);
}
else if (option == 3)
{
int x = 0;
printf("请输入要插入的值\n");
scanf("%d", &x);
SLPushBack(&sl,x);
SLPrint(&sl);
}
else if (option == 4)
{
SLPopBack(&sl);
SLPrint(&sl);
}
else if (option == 5)
{
int a = 0;
int b = 0;
printf("请依次输入要修改数据的位置和要改成的值\n");
scanf("%d %d", &a, &b);
SLModify(&sl,a-1,b);
SLPrint(&sl);
}
else if (option == 6)
{
int x = 0;
printf("请输入要查找的值\n");
scanf("%d", &x);
SLFind(&sl,x);
SLPrint(&sl);
}
} while (option != -1);
return 0;
}
四.顺序表的心得
写顺序表的过程中,我发现几个特别好的编程习惯,现在跟大家分享:<1>写完一段后立刻测试并修改问题<2>写区域代码时明白其开始条件,迭代顺序,结束条件,这将帮助我们将代码有头有尾的书写。还有是函数的嵌套使用,菜单中有头插,尾插等插入删除功能,但后面当实现了在具体位置实现插入删除的函数后,我们可以在头插函数中直接调用insert具插函数,实现代码的清洁化。