目录:
- 代码:
- 分析:
- 汇编:
代码:
StaticList.h
#ifndef _STATICLIST_H_
#define _STATICLIST_H_typedef void StaticList; //空类型静态表类型可以接收任何类型的静态表类型
typedef void StaticListNode;//空类型节点类型可以接收任何类型的节点类型StaticList * StaticList_Create(int capacity);//声明静态表生成函数void StaticList_Destroy(StaticList * list);//声明静态表销毁函数void StaticList_Clear(StaticList * list);//声明静态表清空函数int StaticList_Length(StaticList * list);//声明获取静态表长度函数int StaticList_Capacity(StaticList * list);//声明获取静态表容量函数int StaticList_Insert(StaticList *list, StaticListNode * node, int pos);//声明静态表插入节点函数StaticListNode* StaticList_Get(StaticList * list, int pos);//声明静态表获取元素函数StaticListNode* StaticList_Delete(StaticList *list, int pos);//声明静态表删除节点函数#endif
StaticList.c
#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_StaticList{int capacity;//表的容量TStaticListNode header; //头节点存着当前长度(data)与第一个元素是在哪个节点的下标(next)TStaticListNode node[]; //是不占内存空间的}TStaticList;StaticList * StaticList_Create(int capacity){ //定义静态链表的创建函数TStaticList *ret = NULL;int i = 0;if (capacity >= 0){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 = 0; 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; //重设为0sList->header.next = 0;//重设为0for (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_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 = (sList != NULL);int current = 0; //插入节点的上一个节点的下标int index = 0;//插入的数据在哪个节点(下标)int i = 0;ret = ret && (sList->header.data + 1 <= sList->capacity); //确保有节点存放ret = ret && (pos >= 0) && (node != NULL);//判断插入位置是否正确与节点是否正常if (ret){for (i = 1; i <= sList->capacity; i++){ //从第二个下标开始找到第一个出现next为-1的位置下标if (sList->node[i].next == AVAILABLE){ //找一个空的节点的下标index = i;break;}}sList->node[index].data = (unsigned int)node; //将新插入的元素地址转换存到该下标节点的datasList->node[0] = sList->header; //将表的头节点赋给数组首元素//根据next找到要插入节点的前一个节点//如果sList->node[current].next == 0 表示是最后一个节点for (i = 0; (i < pos) && (sList->node[current].next != 0); i++){current = sList->node[current].next;}sList->node[index].next = sList->node[current].next; //新插入的节点的next等于该节点前一个节点的nextsList->node[current].next = index;// 新插入节点的前一个节点的next等于新插入节点在数组中的下标sList->node[0].data++; //长度增加sList->header = sList->node[0];//将数组首元素赋给header(修改后的数据)}return ret;
}StaticListNode* StaticList_Get(StaticList * list, int pos){ //定义静态链表获取节点数据函数TStaticList* sList = (TStaticList*)list;StaticListNode* ret = NULL;int current = 0;int object = 0;int i = 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 object = 0;int i = 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];//将数组首元素赋给header(修改后的数据)sList->node[object].next = AVAILABLE;//将该节点设为空闲的ret = (StaticListNode*)(sList->node[object].data);//取得删除节点存放的数据转换成指针类型}return ret;//返回该指向的地址
}
main.c
#include<stdio.h>
#include<stdlib.h>
#include"StaticList.h"int main(int argc,char *argv[]){StaticList *list = StaticList_Create(10);int index = 0;int i = 0;int j = 1;int k = 2;int x = 3;int y = 4;int z = 5;StaticList_Insert(list, &i, 0);StaticList_Insert(list, &j, 0);StaticList_Insert(list, &k, 0);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("%d\n", StaticList_Length(list));getchar();return 1;
}
分析:
汇编: