類模板與鍊錶

2021-08-18 18:00:58 字數 1371 閱讀 7105

#include

using namespace std;

templatestruct listnode//相當於把類或結構體當成type型別的

;template

class list

private:

listnode*first,*last;

int length;

};template//模板宣告

void list::empty()

length=0;

}template

bool list::insertnode(t t,int n)

//開始遍歷到目標節點i*q;

q=new listnode;

q->data=t;//?????

q->next=p->next;

p->next=q;

length++;

return true;}}

template

void list::deletenode(int n)

q=p->next;

cout<<"刪除資料項:"next;

length--;

delete q;

}template

bool list::print()

}cout<

int list::find(t t)

if(p)return i;

else return 0;

}template

inline list::list()

template

list::~list()

}#include

using namespace std;

int main()

{listintlist;

intlist.insertnode(2,1);

intlist.insertnode(5,2);

intlist.insertnode(100,3);

intlist.print();

cout<<"50在鍊錶中的位置是:"doublelist.insertnode(87.23,1);

doublelist.insertnode(76.89,2);

doublelist.insertnode(56.89,3);

doublelist.insertnode(82.98,4);

doublelist.insertnode(23.67,2);

cout<<"\n------doublelist------"《在這裡,主體是模板類,鍊錶相當於給類尋找空間並將其連線起來,所以在類的成員函式裡面可以看到都是定義結構體在裡面而且在本例中只對結構體中的data進行操作。

參考文獻 c++.net程式設計

鍊錶類模板

include using namespace std class cnode 定義乙個節點類 template 定義類模板 class clist 定義clist類 type movetrail 獲取尾節點 return ptmp 返回尾節點 void addnode type pnode 新增節...

類模板 》鍊錶,棧

除了構造和析構以外的所有用到模板名稱的地方都加上型別引數 類模板的選擇性例項化 用到哪個函式例項化哪個函式 include template 1 定義乙個型別 2 宣告型別 class clink templateclass node private t mdata node pnext templ...

C 鍊錶類模板

鍊錶 list 即雙向鍊錶容器,它不支援隨機訪問,訪問鍊錶元素要指標從鍊錶的某個斷點開始,插入和刪除操作所花費的時間是固定的,和該元素在鍊錶中的位置無關。list在任何位置插入和刪除動作都很快,不像vector只能在末尾操作。建立list鍊錶類模板的方法 include 呼叫list標頭檔案.lis...