日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/5/22 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python线性表顺序存储实现_数据结构——基于C的线性表的顺序存储结构的基本操作的实现... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/***

*SeqList.c

*Copyright (c) 2015, 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");

}

總結

以上是生活随笔為你收集整理的python线性表顺序存储实现_数据结构——基于C的线性表的顺序存储结构的基本操作的实现...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。