#include <QtCore/QCoreApplication>#include <iostream>template <class Data> class List{ class Node{ public: Data d; Node *next, *prev; Node(Data dat = 0){d = dat; next = 0; prev = 0;} }; Node *pbeg, *pend; public: List(){pbeg = 0; pend = 0;} ~List(); void add(Data d); Node * find(Data i); Node * insert(Data key, Data d); bool remove(Data key); void print(); void print_back();};//-------------------------------------template <class Data>List <Data>::~List(){if (pbeg !=0){Node *pv = pbeg;while (pv){pv = pv->next; delete pbeg;pbeg = pv;} }}//-------------------------------------template <class Data>void List <Data>::print(){Node *pv = pbeg;std::cout << std::endl << "list: ";while (pv){std::cout << pv->d << ' ';pv = pv->next;}std::cout << std::endl;}//-------------------------------------template <class Data>void List <Data>::print_back(){Node *pv = pend;std::cout << std::endl << " list back: ";while (pv){std::cout << pv->d << ' ';pv = pv->prev;}std::cout << std::endl;}//-------------------------------------template <class Data>void List <Data>::add(Data d){Node *pv = new Node(d);if (pbeg == 0)pbeg = pend = pv;else{pv->prev = pend;pend->next = pv;pend = pv;}}//-------------------------------------template <class Data>Node * List <Data>::find(Data d){Node *pv = pbeg;while (pv){if(pv->d == d)break;pv = pv->next;}return pv;}//------------------------------------template <class Data>Node * List<Data>::insert(Data key, Data d){if(Node *pkey = find(key)){Node *pv = new Node(d);pv->next = pkey->next;pv->prev = pkey;pkey->next =* pv;if (pkey != pend)(pv->next)->prev = pv;else pend = pv;return pv;}return 0;}template <class Data>bool List <Data>::remove(Data key){ if(Node *pkey = find(key)){ if (pkey == pbeg){ pbeg = pbeg->next; pbeg->prev = 0;} else if(pkey == pend){ pend = pend->prev; pend->next = 0;} else { (pkey->prev)->next = pkey->next; (pkey->next)->prev = pkey->prev;} delete pkey; return true;} return false;}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); return a.exec();}
C++ (Qt)template <class Data>Node * List <Data>::find(Data d)
C++ (Qt)template <class Data>typename List<Data>::Node * List <Data>::find(Data d)
typedef List<class Data> lst;
lst lst1; for(int i = 0; i < 8; ++i) lst1::add(i);
lst lst1; for(int i = 0; i < 8; ++i) lst1.add(i);
C++ (Qt)typedef List<int> lst;