线性表的顺序存储设计与实现
1.概念
用一段地址连续的存储单元依次存储线性表的数据元素
2.设计与实现
(1)插入元素
a 判断线性表是否合法
b 判断插入位置是否合法
c 把最后一个元素到插入位置的元素后移一个位置
d 将新元素插入
e 线性表长度加1
(2)获取元素位置
a 判断线性表是否合法
b 判断插入位置是否合法
c 直接通过数组下标的方式获取元素
(3)删除元素
a 判断线性表是否合法
b 判断插入位置是否合法
c 将元素取出
d 将删除位置元素后的元素分别向前移动一个位置
e 线性表长度减1
代码如下:
(1)seqlist.h
#ifndef __MY_SEQLIST_H__
#define __MY_SEQLIST_H__
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Destory(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 __MY_SEQLIST_H__
(2)
seqlist.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "seqlist.h"
typedef struct _tag_SeqList
{
int length;
int capacity;
unsigned int **node; //int* node[] //指针数组
}TSeqList;
SeqList* SeqList_Create(int capacity)
{
int ret = 0;
TSeqList *tmp = NULL;
tmp = (TSeqList*)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret = -1;
printf("func SeqList_Create() err:%d\n", ret);
return NULL;
}
memset(tmp, 0, sizeof(TSeqList));
//根据capacity的大小分配结点的空间
tmp->node = (unsigned int *)malloc(sizeof(unsigned int *) * capacity);
if (tmp->node == NULL)
{
ret = -2;
printf("func malloc() err:%d\n", ret);
return NULL;
}
tmp->capacity = capacity;
tmp->length = 0;
return tmp;
}
void SeqList_Destory(SeqList* list)
{
TSeqList* tlist = NULL;
if (list == NULL)
{
return ;
}
tlist = (TSeqList*)list;
if (tlist->node != NULL)
{
free(tlist->node);
}
free(tlist);
return ;
}
//清空链表 回到初始化状态
void SeqList_Clear(SeqList* list)
{
TSeqList* tlist = NULL;
if (list == NULL)
{
return;
}
tlist = (TSeqList*)list;
tlist->length = 0;
return ;
}
int SeqList_Length(SeqList* list)
{
TSeqList* tlist = NULL;
if (list == NULL)
{
return -1;
}
tlist = (TSeqList*)list;
return tlist->length;
}
int SeqList_Capacity(SeqList* list)
{
TSeqList* tlist = NULL;
if (list == NULL)
{
return -1;
}
tlist = (TSeqList*)list;
return tlist->capacity;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
int ret = 0;
int i = 0;
TSeqList* tlist = NULL;
if (list == NULL || node == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Insert() err:%d\n", ret);
return ret;
}
tlist = (TSeqList*)list;
//判断是否满了
if (tlist->length >= tlist->capacity)
{
ret = -2;
printf("func SeqList_Insert() (tlist->length >= tlist->capacity) err:%d\n", ret);
return ret;
}
//容错修正
if (pos > tlist->length)
{
pos = tlist->length;
}
//1 元素后移
for (i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i - 1];
}
//2 插入值
tlist->node[i] = (unsigned int)node;
tlist->length++;
return 0;
}
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
int ret = 0;
SeqListNode* tmp = 0;
TSeqList* tlist = NULL;
//1 判断线性表及删除位置是否合法
if (list == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Get() err:%d\n", ret);
return NULL;
}
tlist = (TSeqList*)list;
//2 直接通过数组下标的方式获取元素
tmp = (void*)tlist->node[pos];
return tmp;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
int i = 0, ret = 0;
SeqListNode* tmp = 0;
TSeqList* tlist = NULL;
//1 判断线性表及删除位置是否合法
if (list == NULL || pos < 0)
{
ret = -1;
printf("func SeqList_Get() err:%d\n", ret);
return NULL;
}
tlist = (TSeqList*)list;
//2 将元素取出
tmp = (SeqListNode *)tlist->node[pos];
//3 将删除位置之后的元素分别向前移动一个位置
for (i = pos + 1; i < tlist->length; i++)
{
tlist->node[i - 1] = tlist->node[i];
}
//4 线性表长度减1
tlist->length--;
return tmp;
}