C 語言 鍊錶的建立與列印

2021-08-21 21:12:06 字數 882 閱讀 1504

/* 包含的標頭檔案 */

#include

#include

/* 定義乙個表示鍊錶的結構體指標 */

struct

list ;

/* 定義乙個鍊錶頭部 */

static

struct

list *list_head = null;

/* 為了保證每乙個鍊錶元素的id不同,特意把id定義成乙個全域性靜態變數 */

static

int list_id = 0;

/** 將指定元素插入到聊表尾部

* head : 表示要插入元素的鍊錶的頭部的位址

* list : 表示要插入到鍊錶中的元素

*/static

void list_add(struct

list **head, struct

list *list)

else

temp = temp->next;}}

}/** 遍歷乙個鍊錶,列印鍊錶中每個元素所包含的資料

* head : 表示要遍歷的鍊錶的頭部的指標

*/static

void list_print(struct

list **head)

}/* 主函式,程式的入口 */

int main(int argc, char *argv)

/* 將分配的10個元素依次填充資料並加入到鍊錶當中 */

for(i = 0; i < 10; i++)

/* 遍歷鍊錶,把鍊錶中每個元素的資訊都列印出來 */

list_print(&list_head);

return

0; }

C 語言 鍊錶的建立與列印

包含的標頭檔案 include include 定義乙個表示鍊錶的結構體指標 struct list 定義乙個鍊錶頭部 static struct list list head null 為了保證每乙個鍊錶元素的id不同,特意把id定義成乙個全域性靜態變數 static int list id 0 ...

C語言 鍊錶的建立,插入,刪除,列印

include include include 結構體定義 struct node typedef struct node listnode 函式宣告部分 listnode createlist int n void insertlist listnode h,int i,char name,int...

c 建立列印線性鍊錶ListNode

刷題的時候,測試演算法的時候需要經常自己建立乙個線性鍊錶的例子,這裡自己造一次輪子,防止以後再造輪子.include include using std vector using std cout 定義鍊錶 struct listnode listnode getlistnode vector in...