|
发表于 2016-6-22 14:30:42
|
显示全部楼层
我自己写了一个- #define TYLIST(T) ListNode<T>*
- //template<typename T>
- //struct ListNode{
- // //数据成员
- // struct ListNode<T> *pre, *next;//双向列表
- // T info;
- // //构造函数
- // ListNode(){};
- // ListNode(T e, TYLIST(T) p1, TYLIST(T) p2) :info(e), pre(p1), next(p2){};//在函数形参之后大括号之前:是初始化操作
- // //操作接口
- // TYLIST(T) Insertpre(T const&el);//在当前节点之后插入节点
- // TYLIST(T) Insertnext(T const&el);//插入操作,在当前节点之后插入节点
- //};//节点类
- //template<typename T>
- //TYLIST(T) ListNode<T>::Insertpre(T const &el)
- //{
- // TYLIST(T) N = new ListNode(el,pre,this);
- // pre->next = N;
- // pre = N;//即,在this和this->pre之间插入N,建立相互之间的链接
- // return N;
- //}
- //template<typename T>
- //TYLIST(T) ListNode<T>::Insertnext(T const &el)
- //{
- // TYLIST(T) N= new ListNode(el,this,next);
- // next->pre = N;
- // next = N;
- // return N;
- //}
- template<class T>
- class ListNode {
- public:
- ListNode<T> *pre, *next;
- T info;
- //构造函数
- ListNode(){};
- ListNode(T e, TYLIST(T) p1, TYLIST(T) p2) :info(e), pre(p1), next(p2){};//在函数形参之后大括号之前:是初始化操作
- //操作接口
- TYLIST(T) Insertpre(T const&el);//在当前节点之后插入节点
- TYLIST(T) Insertnext(T const&el);//插入操作,在当前节点之后插入节点
- };
- template<typename T>
- TYLIST(T) ListNode<T>::Insertpre(T const &el)
- {
- TYLIST(T) N = new ListNode(el,pre,this);
- pre->next = N;
- pre = N;//即,在this和this->pre之间插入N,建立相互之间的链接
- return N;
- }
- template<typename T>
- TYLIST(T) ListNode<T>::Insertnext(T const &el)
- {
- TYLIST(T) N= new ListNode(el,this,next);
- next->pre = N;
- next = N;
- return N;
- }
复制代码
是可以的,注释掉的是struct,是可以用的。我也是菜鸟,相互学习 |
|