C語言之線性表

2021-09-24 11:58:56 字數 1674 閱讀 9808

**測試

線性表,也叫做順序表。類似鍊錶,但是裡面的資料儲存空間是連續的,不像鍊錶是片段的,不連續。

順序儲存,資料連續的儲存,中間不能有空值。

如果需要插入資料,在插入的位置之後的資料需要乙個乙個的往後移動,移動需要遵循把最後乙個資料後移,

再後移最後乙個資料的前乙個資料,插入位置之後的資料依次後移即可,然後在需要的位置插入需要的資料。

如果需要刪除資料,在刪除的位置之後的資料需要乙個乙個的往前移動,移動需要遵循在刪除位置的後乙個資料前移,再前移刪除位置的後乙個資料的後乙個資料,後面的資料依次前移即可,直到前移到最後乙個資料完成。

tips:線性表中資料元素之間的關係是一對一的關係,

即除了第乙個和最後乙個資料元素之外,其它資料元素都是首尾相接的

注意,這句話只適用大部分線性表,而不是全部。

typedef struct orderlist                              //線性表的結構體

olist, *polist;

//初始化線性表

void initorderlist(polist arr, int length) //傳入資料為線性表polist arr,需要的資料長度 int length

經過以上步驟我們就能得到乙個線性表。

//在順序表的任何乙個地方插入資料

//olist arr 順序表指標 pos是需要插入的位置 data是需要插入的陣列

void insertpos(polist arr, int pos, int data)

夠else 不夠

//2. 插入

if (0 >= pos) 前插

後插else if(arr->length <= pos)

arr->pbase[arr->length] = data;

else 中間插入,當前索引位置和後面的後移,把當前索引位置空出來

//3.修正資料關係

arr->length++;

}

//在順序表中任意位置刪除要給節點

//olist arr 順序表指標 pos是需要刪除的位置

void delete_pos(polist arr, int pos)

else if (pos >= (arr->length - 1)) {} 後刪

else 中間刪除

//3.修正資料關係

arr->length--;

}

int getsize(polist arr)

olist clearlist(olist &l)

銷毀順序線性表l

void destroylist(olist &l)

判斷是否為空表

bool listempty(olist l)

int main()

C語言線性表

include include include 定義乙個linearlist結構體 typedef struct linearlist linearlist 初始化線性表 param 無 return linearlist linearlist initlinearlist return ptr 插...

C語言 線性表

include include include define list size 100 define list increment 10 typedef int datatype typedef structseqlist initlist l 初始條件 無 操作結果 構造乙個空的線性表。成功返回...

資料結構C語言之線性表簡單實現

include stdafx.h include include include define list size 100 定義線性表結構 typedef structsqlist int tmain int argc,tchar argv void initlist sqlist l l.leng...