博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构-静态链表
阅读量:5101 次
发布时间:2019-06-13

本文共 5173 字,大约阅读时间需要 17 分钟。

#ifndef _STATICLSIT_H_ #define _STATICLIST_H_ typedef void StaticList; typedef void StaticListNode; /* 该方法用于创建并且返回一个空的线性表 */ StaticList* StaticList_Create(int capacity); /* 该方法用于销毁一个线性表list */ void StaticList_Destroy(StaticList* list); /* 该方法用于将一个线性表list中的所有元素清空 使得线性表回到创建时的初始状态 */ void StaticList_Clear(StaticList* list); /* 该方法用于返回一个线性表list中的所有元素个数 */ int StaticList_Length(StaticList* list); int StaticList_Empty(StaticList* list); int StaticList_Capacity(StaticList* list); /* 该方法用于向一个线性表list的pos位置处插入新元素node 返回值为1表示插入成功,0表示插入失败 */ int StaticList_Insert(StaticList* list, StaticListNode* node, int pos); /* 该方法用于获取一个线性表list的pos位置处的元素 返回值为pos位置处的元素,NULL表示获取失败 */ StaticListNode* StaticList_Get(StaticList* list, int pos); /* 该方法用于删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 */ StaticListNode* StaticList_Delete(StaticList* list, int pos); #endif #include <stdio h=""> #include <malloc h=""> #include "StaticList.h" #define AVAILABLE -1 typedef struct _tag_StaticListNode { unsigned int data; int next; } TStaticListNode; typedef struct _tag_SaticList { int capacity; TStaticListNode header; TStaticListNode node[]; } TStaticList; StaticList* StaticList_Create(int capacity) { TStaticList* ret = NULL; int i = 0; ret = (TStaticList*)malloc(sizeof(TStaticList)+(capacity+1)*sizeof(TStaticListNode)); if (ret != NULL) { ret->capacity = capacity; ret->header.data = 0; ret->header.next = 0; for(i=1;i<=capacity;i++) { ret->node[i].next = AVAILABLE; } } return ret; } void StaticList_Destroy(StaticList* list) { free(list); } void StaticList_Clear(StaticList* list) { TStaticList* slist = (TStaticList*)list; int i=0; if (slist != NULL) { slist->header.data = 0; slist->header.next = 0; for(i=1;i<=slist->capacity;i++) { slist->node[i].next = AVAILABLE; } } } int StaticList_Length(StaticList* list) { TStaticList* slist = (TStaticList*)list; int ret = -1; if (slist != NULL) { ret = slist->header.data; } return ret; } int StaticList_Empty(StaticList* list) { TStaticList* slist = (TStaticList*)list; int ret = 0; if ((slist != NULL)&&(slist->header.data == 0)) { ret = 1; } return ret; } int StaticList_Capacity(StaticList* list) { TStaticList* slist = (TStaticList*)list; int ret = -1; if (slist != NULL) { ret = slist->capacity; } return ret; } int StaticList_Insert(StaticList* list, StaticListNode* node, int pos) { TStaticList* slist = (TStaticList*)list; int ret = 0; int index = 0; int current = 0; int i = 0; if((slist != NULL)&&(node != NULL)&&(0 <=pos)&&(slist->header.data + 1 <slist->capacity)) { ret = 1; } if(ret) { for(i=1;i<=slist->capacity;i++) { if(slist->node[i].next == AVAILABLE) { index = i; break; } } slist->node[0] = slist->header; slist->node[index].data = (unsigned int)node; for(i=0;(i<pos slist-="">node[current].next != 0);i++) { current = slist->node[current].next; } slist->node[index].next = slist->node[current].next; slist->node[current].next = index; slist->node[0].data++; slist->header = slist->node[0]; } return ret; } StaticListNode* StaticList_Get(StaticList* list, int pos) { TStaticList* slist = (TStaticList*)list; StaticListNode* ret = NULL; int i = 0; int current = 0; int object = 0; if((slist != NULL) && (0 <= pos) && (pos < slist->header.data)) { slist->node[0] = slist->header; for(i=0;i<pos i="" current="slist-">node[current].next; } object = slist->node[current].next; ret = (StaticListNode*)(slist->node[object].data); } return ret; } StaticListNode* StaticList_Delete(StaticList* list, int pos) { TStaticList* slist = (TStaticList*)list; StaticListNode* ret = NULL; int current = 0; int i = 0; int object = 0; if((slist != NULL)&&(0 <=pos)) { slist->node[0] = slist->header; for(i=0;(i<pos slist-="">node[current].next != 0);i++) { current = slist->node[current].next; } object = slist->node[current].next; ret = (StaticListNode*)(slist->node[object].data); slist->node[current].next = slist->node[object].next; slist->node[0].data--; slist->header = slist->node[0]; slist->node[object].next = AVAILABLE; } return ret; } #include <stdio h=""> #include <stdlib h=""> #include "StaticList.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { StaticList* list = StaticList_Create(10); printf("the StaticList capacity is %d\n",StaticList_Capacity(list)); char v1 = 'a'; char v2 = 'b'; char v3 = 'c'; char v4 = 'd'; char v5 = 'e'; char v6 = 'f'; char v7 = 'g'; int i = 0; StaticList_Insert(list,(StaticListNode*)&v1,0); StaticList_Insert(list,(StaticListNode*)&v2,0); StaticList_Insert(list,(StaticListNode*)&v3,0); StaticList_Insert(list,(StaticListNode*)&v4,0); StaticList_Insert(list,(StaticListNode*)&v5,0); StaticList_Insert(list,(StaticListNode*)&v6,0); StaticList_Insert(list,(StaticListNode*)&v7,0); for(i=0;i<StaticList_Length(list);i++) { char* p = (char *)StaticList_Delete(list,StaticList_Length(list)-1); printf("the StaticList length is %d\n",StaticList_Length(list)); printf("the %d elem is %c\n",6-StaticList_Length(list),*p); } printf("\n"); StaticList_Clear(list); printf("the StaticList is Empty(%d)\n",StaticList_Empty(list)); StaticList_Destroy(list); return 0; }

转载于:https://www.cnblogs.com/pangblog/p/3299274.html

你可能感兴趣的文章
Screening technology proved cost effective deal
查看>>
mysql8.0.13下载与安装图文教程
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
Kotlin动态图
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
快速幂
查看>>