資料結構之簡單線性表

2021-07-28 14:04:14 字數 2631 閱讀 4425

線性表是一種基本的資料結構。

線性表中資料元素之間的關係是一對一的關係,即除了第乙個和最後乙個資料元素之外,其它資料元素都是首尾相接的。這裡我們只討論順序儲存的線性表。在記憶體中一連續的一段空間來儲存。線性表可以實現在n個連續的空間(即線性

表長度為n)插入,刪除,查詢,清空等操作。詳細用法如下:

1.線性表的定義以及初始化

struct node//定義乙個結構體

;

void initlist(struct node *l)//初始化線性表

2.插入函式

void listinsert(struct node *l,int id,int e)//在指定位置插入某值

if(id<1)//插入位置小於1,插入線性表開頭

id=1;

int i;

for(i=l->len-1; i>=id-1; i--)//原線性表第id(此時結構體陣列下標為id-1)位置及其第id位置以後的數值全部向後移動一項

l->elem[id-1]=e;

l->len++;}}

3.輸出函式

void displaylist(struct node *l)//遍歷輸出線性表

printf("\n");

}

4 .刪除函式

void deletelist(struct node *l,int id)//刪除指定位置的元素的值

l->len--;//刪除一項,線性表長度減少1

}

5.銷毀或清空線性表

void destroylist(struct node *l)//銷毀線性表

void clearlist(struct node *l)//清空線性表

6.查詢指定位置的值的函式

int getlist(struct node *l,int id)//查詢指定位置的值

}

return error;

}8.判斷線性表是否為空

bool listempty(struct node *l)//判斷線性表是否為空

9.返回線性表長度

int listlength(struct node *l)//返回線性表長度

10.返回線性表某位置的前驅或後驅元素

int priorlist(struct node *l,int e)//返回線性表非頭先驅元素值}}

return error;

}int nextlist(struct node *l,int e)//返回線性表非尾後驅元素值

}return error;

}

完整**:

#include#include#include#include#define error -10000

#define length 100

struct node//定義乙個結構體(陣列)

;void initlist(struct node *l)//初始化線性表

void destroylist(struct node *l)//銷毀線性表

void clearlist(struct node *l)//清空線性表

int getlist(struct node *l,int id)//查詢指定位置的值

bool listempty(struct node *l)//判斷線性表是否為空

int listlength(struct node *l)//返回線性表長度

}return error;

}int priorlist(struct node *l,int e)//返回線性表非頭先驅元素值}}

return error;

}int nextlist(struct node *l,int e)//返回線性表非尾後驅元素值

}return error;

}void listinsert(struct node *l,int id,int e)//在指定位置插入某值

if(id<1)//插入位置小於1,插入線性表開頭

id=1;

int i;

for(i=l->len-1; i>=id-1; i--)//原線性表第id(此時結構體陣列下標為id-1)位置及其第id位置以後的數值全部向後移動一項

l->elem[id-1]=e;

l->len++;

}}void deletelist(struct node *l,int id)//刪除指定位置的元素的值

l->len--;//刪除一項,線性表長度減少1

}void displaylist(struct node *l)//遍歷輸出線性表

printf("\n");

}int main()

希望對大家有所幫助

mysql 線性表 資料結構之線性表

概要 參考 大話資料結構 把常用的基本資料結構梳理一下。線性表定義 線性表 list 零個或多個資料元素的有限序列。若將線性表記為 a 1,cdots,a a i,a cdots,a n 則表中 a 領先於 a i a i 領先於 a 稱 a 是 a i 的直接前驅元素,a 是 a i 的直接後繼元...

資料結構之線性表

從現在開始,我們開始討論如何實現一些常用的資料結構和其中的一些經典演算法.等把資料結構講完了.我可能會繼續討論vc 的程式設計只是以及vs平台下的c c 開發等等.呵呵.我們進入正題吧.我在這裡就只實現線性表的連表結構.當然了,這裡實際上包含了好多知識.我希望大家在引用的時候.一定要領悟裡面的一些變...

資料結構之線性表

線性表是具有相同特性的資料元素的乙個有限序列。該序列中所含元素的個數叫做線性表的長度,用n表示,n 0。當n 0時,表示線性表是乙個空表,即表中不包含任何元素。設序列中第i i表示位序 個元素為ai 1 i n 線性表的一般表示為 a1,a2,ai,ai 1,an include include d...