静态链表详解
《静态链表的创建、插入、删除、销毁代码实现》序言:表的学习也没学习多久,对于我而言大部分都是研究别人的代码,取其精华取其糟粕。链表在学习计算机课程的时候,数据结构是一门基础课程,基础不代表不重要,相反是特别重要,就好比你想学习骑自行车,但是你连走路都不会,怎么会骑自行车呢?哈哈,学习数据结构的基本思路是:顺序表,链表,静态链表、双链表,循环量表等.要求:c语言要懂一点。接下来我们就一起分析下面一段代码吧!#include<stdlib.h>#include<stdio.h>//头函数就好比一个仓库,存储我们需要的基础函数,如printf等#include<malloc.h>#defineAVAILABLE-1typedefvoidStaticList;typedefvoidStaticListNode;/**************头函数定义其实也可以不需要只是为了实现结构化***************///下面通过英语提示大家都应该知道函数的实现功能了吧StaticList*StaticList_Create(intcapacity);//创建voidStaticList_Destroy(StaticList*list);//销毁链表voidStaticList_Clear(StaticList*list);//清除链表intStaticList_Length(StaticList*list);//获取长度intStaticList_Capacity(StaticList*list);//获取容量intStaticList_Insert(StaticList*list,StaticListNode*node,intpos);//插入数据StaticListNode*StaticList_Get(StaticList*list,intpos);//获取元素StaticListNode*StaticList_Delete(StaticList*list,intpos);//删除元素//对于这个结构体的定义相信大家都应该很熟悉了吧,关键在这里typedef的应用typedefstruct_tag_StaticListNode{unsignedintdata;//存放和数据的地方intnext;//指向下一个数组坐标的值}TStaticListNode;//结构体变量相当于别名typedefstruct_tag_StaticList//定义一个数据域结构体{intcapacity;TStaticListNodeheader;//数组头TStaticListNodenode[];//相当于指针地址}TStaticList;/************************创建链表*****************************/StaticList*StaticList_Create(intcapacity)//O(n){TStaticList*ret=NULL;inti=0;if(capacity>=0){/*申请内存大小capacity加一是因为头数据要算一个*/ret=(TStaticList*)malloc(sizeof(TStaticList)+sizeof(TStaticListNode)*(capacity+1));}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;}}returnret;}/*销毁链表内存申请相当于调用了一个free函数*/voidStaticList_Destroy(StaticList*list)//O(1){free(list);}/*清除链表元素*/voidStaticList_Clear(StaticList*list)//O(n){TStaticList*sList=(TStaticList*)list;//bavoidturnpointinti=0;if(sList!=NULL){sList->header.data=0;sList->header.next=0;for(i=1;i<=slist->capacity;i++)/*清除后要定义为可用*/{sList->node[i].next=AVAILABLE;}}}/*获取数组元素个数*/intStaticList_Length(StaticList*list)//O(1){TStaticList*sList=(TStaticList*)list;intret=-1;if(sList!=NULL){ret=sList->header.data;}returnret;}/****获取内存容量***/intStaticList_Capacity(StaticList*list)//O(1){TStaticList*sList=(TStaticList*)list;intret=-1;if(sList!=NULL){ret=sList->capacity;}returnret;}/****插入数据元素***/intStaticList_Insert(StaticList*list,StaticListNode*node,intpos)//O(n){/***参数1链表头指针参数2具体数据地址参数3位置****/TStaticList*sList=(TStaticList*)list;intret=(sList!=NULL);intcurrent=0;intindex=0;inti=0;/****判断位置是否有效***/ret=ret&&(sList->header.data+1<=slist-="">capacity);ret=ret&&(pos>=0)&&(node!=NULL);if(ret){for(i=1;i<=slist->capacity;i++){if(sList->node[i].next==AVAILABLE){index=i;break;/****判断是否为可用位置***/}}sList->node[index].data=(unsignedint)node;sList->node[0]=sList->header;for(i=0;(inode[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++;/****之后要data加以***/sList->header=sList->node[0];/***节点游标要回到初始位置****/}returnret;}/****获取元素值***/StaticListNode*StaticList_Get(StaticList*list,intpos)//O(n){TStaticList*sList=(TStaticList*)list;StaticListNode*ret=NULL;intcurrent=0;intobject=0;inti=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);/***赋值结构体求出该位置的数据****/}returnret;}/****删除元素具体实现和插入相仿***/StaticListNode*StaticList_Delete(StaticList*list,intpos)//O(n){TStaticList*sList=(TStaticList*)list;StaticListNode*ret=NULL;intcurrent=0;intobject=0;inti=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;sList->node[current].next=sList->node[object].next;/****主要区别还是上面两行代码进行插入实现***/sList->node[0].data--;sList->header=sList->node[0];sList->node[object].next=AVAILABLE;ret=(StaticListNode*)(sList->node[object].data);}returnret;}/***主函数具体实现创建链表,插入、删除、销毁、获取元素、等操作****/intmain(intargc,char*argv[]){StaticList*list=StaticList_Create(10);intindex=0;inti=0;intj=1;intk=2;intx=3;inty=4;intz=5;StaticList_Insert(list,&i,1);StaticList_Insert(list,&j,3);StaticList_Insert(list,&k,2);StaticList_Insert(list,&x,5);StaticList_Insert(list,&y,4);/****刚开始对于这个赋值没有理解后来明白了***/StaticList_Insert(list,&z,6);/****&i也就是插入的元素的地址,这个地址是指向下一个元素的地址***/for(index=0;index<StaticList_Length(list);index++){int*p=(int*)StaticList_Get(list,index);/*****获取元素**/printf("%d\\n",*p);}printf("\\n");while(StaticList_Length(list)>0){int*p=(int*)StaticList_Delete(list,0);/**删除数据**/printf("%d\\n",*p);}printf("\\n");StaticList_Insert(list,&x,0);StaticList_Insert(list,&y,0);/***插入元素****/StaticList_Insert(list,&z,0);printf("Capacity:%dLength:%d\\n",StaticList_Capacity(list),StaticList_Length(list));for(index=0;index<StaticList_Length(list);index++){int*p=(int*)StaticList_Get(list,index);printf("%d\\n",*p);}StaticList_Destroy(list);/**销毁链表,不用的时候必须要进行操作不然会有内泄露*****/return0;}好啦结束了,希望对于你会有一点帮助,我也是自己理解的难免会有都错误,请谅解!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。