您的位置:首页 > 编程语言 > C语言/C++

双向链表(c++实现)

2016-03-05 10:24 417 查看
双向链表与单链表有许多的相似之处,但是也有不同之处。 双向链表与单链表主要的不同在于:双向链表可以从两个方向进行遍历,但是单链表只能从头节点遍历到尾节点,不能从尾节点遍历到头节点,对于链表中一些插入和删除等操作,双向链表较单链表更为简单些。所以,双向链表有其存在的特殊意义。

——下面是通过C++来实现双向链表的基本功能。
#include <assert.h>
//双向链表

typedef int DataType;
class ListNode    //节点结构
{
friend class List;
public:
ListNode(DataType x)    //构造函数
:Data(x)
, _next(NULL)
, _prev(NULL)
{ }

private:
DataType Data;
ListNode * _next;      //存放下一个节点的地址
ListNode * _prev;      //存放上一个节点的地址
};

class List
{
public:
List()     //无参构造函数
:_head(NULL)
, _tail(NULL)
{ }

~List()   //析构函数
{
Clear();
}

public:
void pushBack(DataType x)    //尾插
{
if (_head == NULL)
{
_head = _tail = new ListNode(x);
}
else
{
ListNode * tmp = new ListNode(x);
_tail->_next = tmp;
tmp->_prev = _tail;
tmp->_next = NULL;
_tail = tmp;
}
}

void popBack()      //尾删
{
//考虑无节点、一个节点、多个节点的情况
if (_head == _tail)
{
if (_head)
{
delete _head;
_head = _tail = NULL;
}
}
else
{
ListNode * cur = _tail->_prev;
delete _tail;
_tail = cur;
_tail->_next = NULL;
}
}

void pushFront(DataType x)    //头插
{
ListNode * tmp = new ListNode(x);
_head->_prev = tmp;
tmp->_next = _head;
tmp->_prev = NULL;
_head = tmp;
}

void popFront()     //头删
{
if (_head)
{
ListNode * tmp = _head;
_head = tmp->_next;
delete tmp;
}
}

void Insert(ListNode * pos, DataType x)     //在pos位置插入节点
{
//考虑pos的取值范围
assert(pos);
if (pos == _tail)
{
pushBack(x);
}
else
{
ListNode * cur = pos;
ListNode * next = pos->_next;
ListNode * tmp = new ListNode(x);
cur->_next = tmp;
tmp->_prev = cur;
tmp->_next = next;
next->_prev = tmp;
}
}

ListNode * Find(DataType x)     //查找
{
ListNode * cur = _head;
while (cur)
{
if (cur->Data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}

void Erase(ListNode * pos)     //删除pos位置上的数据
{
if (pos == _head)
{
popFront();
}
else if (pos == _tail)
{
popBack();
}
else
{
ListNode * tmp = pos;
ListNode * cur = pos->_prev;
ListNode * next = pos->_next;
cur->_next = next;
next->_prev = cur;

/*tmp->_prev->_next = tmp->_next;
tmp->_next->_prev = tmp->_prev;*/
delete tmp;
}
}

void Reverse()       //逆置双向链表
{
ListNode * begin = _head;
ListNode * end = _tail;
while (begin != end && begin->_prev != end)
{
swap(begin->Data, end->Data);
begin = begin->_next;
end = end->_prev;
}

/*ListNode * cur = _head;
swap(_head, _tail);
while (cur)
{
swap(cur->_prev, cur->_next);
cur = cur->_prev;
}*/
}

void printList()     //格式输出
{
ListNode * cur = _head;
while (cur)
{
cout << cur->Data << " ";
cur = cur->_next;
}
cout << endl;
}

void Clear()   //释放所有节点
{
ListNode * cur = _head;
while (cur)
{
ListNode * del = cur;
cur = cur->_next;
delete del;
}
}

private:
ListNode * _head;
ListNode * _tail;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息