C 實現單鏈表

2021-10-04 15:25:26 字數 2908 閱讀 5871

1. 尾插法建立鍊錶

2. 頭插法建立鍊錶

3. 從頭至尾列印鍊錶

4. 查詢指定位置資料

5. 查詢指定資料是否存在

6. 在指定位置插入指定值

7. 刪除鍊錶指定位置的節點

8. 獲得鍊錶當前長度

整體**:

```cpp

#include

using

namespace std;

struct node

;class

linklist

~linklist()

//析構函式 ,物件消亡時,自動被呼叫,用來釋放物件占用的空間

bool

initlinklist_tool

(int size)

;//尾插法初始化鍊錶

bool

initlinklist_head

(int size)

;//頭插法初始化鍊錶

bool

printlinklist()

;//列印鍊錶

bool

searchlinklist_place

(int place)

;//查詢鍊錶指定位置資料

bool

searchlinklist_value

(int value)

;//查詢指定資料是否存在

bool

insertlinklist

(int place,

int value)

;//在指定位置place插入資料域為指定值value的節點

bool

deletelinklist

(int place)

;//刪除鍊錶指定位置的節點

intgetlength()

;//獲得鍊錶長度 };

bool linklist::

initlinklist_tool

(int n)

node *ptemp =

null

;//輔助指標變數

node *pnew =

null

;//輔助指標變數

this

->length = n;

//設定鍊錶長度為n

ptemp =

this

->head;

//取得煉表頭結點

for(

int i=

0;i) cout <<

"建立完成"

<< endl;

return

true;}

bool linklist::

initlinklist_head

(int n)

node *ptemp=

null

; node *pnew=

null

;this

->length=n;

//設定鍊錶長度

for(

int i=

0;i++i)

cout <<

"建立完成"

<< endl;

return

true;}

bool linklist::

printlinklist()

cout<}bool linklist::

searchlinklist_place

(int n)

node *ptemp=

this

->head;

//取得節點

for(

int i=

0;i++i)

cout<>data

return

true;}

bool linklist::

searchlinklist_value

(int x)

}return flag;

}bool linklist::

insertlinklist

(int n,

int x)

node *ptemp=

this

->head;

//取得頭結點

for(

int i=

0;i++i)

node *pnew=

new node;

//建立新節點

pnew-

>data=x;

//新節點的資料域為x

pnew-

>next=ptemp-

>next;

//設定新節點的後繼為目標位置節點(也就是ptemp節點的後繼)

ptemp-

>next=pnew;

//設定新節點的前驅為目標節點的前驅(也就是ptemp)

this

->length++

;//鍊錶長度加一

}bool linklist::

deletelinklist

(int n)

node *ptemp=

this

->head;

//取得頭結點

for(

int i=

0;i++i)

ptemp-

>next=ptemp-

>next-

>next;

//目標節點的前驅節點指向目標節點的後繼,把目標節點刪除

this

->length--

;//鍊錶長度減一

}int linklist::

getlength()

intmain()

C 單鏈表實現

1 單向鍊錶 單向鍊錶 include include class cnode 節點類 class clist 鍊錶類 cnode movetrail cnode pnode 移動到尾節點 return ptmp void addnode cnode pnode 新增節點 else m nodesu...

c 實現單鏈表

include include using namespace std typedef int datatype struct linknode 建立乙個節點 class slist void swap slist s slist const slist s head null tail null ...

單鏈表(C實現)

ifndef list h define list h typedef struct node node typedef struct list list initlist int insertlist list l,void data,int size node findnodebykey lis...