1、基本概念
2、设计与实现
插入元素算法
判断线性表是否合法
判断插入位置是否合法
把最后一个元素到插入位置的元素后移一个位置
将新元素插入
线性表长度加1
获取元素操作
判断线性表是否合法
判断位置是否合法
直接通过数组下标的方式获取元素
删除元素算法
判断线性表是否合法
判断删除位置是否合法
将元素取出
将删除位置后的元素分别向前移动一个位置
线性表长度减1
3、优点和缺点
优点:
无需为线性表中的逻辑关系增加额外的空间
可以快速的获取表中合法位置的元素
缺点:
插入和删除操作需要移动大量元素
当线性表长度变化较大时难以确定存储空间的容量
程序演示
头文件.h
#ifndef _zeng_
#define _zeng_
typedef void SeqList;
typedef void SeqListNode;
SeqList *SeqList_Create(int capacity);//根据capacity创建线性表大小
void SeqList_Destroy(SeqList *list);//销毁线性表
void SeqList_Clear(SeqList*list);//线性表清空
int SeqList_Length(SeqList *list);//获取线性表长度
int SeqList_Capacity(SeqList *list);//获取线性表容量
int SeqList_Insert(SeqList*list, SeqListNode *node, int pos);//在指定位置插入结点地址
SeqListNode *SeqList_Get(SeqList*list, int pos);//获得结点的地址
SeqListNode *SeqList_Delete(SeqList*list, int pos);//删除一个结点地址,并返回该结点地址
#endif
seqlist.c
#include<stdlib.h>
#include<stdio.h>
#include"seqlist.h"
#include<string.h>
typedef struct _tag_SeqList
{
int capacity;//容量
int length;//记录现在存储的东西
unsigned int *node;//创建一级指针,动态分配内存大小
}TSeqList;
//一次打造2个内存空间
SeqList *SeqList_Create(int capacity)
{
TSeqList *ret = NULL;
if (capacity < 0)
{
printf("容量大小<0\n");
return;
}
ret = (TSeqList *)malloc(sizeof(TSeqList)+sizeof(unsigned int)*capacity);//一次分配
if (ret == NULL)
{
printf("ret分配失败\n");
return NULL;
}
memset(ret, 0, sizeof(TSeqList)+sizeof(unsigned int)*capacity);
ret->node = (unsigned int *)(ret + 1);//ret+1跳sizeof(TSeqList)大小,ret->node指向数组
ret->capacity = capacity;
ret->length = 0;
return ret;
}
void SeqList_Destroy(SeqList *list)
{
if (list == NULL)
{
return;
}
return free(list);//一次就释放,很爽
}
//链表清零
void SeqList_Clear(SeqList*list)
{
TSeqList *tlist = NULL;
if (list == NULL)
{
return;
}
tlist->length = 0;
}
int SeqList_Length(SeqList *list)
{
TSeqList *tList = NULL;
if (list == NULL)
{
return;
}
tList = (TSeqList *)list;
return tList->length;
}
//线性表的容量和线性表的长度是不一样的
int SeqList_Capacity(SeqList *list)
{
TSeqList *tList = NULL;
tList = (TSeqList *)list;
if (list == NULL)
{
return;
}
return tList->capacity;
}
int SeqList_Insert(SeqList*list, SeqListNode *node, int pos)
{
TSeqList *tList = NULL;
tList = (TSeqList *)list;
if (tList->length >= tList->capacity)//判断是否满了
{
printf("容量已经满了\n");
return -1;
}
if (list == NULL||node==NULL)
{
printf("传入错误\n");
}
if (pos<0 || pos > tList->length)
{
printf("pos位置错误\n");
return;
}
if (pos >= tList->length)
{
printf("pos位置大于总长,友好容错\n");
pos = tList->length;
}
//从pos位置处开始,把数组后面的元素依次后移
for (int i = tList->length; i > pos; i--)//元素后移
{
tList->node[i] = tList->node[i - 1];
}
//循环跳出以后,pos正好是要插入的位置
tList->node[pos] =(unsigned int ) node;//在pos处插入地址
++tList->length;
return 0;
}
SeqListNode *SeqList_Get(SeqList*list, int pos)
{
SeqListNode *ret = NULL;
TSeqList *tList = NULL;
tList = (TSeqList *)list;
if (list == NULL || pos < 0 || pos >= tList->length)
{
printf("失败\n");
return NULL;
}
ret = (SeqListNode*)tList->node[pos];
return ret;
}
SeqListNode *SeqList_Delete(SeqList*list, int pos)
{
TSeqList *tlist = NULL;
tlist = (TSeqList *)list;
SeqListNode *ret = NULL;
if (list == NULL || pos < 0 || pos >= tlist->length)
{
printf("删除失败\n");
return NULL;
}
//覆盖之前,要把覆盖之前的地址保存下来
ret = (SeqListNode * )tlist->node[pos];
for (int i = pos + 1; i < tlist->length; i++)
{
tlist->node[i - 1] = tlist->node[i];
}
tlist->length--;
return ret;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"seqlist.h"
typedef struct Teacher
{
char name[10];
int age;
}Teacher;
int main()
{
SeqList *list = NULL;
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
list = SeqList_Create(10);
//头插法
SeqList_Insert(list, (SeqListNode *)&t1, 0);//插入结点
SeqList_Insert(list, (SeqListNode *)&t2, 0);
SeqList_Insert(list, (SeqListNode *)&t3, 0);
for (int i = 0; i<SeqList_Length(list);i++ )
{
Teacher *tmp = (Teacher *)SeqList_Get(list, i);
if (tmp != NULL)
{
printf("t%d.age=%d\n", i+1, tmp->age);
}
}
for (int i = 0; SeqList_Length(list)>0;)
{
Teacher *ret=(Teacher*)SeqList_Delete(list, 0);
printf("t%d.age=%d\n", i + 1, ret->age);
}
SeqList_Destroy(list);
system("pause");
return 0;
}