目录:
- 代码:
- 分析:
- 汇编:
代码:
SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_ typedef void SeqList; //定义链表数据类型,void因为要适用不同链表数据类型
typedef void SeqListNode; //定义链表节点类型 void因为要适用不同节点类型 SeqList* SeqList_Create(int capacity);//声明创建链表函数void SeqList_Destroy(SeqList* list); //声明删除链表函数void SeqList_Clear(SeqList* list);//声明获取链表当前长度函数int SeqList_Length(SeqList* list);//声明获取链表当前长度函数int SeqList_Capacity(SeqList* list);//声明获取链表容量函数int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);//声明插入数据函数SeqListNode* SeqList_Get(SeqList* list, int pos);//声明获取数据函数SeqListNode* SeqList_Delete(SeqList* list, int pos);//声明删除一个数据函数#endif
SeqList.c
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"typedef unsigned int TSeqListNode; typedef struct _tag_SeqList
{int capacity;int length;TSeqListNode* node; //链表类型中存放指向数据的指针(数组)
} TSeqList; //定义链表数据类型 SeqList* SeqList_Create(int capacity) //定义创建链表函数 根据参数容量创建
{TSeqList* ret = NULL;if( capacity >= 0 ){ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);}if( ret != NULL ) //成功{ret->capacity = capacity; //赋值容量ret->length = 0; //当前长度 下标ret->node = (TSeqListNode*)(ret + 1);//加1 刚好是数组首元素地址}return ret; //返回自定义的链表数据类型
}void SeqList_Destroy(SeqList* list) //定义删除链表函数
{free(list);
}void SeqList_Clear(SeqList* list)//定义清除链表长度重设为0
{TSeqList* sList = (TSeqList*)list;if( sList != NULL ){sList->length = 0;}
}int SeqList_Length(SeqList* list) //定义获取链表当前长度函数
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->length;}return ret;
}int SeqList_Capacity(SeqList* list) //定义获取链表容量函数
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->capacity;}return ret;
}int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //定义插入数据函数
{TSeqList* sList = (TSeqList*)list;int ret = (sList != NULL);int i = 0;ret = ret && (sList->length + 1 <= sList->capacity); //判断链表是否满了ret = ret && (0 <= pos); //判断要插入的位置不能小于0if( ret ) //上面条件满足{if( pos >= sList->length ) //是否大于当前的长度下标{pos = sList->length; //表示直接插入的位置就是最后一个}for(i=sList->length; i>pos; i--) //循环元素后移{sList->node[i] = sList->node[i-1];}sList->node[i] = (TSeqListNode)node;//将链表数据的地址转成数值赋值到元素sList->length++; //长度增加}return ret;
}SeqListNode* SeqList_Get(SeqList* list, int pos) //定义获取数据函数
{TSeqList* sList = (TSeqList*)list;SeqListNode* ret = NULL;if( (sList != NULL) && (0 <= pos) && (pos < sList->length) ) //判断不能超出范围{ret = (SeqListNode*)(sList->node[pos]); //移到指针 指针的值就是地址转成的数值,再转回指针}return ret;
}SeqListNode* SeqList_Delete(SeqList* list, int pos) //定义删除一个数据函数
{TSeqList* sList = (TSeqList*)list;SeqListNode* ret = SeqList_Get(list, pos); //将数据获取出来int i = 0;if( ret != NULL ){for(i=pos+1; i<sList->length; i++)//循环将元素前移{sList->node[i-1] = sList->node[i];}sList->length--;//当前长度减少}return ret;
}
smain.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"int main(int argc, char *argv[])
{SeqList* list = SeqList_Create(5);int i = 10;int j = 11;int k = 12;int x = 13;int y = 14;int z = 15;int index = 0;SeqList_Insert(list, &i, 0);SeqList_Insert(list, &j, 0);SeqList_Insert(list, &k, 0);SeqList_Insert(list, &x, 0);SeqList_Insert(list, &y, 0);SeqList_Insert(list, &z, 0);for(index=0; index<SeqList_Length(list); index++){int* p = (int*)SeqList_Get(list, index);printf("%d\n", *p);}printf("\n");while( SeqList_Length(list) > 0 ){int* p = (int*)SeqList_Delete(list, 0);printf("%d\n", *p);}SeqList_Destroy(list);getchar();return 0;
}
分析:
汇编: