資料結構 單向鍊錶

2021-09-24 21:16:43 字數 2049 閱讀 8027

#include

using

namespace std;

template

<

class

t>

class

node

node

(const t& element)

:element

(element)

node

(const t& element, node

* next)

:element

(element)};

template

<

class

t>

class

link

~link()

}int

size()

bool

empty()

node

*gethead()

void

insert

(int

,const t&);

void

erase

(int);

intfind

(const t&);

t&get

(int);

void

output()

}};template

<

class

t>

void link

::insert

(int index,

const t& element)

// index 代表索引 從0開始

// index >=size時 元素預設放在最後面

node

* current=

null;if

(index==0)

current = head;

while

(current-

>next&&

--index)

node

* p =

new node

(element, current-

>next)

; current-

>next = p;

length++;}

template

<

class

t>

void link

::erase

(int index)

if(index>=length)

return

; node

* current=head;

while

(current&&

--index)

node

* p =current-

>next;

current-

>next = p-

>next;

delete p;

length--;}

template

<

class

t>

int link

::find

(const t& element)

return-1

;}template

<

class

t>

t& link

::get

(int index)

node

* current = head;

while

(current&&index--

)return current-

>element;

}int

main()

鍊錶元素:432

1 鍊錶大小:4

鍊錶元素:435

216 鍊錶大小:6

刪除索引0後,鍊錶元素:352

16 刪除索引10後,鍊錶元素:352

16 刪除索引2後,鍊錶元素:351

6 鍊錶大小:4

鍊錶索引為1:5

資料結構 單向鍊錶

鍊錶結構的資料格式儲存 include stdafx.h 把這行放在最開始。include includeusing namespace std typedef struct data typedef struct node 這裡與上面的不同是多了node,注意如果沒有這個node,下面的struc...

資料結構(單向鍊錶)

ifndef linklist h define linklist h 鍊錶節點 template class linklistdata linklistdata linklistdata 獲取資料 t getdata public t data 資料 template class linklist...

資料結構 單向鍊錶

錯誤 h next null 或其他 讓指標指向空 的語句執行報錯 null undeclared identifier cannot convert from int to struct node 原因 未引入標頭檔案 include stdio.h 把null定義為乙個空位址是在 stdio標頭...