C 模板類實現LinkList

2021-10-02 02:14:56 字數 1569 閱讀 6746

linklist.**件

#pragma once

#include //在插入元素時,應該把每乙個元素都儲存下來

//模板類中應該能分配節點,快取節點

template struct node

;template class linklist

;

linklist.cpp檔案

#include "linklist.h"

template linklist::linklist(void)

template linklist::~linklist(void)

len = 0;

head = null;

}template int linklist::clear()

head = new node;//建立乙個帶有頭結點的鍊錶

head->next = null;

len = 0;

return 0;

}template int linklist::getlen()

template int linklist::insert(t &t,int pos)

node* node = new node;

if (node == null)

node->t = t;

node->next = current->next;

current->next = node;

len++;

return 0;

}template int linklist::get(int pos,t &t)

t = current->next->t;

return 0;

}template int linklist::del(int pos,t &t)

node = current->next;

t = node->t;

current->next = node->next;

len--;

delete node;//插入時new node需釋放

return 0;

}

main.cpp

#include "linklist.cpp"

using namespace std;

struct teacher

;int main()

cout << endl;

while(list.getlen() > 0)

cout << endl;

list.clear();

list.insert(t1,0);

list.insert(t2,0);

list.insert(t3,0);

while(list.getlen() > 0)

cout << endl;

system("pause");

return 0;

}

輸出結果:

C 實現模板陣列類

利用模板陣列類,可以實現類似於容器的功能 myvector.h pragma once include using namespace std template typename t class myvector myvector.cpp pragma once include myvector.h...

LinkList單鏈表實現

1.封裝node實體類 package data table.syn.com.common.util 簡單封裝node節點 author administrator public class node public void setvalue object value public node get...

LinkList的具體實現

include includeusing namespace std define list init size 100 define listincrement 10 define true 1 define false 0 define ok 1 define error 0 define in...