通用單向鍊錶的實現(附使用例子)

2021-08-30 08:58:11 字數 3129 閱讀 8837

當我開啟csdn,發現我有第乙個粉絲了,激動得差點大喊「hello world」,整晚都很開心,哈哈,為了保住我唯一的粉絲,我決定,以後勤快一點。今天就先出一篇通用鍊錶實現的部落格,只包含了乙個標頭檔案(.h)和乙個原始檔(.c),可以很方便地新增到你們的專案中。

list.h:

#ifndef __list_h__

#define __list_h__

#define list_no_mem -2

#define list_error -1

#define list_success 0

typedef struct _node node_t;

/*node*/

struct _node;

/*list*/

typedef struct _listlist_t;

/*initialise the list,the list shouldn't be null*/

int list_init(list_t *list);

/*get the size of a list of element*/

int list_size(const list_t *list);

/*add a element to list*/

int list_add(list_t *list,void *element,int pos);

/*get an element from a list*/

void *list_get(const list_t *list,int pos);

/*remove an element from a list*/

int list_remove(list_t *list,int pos);

#endif

list.c

#include "list.h"

#include #include #include int list_init(list_t *list)

int list_size(const list_t *list)

/*index start from 0*/

int list_add(list_t *list,void *element,int pos)

if(pos < 0 || pos >= list->nb_elt)

pos = list->nb_elt;

node = list->node;

if(pos == 0)

while(i + 1 < pos)

if(pos == list->nb_elt)

new_node->next = node->next;

node->next->next = new_node;

list->nb_elt++;

return list->nb_elt;

}void *list_get(const list_t *list,int pos)

return node->element;

}int list_remove(list_t *list,int pos)

while(pos > i + 1)

del_node = node->next;

node->next = node->next->next;

list->nb_elt--;

free(del_node);

return list_success;

}

#include #include #include "list.h"

int main()

{ int size = 0;

int i = 0;

list_t list;

char *elt;

list_init(&list);

char arr1[5] = "hello";

char arr2[5] = "world";

char arr3[32] = "i'm pumpkin monkey";

list_add(&list,(void*)arr1,-1);

list_add(&list,(void*)arr2,-1);

list_add(&list,(void*)arr3,-1);

printf("list size:%d\n",list_size(&list));

list_remove(&list,1);

size = list_size(&list);

printf("list size:%d\n",size);

for(i = 0;i以上就是最基本的通用單向鍊錶的實現,包含了建立、新增、刪除、長度、獲取等最常用的基本功能。還可以包含轉殖,下個元素、等其他功能,可根據需要自己新增

實際上鍊表並不複雜,但是很多人還是沒法寫好,主要還是特殊情況(邊界條件)考慮得不夠充分。不僅是在鍊錶,在任何乙個函式的實現裡,都必須要考慮特殊場景。對於鍊錶來說,其實需要注意的也就那幾個點,目標節點(pos)是否存在、鍊錶長度為0和pos剛好等於鍊錶長度、輸入鍊錶是否有效、迴圈條件。

資料結構中,在單鏈表的開始結點之前附設乙個型別相同的結點,稱之為頭結點。頭結點的資料域可以不儲存任何資訊,頭結點的指標域儲存指向開始結點的指標(即第乙個元素結點的儲存位置)。

頭結點作用:

1、防止單鏈表是空的而設的.當鍊表為空的時候,帶頭結點的頭指標就指向頭結點.如果當鍊表為空的時候,單鏈表沒有帶頭結點,那麼它的頭指標就為null.

2、是為了方便單鏈表的特殊操作,插入在表頭或者刪除第乙個結點.這樣就保持了單鏈表操作的統一性!

3、單鏈表加上頭結點之後,無論單鏈表是否為空,頭指標始終指向頭結點,因此空表和非空表的處理也統一了,方便了單鏈表的操作,也減少了程式的複雜性和出現bug的機會。

4、對單鏈表的多數操作應明確對哪個結點以及該結點的前驅。不帶頭結點的鍊錶對首元結點、中間結點分別處理等;而帶頭結點的鍊錶因為有頭結點,首元結點、中間結點的操作相同 ,從而減少分支,使演算法變得簡單 ,流程清晰。對單鏈表進行插入、刪除操作時,如果在首元結點之前插入或刪除的是首元結點,不帶頭結點的單鏈表需改變頭指標的值,在c 演算法的函式形參表中頭指標一般使用指標的指標(在c+ +中使用引用 &);而帶頭結點的單鏈表不需改變頭指標的值,函式引數表中頭結點使用指標變數即可。

實現單向鍊錶

鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...

實現單向鍊錶

鍊錶類 public class link 增加節點 public void add node node else 輸出節點 public void print else 內部搜尋節點的方法 public boolean search string data 直到不存在下乙個節點結束搜尋 if th...

單向鍊錶的實現

c語言實現單向鍊錶是利用結構體作為節點,結構體指標傳遞位址 include include include typedef struct node node typedef struct node lnode 定義結構體資料型別lnode typedef struct node linklist 定...