靜態鍊錶的實現

2021-09-27 07:06:00 字數 2173 閱讀 7773

/**

*2018-8-23 21:35

*靜態鍊錶的實現

*陣列中第乙個結點表示備用鍊錶的第乙個結點

*陣列中最後乙個結點表示鍊錶的第乙個結點

*當next域為0時均代表到了當前鍊錶的結尾,因此有兩個陣列單元(0與max-1)無法儲存目標資料**/

#include

#define max 10

//此時靜態鍊錶中只能儲存8個元素

typedef

struct

elem;

typedef

struct

staticlinked;

staticlinked create()

;void

insert

(staticlinked *linked,

char e)

;void

delete

(staticlinked *linked,

char e)

;int

location

(staticlinked *linked,

char e)

;int

main()

putchar

('\n');

for(size_t i = linkedlist.data[max -1]

.next; i !=

0; i = linkedlist.data[i]

.next)

putchar

('\n');

printf

("所查詢元素的位置:%d\n"

,location

(&linkedlist,

'2'));

system

("pause");

return0;

}staticlinked create()

,0};

linked.data[max -1]

.next =0;

linked.data[max -2]

.next =0;

for(size_t i =

0; i < max -

2; i++

) linked.data[i]

.next = i+1;

return linked;

}void

insert

(staticlinked *linked,

char e)

//獲取備用鍊錶的第乙個結點,為0表示沒有

int emptylocation = linked->data[0]

.next;

//獲取當前空結點的下乙個空結點

linked->data[0]

.next = linked->data[emptylocation]

.next;

//插入指定資料

linked->data[emptylocation]

.e = e;

//將插入的結點的下乙個結點指標置為0,因為插入的結點一定是最後乙個結點

linked->data[emptylocation]

.next =0;

//將插入的結點連線到鍊錶的最後

linked->data[endpoint]

.next = emptylocation;

//判斷當前鍊錶是否為空鍊錶,如果是則將當前插入的結點作為第乙個結點不是則不變if(

!linked->data[max -1]

.next)linked->data[max -1]

.next = emptylocation;

//記錄鍊錶的數量

linked->length++;}

}void

delete

(staticlinked *linked,

char e)

//不相等則繼續查詢下乙個結點

previous = linked->data[previous]

.next;

nextpoint = linked->data[previous]

.next;}}

intlocation

(staticlinked *linked,

char e)

return0;

}

靜態鍊錶的實現

include include define size 6 define struct of static list typedef struct stlist stlist 初始化靜態鍊錶分配空間大小為size個 stlist initlist stlist tempptr of for i in...

靜態鍊錶的實現

用陣列來代替指標,來描述單鏈表 將陣列元素分成兩個資料域,data和cur。data用來存放資料元素,cur存放該元素的後繼在陣列中的下標 游標 游標實現法 線性表的靜態鍊錶儲存結構 define maxsize 1000 typedef struct component,staticlinklis...

靜態鍊錶實現

1 include stdio.h 2 struct nodelist 100 space 100 6 int listlen 0 7 void init 813 int free int p 14 18int malloc 1925 void add int p,int val 26 31int ...