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

C++实现双向链表

2016-06-05 18:37 351 查看
双向链表结构图:




节点结构:



代码实现:

[code=cpp;toolbar:false">/*DList.h*/

#pragma once

#include <iostream>
#include <cassert>
using namespace std;

typedef int DataType;
struct Node
{
Node(const DataType& x)
:_data(x)
,_next(NULL)
,_prev(NULL)
{}
DataType _data; //数据
Node* _next;    //后继
Node* _prev; //前驱
};

class DList
{
public:
DList()
:_head(NULL),_tail(NULL)
{}
~DList()
{
Node* cur = _head;
while(cur)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
}
public:
void Display(); //打印链表
void PushBack(const DataType& x); //尾插
void PushFront(const DataType& x); //头插
void PopBack(); //尾删
void PopFront(); //头删
int DListLength(); //求链表长度
Node* Find(const DataType& x); //找元素x位置
void Erase(Node* pos); //删除pos位置元素
void Remove(const DataType& x); //删除指定元素x
void RemoveAll(const DataType& x); //删除所有元素x
void InsertBack(Node* pos, const DataType& x); //在pos位置后面插入x
void InsertFront(Node* pos,const DataType& x); //在pos位置前面插入x
void Sort(); //排序(元素递增方式)
void Reverse(); //反转链表
private:
Node* _head; //头管理
Node* _tail; //尾管理
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: