資料結構 鍊錶的c 實現

2021-10-05 17:28:40 字數 2758 閱讀 4962

#ifndef my_head_h

#define my_head_h

#include

"g://code/c++/myhead.h"

#endif

// !my_head_h

template

<

typename elemtype>

class

linklist

;typedef list_node *nodepointer;

//指向結點的指標型別說明

//鍊錶置空

void

clear()

;//求鍊錶結點個數

intgetlength()

;//取第i個結點

status getitem

(int i, elemtype& e)

;//取第乙個結點的指標

nodepointer gethead()

//刪除第i個結點

status deleteitem

(int i)

;//在第i個結點之前插入乙個資料為e的結點

status insert

(int i, elemtype e)

;//列印出每乙個結點的值

void

display_all()

;//以隨機數填充num個結點

status random_fill

(int num)

;linklist()

;~linklist()

;protected

: nodepointer head;};

template

<

typename elemtype>

void linklist

::clear()

head =

null;}

template

<

typename elemtype>

int linklist

::getlength()

return num;

}template

<

typename elemtype>

status linklist

::getitem

(int i, elemtype& e)

//若q為空或者i超過列表結點個數,返回errorif(

!q || count > i)

return error;

e = q-

>data;

return ok;

}/*templatelinklist::nodepointer linklist::gethead()

*/template

<

typename elemtype>

status linklist

::deleteitem

(int i)

//若q為空或者i超過列表結點個數,返回errorif(

!q || count > i)

return error;

return ok;

p->next = q-

>next;

delete p;

}template

<

typename elemtype>

status linklist

::insert

(int i, elemtype e)

//若q為空或者i超過列表結點個數,返回errorif(

!q || count > i)

return error;

s =new list_node;

s->data = e;

s->next = q-

>next;

q->next = s;

}template

<

typename elemtype>

inline

void linklist

::display_all()

cout <<

"end"

<< endl;

}template

<

typename elemtype>

status linklist

::random_fill

(int num)

for(

int i =

0; i < num; i++

) q-

>next =

null

;return ok;

}template

<

typename elemtype>

linklist

::linklist()

template

<

typename elemtype>

linklist ::~

linklist()

其中myhead.h是乙個儲存基本配置的標頭檔案。

#include

#include

#include

#include

using

namespace std;

typedef

int status;

#define error 0;

#define ok 1;

#define random(x) (rand()%x)

c 資料結構鍊錶的實現

資料結構中最開始學習實現的就是鍊錶 1 這個標頭檔案 list head 為建立結構體,用來儲存資料 include using namespace std template struct node 建立結構體 template node node template node node type i...

資料結構 鍊錶的實現 C

昨天寫了鍊錶,目前只寫了單鏈表,等有時間把迴圈鍊錶什麼的變異產品再寫出來 只有頭指標 沒有頭結點 的單鏈表 pragma once template struct node template class singlelinkedlist include singlelinkedlist.h temp...

資料結構 C語言鍊錶實現

資料結構 c語言鍊錶實現 目錄 靜態單鏈表實現 動態單鏈表實現 雙向鍊錶實現 迴圈單鏈表 我學資料結構的時候也是感覺很困難,當我學完後我發現了之所以困難時因為我沒有系統的進行學習,而且很多教授都只是注重資料結構思想,而忽略了 方面,為此我寫了這些博文給那些試圖自學資料結構的朋友,希望你們少走彎路 我...