資料結構與演算法 C語言 單向鍊錶1

2021-10-22 04:11:49 字數 3206 閱讀 4652

將儲存資料元素資訊的域稱為資料域,將儲存直接後繼位置的域稱為指標域。指標域中儲存的資訊稱做指標或鏈。這兩部分資訊組成資料元素稱為結點。

1、頭指標是指向鍊錶第乙個結點的指標。注,並不一定是頭結點的指標。

無論鍊錶是否為空,頭指標均不為空。

2、頭結點,為了操作的統一和方便而設立的,放在第乙個元素結點之前,其資料域一般無意義。有了頭結點,對在第一元素結點前插入和刪除第乙個結點,其操作與其他結點操作就統一了。

#ifndef __linkedlist_h

#define __linkedlist_h

#include

#include

#include

#define ok 1

#define error 0

typedef

int states;

/** 結點資料 */

typedef

struct

elementtype;

/** 線性表的單鏈表儲存結構 */

typedef

struct linkednodelinkednode;

/** 線性表的單鏈表表頭結構 */

typedef

struct linkedlistlinkedlist;

/** 鍊錶初始化 */

states initlinkedlist

(linkedlist *ptrl)

;/** 向鍊錶中插入結點 (按照位置下標插入)*/

states insertlinkedlist

(linkedlist *ptrl,

int pos, elementtype data)

;/** 從鍊錶中刪除結點 */

states deletelinkedlist

(linkedlist *ptrl,

int pos, elementtype *data)

;/** 單鏈表的讀取 */

states getlinkedlistelem

(linkedlist *ptrl,

int pos, elementtype *data)

;/** 獲取鍊錶中結點個數 */

intgetlinkedlistlength

(linkedlist *ptrl)

;/** 鍊錶的整表建立操作 --頭插法 */

void

createlinkedlist_h

(linkedlist *ptrl)

;/** 鍊錶的整表建立操作 --尾插法 */

void

createlinkedlist_t

(linkedlist *ptrl)

;/** 鍊錶的整表刪除操作 */

states clearlinkedlist

(linkedlist *ptrl)

;/** 鍊錶節點的列印 */

void

printflinkedlist

(linkedlist *ptrl)

;#endif

/* __linkedlist_h */

#include

"linkedlist.h"

/** 鍊錶初始化 */

states initlinkedlist

(linkedlist *ptrl)

/** 向鍊錶中插入結點 (按照位置下標插入)*/

states insertlinkedlist

(linkedlist *ptrl,

int pos, elementtype data)

//尋找位置為 pos 的結點指標

tempnode = ptrl->headptr;

for(i =

1;i < pos -

1;i++

) node->next = tempnode->next;

tempnode->next = node;

ptrl->length++

;return ok;

}/** 從鍊錶中刪除結點 */

states deletelinkedlist

(linkedlist *ptrl,

int pos, elementtype *data)

tempnode = ptrl->headptr;

for(i =

1;i < pos -

1;i++

) node = tempnode->next;

tempnode->next = node->next;

//鏈結單鏈表

*data = node->data;

free

(node)

; ptrl->length--

;return ok;

}/** 單鏈表的讀取 */

states getlinkedlistelem

(linkedlist *ptrl,

int pos, elementtype *data)

tempnode = ptrl->headptr;

for(i =

1;i < pos -

1;i++

) node = tempnode->next;

*data = node->data;

return ok;

}/** 獲取鍊錶中結點個數 */

intgetlinkedlistlength

(linkedlist *ptrl)

/** 鍊錶的整表建立操作 --頭插法 */

void

createlinkedlist_h

(linkedlist *ptrl)

/** 鍊錶的整表建立操作 --尾插法 */

void

createlinkedlist_t

(linkedlist *ptrl)

else}}

/** 鍊錶的整表刪除操作 */

states clearlinkedlist

(linkedlist *ptrl)

ptrl->headptr =

null

;return ok;

}/** 鍊錶節點的列印 */

void

printflinkedlist

(linkedlist *ptrl)

}

資料結構(C語言) 單向鍊錶

c語言的單向鍊錶,就是在乙個將一些資料放在乙個結構體裡,然後在結構體裡加 struct next 的成員,用於指向下一結點。引用時,建立乙個臨時的結構體變數進行引用。如原結構體變數為 struct p 則 可建立 struct temp,然後 for temp p temp next null te...

資料結構 單向鍊錶 C語言

chainlist.h include include typedef struct nodechainlisttype chainlisttype chainlistaddend chainlisttype head,data data 新增節點到鍊錶末尾 chainlisttype chainl...

C 資料結構與演算法之單向鍊錶

如果乙個節點將指向另乙個節點的指標作為資料成員,那麼多個這樣的節點可以連線起來,只用乙個變數就能夠訪問整個節點序列。這樣的節點序列就是最常用的鍊錶實現方法。鍊錶是一種由節點組成的資料結構,每個節點都包含某些資訊以及指向鍊錶中另乙個節點的指標。如果序列中的節點只包含指向後繼節點的鏈結,該鍊錶就成為單向...