#ifndef _LISTEX_H_
#define _LISTEX_H_
/***********************************
@function: 链表模板
@param:
***********************************/
#include <iostream>
using namespace std;
template <typename T>
class CList
{
private:
struct SNode
{
T data;
SNode *pPre;
SNode *pNext;
};
SNode *m_pHead, *m_pTail;
public:
/*
@function: 构造函数
@param:
*/
CList()
{
m_pHead = new SNode();
m_pTail = new SNode();
m_pHead->pNext = m_pTail;
m_pTail->pPre = m_pHead;
}
/*
@function: 析构函数
@param:
*/
~CList()
{
SNode *p = m_pHead, *q;
while (p != m_pTail)
{
q = p;
p = p->pNext;
delete q;
q = NULL;
}
delete p;
p = NULL;
}
/**********************************
@function:遍历链表
@param:
**********************************/
void Print() const
{
SNode *p = m_pHead->pNext;
while (p != m_pTail)
{
cout << p->data << endl;
p = p->pNext;
}
}
/***********************************
@function: 向尾部添加数据
@param: data--要添加的数据
***********************************/
void AddTail(T data)
{
SNode *q = m_pTail->pPre, *p = new SNode();
p->data = data;
p->pPre = q;
p->pNext = m_pTail;
m_pTail->pPre = p;
q->pNext = p;
}
/**
C++泛型编程之双向链表
最新推荐文章于 2021-11-13 17:34:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)