資料結構系列 單鏈表的基本操作

2021-10-23 06:27:12 字數 1788 閱讀 7107

c語言描述鍊錶的實現及操作

線性表的基本操作及應用(單鏈表的建立、插入、刪除、查詢、顯示)

《大話資料結構–程杰》

線性表是一種重要的資料結構。

線性表的鏈式儲存結構就是單鏈表。

線性表的順序儲存結構就是陣列。

下面是c語言實現的單鏈表的**。

#include

#include

typedef

int elemtype;

// 單鏈表的宣告

typedef

struct _node_s

node,

*linklist;

// 1、建立:從尾部開始建立鍊錶(尾插法)

linklist createlinklisttail

(int n)

//ptail->next = null;

return l;

}// 2、建立:從頭部開始建立鍊錶(頭插法)

linklist createlinklisthead

(int n)

return l;

}// 3、查詢:查詢鍊錶中的第i個元素

intgeteleinlinklist

(linklist l,

int i, elemtype* e)if(

!p || j>i)

*e = p->data;

printf

("the %dth element: %d \n"

,i,*e)

;return0;

}// 4、插入:在鍊錶中第i個資料插入結點

intinsertnodeinlinklist

(linklist ll,

int i, elemtype* m)if(

!p || j// 查詢成功,建立空節點

newnode =

(node*

)malloc

(sizeof

(node));

if(null

== newnode)

newnode->data =

*m; newnode->next = p->next;

p->next = newnode;

return0;

}// 5、遍歷輸出:遍歷鍊錶中的每個元素並輸出

inttranvesallinklist

(linklist ll)

while

(p)printf

("\n");

return0;

}// 6、刪除:刪除鍊錶中的第i個結點,並取出該結點的值

intdeletenodeinlinklist

(linklist ll,

int i, elemtype *e)if(

!p || j>i)

q = p->next;

p->next = q->next;

*e = q->data;

printf

("the deleted node data: %d\n"

,*e)

;free

(q);

return0;

}// 7、整表刪除

intdeletedallnodeinlinklist

(linklist ll)

ll->next =

null

;return0;

}int

main

(int argc,

int ar**)

資料結構 單鏈表基本操作

實現單鏈表的初始化,頭插法建表,尾插法建表,查詢元素,插入元素,刪除元素等功能 include using namespace std define elemtype char typedef struct node node,linklist 初始化單鏈表 void initlist linkli...

資料結構 單鏈表的基本操作

最近正好在複習資料結構,鍊錶作為比較重要的資料結構,特地自己實現了一遍。首先我們要理解幾個概念 1 鏈式儲存是最常用的儲存方式之一,可以表示線性和非線性的資料結構。2 按照鏈式儲存的線性表簡稱為鍊錶。3 單鏈表是一種鏈式訪問的資料結構,用一組位址任意的儲存單元來存放線性表中的資料元素。4 鍊錶由節點...

《資料結構》單鏈表基本操作實現

define ok 1 define error 1 typedef int elemtype typedef int status typedef struct node lnode,linklist 構造空表 status initlist linklist l void creatlist l...