1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python线性表顺序存储实现_数据结构——基于C的线性表的顺序存储结构的基本操作的实现...

python线性表顺序存储实现_数据结构——基于C的线性表的顺序存储结构的基本操作的实现...

时间:2023-06-03 11:29:13

相关推荐

python线性表顺序存储实现_数据结构——基于C的线性表的顺序存储结构的基本操作的实现...

/***

*SeqList.c

*Copyright (c) , XZG. All rights reserved.

*Purpose:

* 线性表顺序存储结构的创建、数据插入、数据获取、获取长度、删除数据、清空数据、销毁顺序存储结构方法的实现

***/

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

typedef struct _SeqList

{

unsigned int* node;

int length;

int capacity;

}TSeqList;

typedef void SeqList;

typedef void SeqListNode;

//线性表顺序存储结构的创建

SeqList* SeqList_Create(int capacity)

{

//为结构体分配内存

/*第一种内存分配方式

TSeqList* list = (TSeqList *)malloc(sizeof(TSeqList));

为结构体中的数组分配内存

list->node = (unsigned int *)malloc(sizeof(unsigned int)*capacity);

*/

/*第二种内存分配方式

TSeqList* list = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(unsigned int)*capacity);

list->node = (unsigned int*)list++;

*/

TSeqList* ret = NULL;

if (capacity <= 0)

{

return NULL;

}

ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(unsigned int)*capacity);;

if (ret == NULL)

{

return NULL;

}

ret->node = (unsigned int*)(ret + 1);

if (ret->node == NULL)

{

return NULL;

}

//对其初始化

ret->capacity = capacity;

ret->length = 0;

memset(ret->node, 0, sizeof(unsigned int)*capacity);

return ret;

}

//在线性表顺序存储结构的指定位置插入数据

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)

{

TSeqList *tList = NULL;

//判断指针是否为空

if (list == NULL)

{

return -1;

}

tList = (TSeqList*)(list);

if (node == NULL)

{

return -1;

}

//判断是否满了

if (tList->length >= tList->capacity)

{

return -2;

}

//判断位置是否越界

if (pos < 0 || pos >= tList->capacity)

{

return -2;

}

if (pos >= tList->length)

{

pos = tList->length;

}

//插入算法

for (int i = tList->length; i > pos; i--)

{

//将第pos个元素到最后一个元素向后移动一格

tList->node[i] = tList->node[i - 1];

}

tList->node[pos] = (unsigned int)node;

tList->length++;

return 0;

}

//删除线性表顺序存储结构指定位置的数据

SeqListNode* SeqList_Delete(SeqList* list, int pos)

{

TSeqList *tList = NULL;

SeqListNode* ret = NULL;

//判断指针是否为空

if (list == NULL)

{

return NULL;

}

tList = (TSeqList*)(list);

//判断位置是否越界

if (pos < 0 || pos >= tList->length)

{

return NULL;

}

ret = (SeqListNode*)tList->node[pos];

for (int i = pos; i < tList->length; i++)

{

//将第pos个元素到最后一个元素向后移动一格

tList->node[i] = tList->node[i + 1];

}

tList->length--;

return ret;

}

//获取线性表顺序存储结构指定位置的数据

SeqListNode* SeqList_Get(SeqList* list, int pos)

{

TSeqList *tList = NULL;

SeqListNode* ret = NULL;

//判断指针是否为空

if (list == NULL)

{

return NULL;

}

tList = (TSeqList*)(list);

//判断位置是否越界

if (pos < 0 || pos >= tList->length)

{

return NULL;

}

ret = (SeqListNode*)tList->node[pos];

return ret;

}

//初始化线性表顺序存储结构

void SeqList_Clear(SeqList* list)

{

TSeqList *tList = NULL;

//判断指针是否为空

if (list == NULL)

{

return;

}

tList = (TSeqList*)(list);

tList->length = 0;

memset(tList->node, 0, sizeof(unsigned int)*tList->capacity);

return;

}

//获取线性表顺序存储结构的长度

int SeqList_Length(SeqList* list)

{

TSeqList *tList = NULL;

//判断指针是否为空

if (list == NULL)

{

return 0;

}

tList = (TSeqList*)(list);

return tList->length;

}

//获取线性表顺序存储结构的容量

int SeqList_Capacity(SeqList* list)

{

TSeqList *tList = NULL;

//判断指针是否为空

if (list == NULL)

{

return 0;

}

tList = (TSeqList*)(list);

return tList->capacity;

}

//销毁线性表顺序存储结构

int SeqList_Destory(SeqList* list)

{

//判断指针是否为空

if (list == NULL)

{

return 0;

}

free(list);

list = NULL;

return 0;

} 下面为我写的测试用例:

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "SeqList.h"

typedef struct _Teacher

{

char name[30];

int age;

}Teacher;

void main()

{

SeqList * s = NULL;

Teacher t1, t2, t3;

t1.age = 11;

t2.age = 22;

t3.age = 33;

s = SeqList_Create(10);//线性表数据创建

SeqList_Insert(s, (SeqListNode*)&t1, 0);//线性表数据插入

SeqList_Insert(s, (SeqListNode*)&t2, 0);

SeqList_Insert(s, (SeqListNode*)&t3, 0);

int i = 0, len = 0;

len = SeqList_Length(s);//获取线性表长度

for ( i = 0; i < len; i++)

{

Teacher * tmp = (Teacher*)SeqList_Get(s, i);//获取线性表中指定的数据

printf("%d ",tmp->age);

}

printf("\n");

for ( i = 0; i < len; i++)

{

Teacher * tmp = (Teacher*)SeqList_Delete(s, 0);//删除线性表中数据

printf("%d _", tmp->age);

}

SeqList_Destory(s);

system("pause");

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。