您的位置:首页 > 其它

线性表顺序存储设计与实现

2019-05-05 20:56 351 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FAKER_0X0/article/details/89855018

seqlist.h

[code]//#pragma once		//和下面#ifndef作用一样
#ifndef _MY_SEQLIST_H_
#define _MY_SEQLIST_H_

typedef void SeqList;
typedef void SeqListNode;

//创建并且返回一个空的线性表
SeqList* SeqList_Create(int capacity);

//销毁一个线性表list
void SeqList_Destroy(SeqList* list);

//将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态
void SeqList_Clear(SeqList* list);

//返回一个线性表list中的所有元素个数
int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

//向一个线性表list的pos位置处插入新元素node
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

//获取一个线性表list的pos位置处的元素
SeqListNode* SeqList_Get(SeqList* list, int pos);

//删除一个线性表list的pos位置处的元素  返回值为被删除的元素,NULL表示删除失败
SeqListNode* SeqList_Delete(SeqList* list, int pos);

#endif

seqlist.cpp

[code]//打桩
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.h"

typedef struct _tag_SeqList	//在结构体中套一级指针
{
int length;
int capacity;
unsigned int *node;	//需实现内存动态分配	int node[]

//unsigned int **node;	//测试后用二级指针会比较好?用二级指针会出现参数转换错误。。。

}TSeqList;	//重点:TSeqList==>底层库的内存句柄

//创建并且返回一个空的线性表
SeqList* SeqList_Create(int capacity)
{
int ret = 0;
TSeqList *tmp = (TSeqList *)malloc(sizeof(TSeqList));	//分配结构体的内存
if (tmp == NULL)
{
ret = -1;
printf("func SeqList_Create() err: %d\n", ret);
return NULL;
}
//memset将某一块内存中的内容全部设置为指定的值, 这个函数通常为新申请的内存做初始化工作
memset(tmp, 0, sizeof(TSeqList));	//初始化为0

//根据capacity大小分配结点空间
tmp->node = (unsigned int *)malloc(sizeof(unsigned int *) *capacity);	//重点:语法*capacity

//tmp->node = (unsigned int **)malloc(sizeof(unsigned int *) *capacity);	//换二级指针后

if (tmp->node == NULL)
{
ret = -2;
printf("func SeqList_Create() err: malloc err %d\n", ret);
return NULL;
}
tmp->capacity = capacity;
tmp->length = 0;
return tmp;
}

//销毁一个线性表list
void SeqList_Destroy(SeqList* list)		//先释放后分配的内存
{
TSeqList* tlist = NULL;
if (list == NULL)	//空则不需用释放
{
return;
}
tlist = (TSeqList*)list;//别人不知道什么类型我知道是什么类型,别人传来是void,我们知道在底层库中是TSeqList
if (tlist->node != NULL)
{
free(tlist->node);
}
free(tlist);
return ;
}

//将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态
void SeqList_Clear(SeqList* list)
{
//代码复用以上的
TSeqList* tlist = NULL;
if (list == NULL)
{
return;
}
tlist = (TSeqList*)list;
tlist->length = 0;
return ;
}

//返回一个线性表list中的所有元素个数
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;
}

//向一个线性表list的pos位置处插入新元素node
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
int ret = 0, 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;
}

//容错修正	6个长度 容量20,用户pos10位置插入..
if (pos > tlist->length)
{
pos = tlist->length;
}

//1 元素后移
for (i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i - 1];
//a[7]=a[6]
}
//2 pos位置插入元素
tlist->node[pos] = (unsigned int)node;	//类型转换

//tlist->node[pos] = node;	//换二级指针后

tlist->length++;	//长度+1
return 0;
}

//获取一个线性表list的pos位置处的元素
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
int i = 0;
SeqListNode* ret = NULL;
TSeqList* tlist = NULL;
if (list == NULL || pos < 0)
{
printf("func SeqList_Get() err: %d\n", ret);
return ret;
}
tlist = (TSeqList*)list;
ret = (SeqListNode*)tlist->node[pos];
return ret;
}

//删除一个线性表list的pos位置处的元素  返回值为被删除的元素,NULL表示删除失败
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
int i = 0;
SeqListNode* ret = NULL;
TSeqList* tlist = NULL;
if (list == NULL || pos < 0)
{
printf("func SeqList_Delete() err: %d\n", ret);
return ret;
}
tlist = (TSeqList*)list;

ret = (SeqListNode*)tlist->node[pos];	//缓存位置pos的元素
for (i = pos + 1; i < tlist->length; i++)	//pos位置后面的元素前移
{
tlist->node[i - 1] = tlist->node[i];
//a[6]=a[7]
}
tlist->length--;
return ret;
}

测试框架(VS2015编译)

这里有个用.c文件还是.cpp文件的问题...

[code]#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.cpp"	//这里还是要加.cpp文件而不是.h文件,这里不能用.c文件,可能是和MFC库冲突?

typedef struct _Teacher
{
int age;
char name[64];
}Teacher;

void main()
{
int ret = 0, i = 0;
SeqList* list = NULL;

Teacher t1, t2, t3, t4, t5;
t1.age = 31;
t2.age = 32;
t3.age = 33;
t4.age = 34;
t5.age = 35;

list = SeqList_Create(10);//创建线性表
if (list == NULL)
{
printf("func SeqList_Create() ret:%d \n", ret);
return;
}

ret = SeqList_Insert(list, (SeqListNode*)&t1, 0);	//头插法 插入线性表
ret = SeqList_Insert(list, (SeqListNode*)&t2, 0);	//头插法
ret = SeqList_Insert(list, (SeqListNode*)&t3, 0);	//头插法
ret = SeqList_Insert(list, (SeqListNode*)&t4, 0);	//头插法
ret = SeqList_Insert(list, (SeqListNode*)&t5, 0);	//头插法,所以t5是第一个

//遍历
for (i = 0; i < SeqList_Length(list); i++)
{
Teacher* tmp = (Teacher*)SeqList_Get(list, i);	//获取i号位置元素 0-4位置
if (tmp == NULL)
{
return;
}
printf("tmp->age: %d \n", tmp->age);
}

//删除元素
while (SeqList_Length(list) > 0)
{
SeqList_Delete(list, 0);	//从头部开始删除元素
}

system("pause");
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: