静态链表(代码、分析、汇编)
生活随笔
收集整理的這篇文章主要介紹了
静态链表(代码、分析、汇编)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄:
- 代碼:
- 分析:
- 匯編:
代碼:
StaticList.h
#ifndef _STATICLIST_H_ #define _STATICLIST_H_typedef void StaticList; //空類型靜態(tài)表類型可以接收任何類型的靜態(tài)表類型 typedef void StaticListNode;//空類型節(jié)點(diǎn)類型可以接收任何類型的節(jié)點(diǎn)類型StaticList * StaticList_Create(int capacity);//聲明靜態(tài)表生成函數(shù)void StaticList_Destroy(StaticList * list);//聲明靜態(tài)表銷毀函數(shù)void StaticList_Clear(StaticList * list);//聲明靜態(tài)表清空函數(shù)int StaticList_Length(StaticList * list);//聲明獲取靜態(tài)表長(zhǎng)度函數(shù)int StaticList_Capacity(StaticList * list);//聲明獲取靜態(tài)表容量函數(shù)int StaticList_Insert(StaticList *list, StaticListNode * node, int pos);//聲明靜態(tài)表插入節(jié)點(diǎn)函數(shù)StaticListNode* StaticList_Get(StaticList * list, int pos);//聲明靜態(tài)表獲取元素函數(shù)StaticListNode* StaticList_Delete(StaticList *list, int pos);//聲明靜態(tài)表刪除節(jié)點(diǎn)函數(shù)#endifStaticList.c
#include<stdio.h> #include<malloc.h> #include "StaticList.h"#define AVAILABLE -1 //控制節(jié)點(diǎn)是空閑typedef struct _tag_StaticListNode{unsigned int data; //節(jié)點(diǎn)存放數(shù)據(jù) 重點(diǎn):該元素一定要放在結(jié)構(gòu)體第一個(gè),要不然獲取時(shí)錯(cuò)誤int next; //下一個(gè)節(jié)點(diǎn)的下標(biāo) }TStaticListNode;typedef struct _tag_StaticList{int capacity;//表的容量TStaticListNode header; //頭節(jié)點(diǎn)存著當(dāng)前長(zhǎng)度(data)與第一個(gè)元素是在哪個(gè)節(jié)點(diǎn)的下標(biāo)(next)TStaticListNode node[]; //是不占內(nèi)存空間的}TStaticList;StaticList * StaticList_Create(int capacity){ //定義靜態(tài)鏈表的創(chuàng)建函數(shù)TStaticList *ret = NULL;int i = 0;if (capacity >= 0){ret = (TStaticList*)malloc(sizeof(TStaticList)+sizeof(TStaticListNode)*(capacity + 1));}if (ret != NULL){//申請(qǐng)內(nèi)存成功ret->capacity = capacity;//設(shè)置容量ret->header.data = 0;//當(dāng)前長(zhǎng)度ret->header.next = 0;//首節(jié)點(diǎn)下標(biāo)for (i = 0; i <= capacity; i++){ //將全部節(jié)點(diǎn)設(shè)為空閑ret->node[i].next = AVAILABLE;}}return ret; }void StaticList_Destroy(StaticList * list){//定義靜態(tài)鏈表的銷毀函數(shù)free(list); }void StaticList_Clear(StaticList * list){//定義靜態(tài)鏈表的清空函數(shù)TStaticList* sList = (TStaticList *)list;int i = 0;if (sList != NULL){//判斷表不為空sList->header.data = 0; //重設(shè)為0sList->header.next = 0;//重設(shè)為0for (i = 1; i <= sList->capacity; i++){ //將全部節(jié)點(diǎn)設(shè)為空閑sList->node[i].next = AVAILABLE;}} }int StaticList_Length(StaticList * list){ //定義獲取靜態(tài)鏈表當(dāng)前長(zhǎng)度函數(shù)TStaticList * sList = (TStaticList *)list;int ret = -1;if (sList != NULL){ret = sList->header.data;}return ret; }int StaticList_Capacity(StaticList * list){//定義獲取靜態(tài)鏈表容量函數(shù)TStaticList * sList = (TStaticList*)list;int ret = -1;if (sList != NULL){ret = sList->capacity;}return ret; }int StaticList_Insert(StaticList *list, StaticListNode * node, int pos){//定義靜態(tài)鏈表的插入元素函數(shù)TStaticList * sList = (TStaticList*)list;int ret = (sList != NULL);int current = 0; //插入節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)的下標(biāo)int index = 0;//插入的數(shù)據(jù)在哪個(gè)節(jié)點(diǎn)(下標(biāo))int i = 0;ret = ret && (sList->header.data + 1 <= sList->capacity); //確保有節(jié)點(diǎn)存放ret = ret && (pos >= 0) && (node != NULL);//判斷插入位置是否正確與節(jié)點(diǎn)是否正常if (ret){for (i = 1; i <= sList->capacity; i++){ //從第二個(gè)下標(biāo)開始找到第一個(gè)出現(xiàn)next為-1的位置下標(biāo)if (sList->node[i].next == AVAILABLE){ //找一個(gè)空的節(jié)點(diǎn)的下標(biāo)index = i;break;}}sList->node[index].data = (unsigned int)node; //將新插入的元素地址轉(zhuǎn)換存到該下標(biāo)節(jié)點(diǎn)的datasList->node[0] = sList->header; //將表的頭節(jié)點(diǎn)賦給數(shù)組首元素//根據(jù)next找到要插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)//如果sList->node[current].next == 0 表示是最后一個(gè)節(jié)點(diǎn)for (i = 0; (i < pos) && (sList->node[current].next != 0); i++){current = sList->node[current].next;}sList->node[index].next = sList->node[current].next; //新插入的節(jié)點(diǎn)的next等于該節(jié)點(diǎn)前一個(gè)節(jié)點(diǎn)的nextsList->node[current].next = index;// 新插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)的next等于新插入節(jié)點(diǎn)在數(shù)組中的下標(biāo)sList->node[0].data++; //長(zhǎng)度增加sList->header = sList->node[0];//將數(shù)組首元素賦給header(修改后的數(shù)據(jù))}return ret; }StaticListNode* StaticList_Get(StaticList * list, int pos){ //定義靜態(tài)鏈表獲取節(jié)點(diǎn)數(shù)據(jù)函數(shù)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)){ //判斷表是否空,下標(biāo)是否在范圍之內(nèi)sList->node[0] = sList->header;for (i = 0; i < pos; i++){ //找到要獲取的節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)的下標(biāo)current = sList->node[current].next;}object = sList->node[current].next; //取得要獲取的節(jié)點(diǎn)的下標(biāo)ret = (StaticListNode*)(sList->node[object].data); //取得節(jié)點(diǎn)存放的數(shù)據(jù)轉(zhuǎn)換成指針類型}return ret; //返回該指向的地址 }StaticListNode * StaticList_Delete(StaticList* list, int pos){//定義靜態(tài)鏈表刪除節(jié)點(diǎn)數(shù)據(jù)函數(shù)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)){//判斷是否在范圍內(nèi)sList->node[0] = sList->header;for (i = 0; i < pos; i++){//找到要?jiǎng)h除的節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)的下標(biāo)current = sList->node[current].next;}object = sList->node[current].next;//獲取要?jiǎng)h除的節(jié)點(diǎn)的下標(biāo)sList->node[current].next = sList->node[object].next; //sList->node[0].data--;//長(zhǎng)度減少sList->header = sList->node[0];//將數(shù)組首元素賦給header(修改后的數(shù)據(jù))sList->node[object].next = AVAILABLE;//將該節(jié)點(diǎn)設(shè)為空閑的ret = (StaticListNode*)(sList->node[object].data);//取得刪除節(jié)點(diǎn)存放的數(shù)據(jù)轉(zhuǎn)換成指針類型}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; }分析:
匯編:
總結(jié)
以上是生活随笔為你收集整理的静态链表(代码、分析、汇编)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c1货运资格证多少钱啊?
- 下一篇: 循环链表(代码、分析、汇编)