目錄:
- 代碼:
- 分析:
- 匯編:
代碼:
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;
}
分析:
匯編: