线性表
线性表是0个或多个类型相同的数据元素的集合,这些元素是有限的顺序存储的。
一个线性表有哪些具体的操作,在程序中的表现为一组函数
- 创建线性表
- 销毁线性表
- 清空线性表
- 将元素插入线性表
- 将元素从线性表中删除
- 获取线性表中某个位置的元素
- 获取线性表的长度
线性表的顺序存储
一段地址连续的存储单元依次存储线性表的数据元素
插入数据:
在某个位置插入一个节点,我们需要检索从插入位置起到尾部元素,从尾部元素开始依次向后移一个位置。
删除数据:
删除某个位置上的节点,如果需要返回这个节点,我们需要提前缓存先来,之后从这个节点的后一个节点开始依次向前移动一个位置。
顺序存储的优缺点:
优点:
无需为线性表中的逻辑关系增加额外的空间
可以快速的获取线性表中合法位置的元素
缺点:
需要提前确定线性表的容量
插入和删除操作需要移动大量元素
//SeqList.h
#pragma once
typedef void SeqList;
typedef void SeqListNode;
//根据自定义容量创建并返回一个空的线性表
SeqList *SeqList_Creat(int capacity);
//销毁一个线性表
void SeqList_Destroy(SeqList *list);
//将线性表中的元素清空,恢复到初始化状态
void SeqList_Clear(SeqList *list);
//返回线性表中的元素个数
int SeqList_GetLength(SeqList *list);
//返回线性表中的容量
int SeqList_GetCapacity(SeqList *list);
//往线性表中的pos位置处插入一个行的元素(节点)
int SeqList_inster(SeqList *list, SeqListNode *node, int pos);
//获取线性表中pos位置处的元素(节点),并返回
SeqListNode *SeqList_Get(SeqList *list, int pos);
//根据pos位置删除线性表中的元素(节点)
SeqListNode *SeqList_Delete(SeqList *list, int pos);
//SeqList.c
#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqList.h"
typedef struct tag_SeqList
{
int capacity;
int length;
unsigned int **node;//int *node[]
}TSeqList;
//根据所给容量初始化线性表
SeqList *SeqList_Creat(int capacity)
{
TSeqList * temp = NULL;
if (capacity <= 0)
{
printf("SeqList_Creat err : capacity <= 0");
return NULL;
}
//分配结构体TSeqList的空间
temp = (TSeqList *)malloc(sizeof(TSeqList));
if (temp == NULL)
{
printf("SeqList_Creat err : temp = NULL");
return NULL;
}
memset(temp, 0, sizeof(TSeqList));
//根据capacity 的大小分配节点的空间
temp->node = (unsigned int **)malloc(sizeof(unsigned int **)*capacity);
if (temp->node == NULL)
{
printf("SeqList_Creat err : temp->node = NULL");
return NULL;
}
temp->capacity = capacity;
temp->length = 0;
return temp;
}
//删除节点空间和结构体空间
void SeqList_Destroy(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_Destroy err : list = NULL");
return;
}
temp = (TSeqList *)list;
if (temp->node != NULL)
{
free(temp->node);
}
free(temp);
temp->length = 0;
temp->capacity = 0;
printf("SeqList_Destroy finished \n");
}
//清空链表 回到初始化状态
void SeqList_Clear(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_Clear err : list = NULL");
return ;
}
temp = (TSeqList *)list;
temp->length = 0;
printf("SeqList_Destroy cleared \n");
}
//获取链表长度
int SeqList_GetLength(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_GetLength err : list = NULL");
return -1;
}
temp = (TSeqList *)list;
return temp->length;
}
//获取链表容量
int SeqList_GetCapacity(SeqList * list)
{
TSeqList *temp = NULL;
if (list == NULL)
{
printf("SeqList_GetCapacity err : list = NULL");
return -1;
}
temp = (TSeqList *)list;
return temp->capacity;
}
//插入节点到线性表
int SeqList_inster(SeqList * list, SeqListNode * node, int pos)
{
TSeqList *temp = NULL;
int i = 0;
if (list == NULL || node == NULL || pos < 0)
{
printf("SeqList_inster err : list = NULL || node = NULL || pos < 0");
return -1;
}
temp = (TSeqList *)list;
//判断插入的位置是否越界
if (pos > temp->length)
{
pos = temp->length;
}
//判断线性表的节点是否已满
if (temp->length == temp->capacity)
{
printf("SeqList_inster err : temp->length = temp->capacity");
return -2;
}
//要在线性表某个位置插入节点,那么后面的节点应该向后移
//从最后一个节点开始,每个节点依次向后移动
for (i = temp->length; i > pos; i--)
{
temp->node[i] = temp->node[i - 1];
}
temp->node[pos] = node;
temp->length++;
return 0;
}
//根据位置获得节点
SeqListNode * SeqList_Get(SeqList *list, int pos)
{
TSeqList *temp = NULL;
if (list == NULL || pos <0)
{
printf("SeqList_Get err : list = NULL || pos <0");
return NULL;
}
temp = (TSeqList *)list;
//判断获取的位置是否越界
if (pos > temp->length)
{
printf("SeqList_Get err : pos > temp->length");
return NULL;
}
return temp->node[pos];
}
SeqListNode * SeqList_Delete(SeqList * list, int pos)
{
TSeqList *temp = NULL;
SeqListNode * SLNode = NULL;
int i = 0;
if (list == NULL || pos < 0)
{
printf("SeqList_Delete err : list = NULL || pos <0");
return NULL;
}
temp = (TSeqList *)list;
//判断获取的位置是否越界
if (pos > temp->length)
{
printf("SeqList_Delete err : pos > temp->length");
return NULL;
}
//删除之前先把要删除的节点缓存下来
//从要删除的位置开始,把后一个节点移动到前一个节点的位置
SLNode = temp->node[pos];
for (i = pos; i < temp->length; i++)
{
temp->node[i] = temp->node[i + 1];
}
temp->length--;
return SLNode;
}
//main.c
#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SeqList.h"
typedef struct tag_Teacher
{
int age;
char *name;
}Teacher;
int main(int argc, char** argv)
{
Teacher t1, t2, t3, t4, t5;
t1.age = 20;
t1.name = "zhangsan";
t2.age = 21;
t2.name = "lisi";
t3.age = 22;
t3.name = "wangwu";
t4.age = 23;
t4.name = "zhaoliu";
t5.age = 24;
t5.name = "wangqi";
SeqList *list = NULL;
int i, ret = 0;
list = SeqList_Creat(10);
if (list == NULL)
{
ret = -1;
printf("SeqList_Creat err : list = NULL");
return ret;
}
ret = SeqList_inster(list, (SeqListNode *)&t1, 0);
ret = SeqList_inster(list, (SeqListNode *)&t2, 0);
ret = SeqList_inster(list, (SeqListNode *)&t3, 0);
ret = SeqList_inster(list, (SeqListNode *)&t4, 0);
ret = SeqList_inster(list, (SeqListNode *)&t5, 0);
printf("SeqList capacity = %d\n", SeqList_GetCapacity(list));
printf("SeqList length = %d\n", SeqList_GetLength(list));
for (i = 0; i < SeqList_GetLength(list); i++)
{
Teacher *t = SeqList_Get(list, i);
printf("age = %d,name = %s\n", t->age, t->name);
}
SeqList_Delete(list, 3);
printf("SeqList_Delete \n");
for (i = 0; i < SeqList_GetLength(list); i++)
{
Teacher *t = SeqList_Get(list, i);
printf("age = %d,name = %s\n", t->age, t->name);
}
SeqList_Destroy(list);
system("pause");
return 0;
}