c++实现双向链表 :

 #ifndef DOUBLE_LINK_HXX
#define DOUBLE_LINK_HXX #include <iostream>
using namespace std; template<class T>
struct DNode
{
public:
T value;
DNode *prev;
DNode *next;
public:
DNode() { }
DNode(T t, DNode *prev, DNode *next) {
this->value = t;
this->prev = prev;
this->next = next;
}
}; template<class T>
class DoubleLink
{
public:
DoubleLink();
~DoubleLink(); int size();
int is_empty(); T get(int index);
T get_first();
T get_last(); int insert(int index, T t);
int insert_first(T t);
int append_last(T t); int del(int index);
int delete_first();
int delete_last(); private:
int count;
DNode<T> *phead;
private:
DNode<T> *get_node(int index);
}; template<class T>
DoubleLink<T>::DoubleLink() : count()
{
// 创建“表头”。注意:表头没有存储数据!
phead = new DNode<T>();
phead->prev = phead->next = phead;
// 设置链表计数为0
//count = 0;
} // 析构函数
template<class T>
DoubleLink<T>::~DoubleLink()
{
// 删除所有的节点
DNode<T>* ptmp;
DNode<T>* pnode = phead->next;
while (pnode != phead)
{
ptmp = pnode;
pnode=pnode->next;
delete ptmp;
} // 删除"表头"
delete phead;
phead = NULL;
} // 返回节点数目
template<class T>
int DoubleLink<T>::size()
{
return count;
} // 返回链表是否为空
template<class T>
int DoubleLink<T>::is_empty()
{
return count==;
} // 获取第index位置的节点
template<class T>
DNode<T>* DoubleLink<T>::get_node(int index)
{
// 判断参数有效性
if (index< || index>=count)
{
cout << "get node failed! the index in out of bound!" << endl;
return NULL;
} // 正向查找
if (index <= count/)
{
int i=;
DNode<T>* pindex = phead->next;
while (i++ < index) {
pindex = pindex->next;
} return pindex;
} // 反向查找
int j=;
int rindex = count - index -;
DNode<T>* prindex = phead->prev;
while (j++ < rindex) {
prindex = prindex->prev;
} return prindex;
} // 获取第index位置的节点的值
template<class T>
T DoubleLink<T>::get(int index)
{
return get_node(index)->value;
} // 获取第1个节点的值
template<class T>
T DoubleLink<T>::get_first()
{
return get_node()->value;
} // 获取最后一个节点的值
template<class T>
T DoubleLink<T>::get_last()
{
return get_node(count-)->value;
} // 将节点插入到第index位置之前
template<class T>
int DoubleLink<T>::insert(int index, T t)
{
if (index == )
return insert_first(t); DNode<T>* pindex = get_node(index);
DNode<T>* pnode = new DNode<T>(t, pindex->prev, pindex);
pindex->prev->next = pnode;
pindex->prev = pnode;
count++; return ;
} // 将节点插入第一个节点处。
template<class T>
int DoubleLink<T>::insert_first(T t)
{
DNode<T>* pnode = new DNode<T>(t, phead, phead->next);
phead->next->prev = pnode;
phead->next = pnode;
count++; return ;
} // 将节点追加到链表的末尾
template<class T>
int DoubleLink<T>::append_last(T t)
{
DNode<T>* pnode = new DNode<T>(t, phead->prev, phead);
phead->prev->next = pnode;
phead->prev = pnode;
count++; return ;
} // 删除index位置的节点
template<class T>
int DoubleLink<T>::del(int index)
{
DNode<T>* pindex = get_node(index);
pindex->next->prev = pindex->prev;
pindex->prev->next = pindex->next;
delete pindex;
count--; return ;
} // 删除第一个节点
template<class T>
int DoubleLink<T>::delete_first()
{
return del();
} // 删除最后一个节点
template<class T>
int DoubleLink<T>::delete_last()
{
return del(count-);
} #endif

本文来自http://www.cnblogs.com/skywang12345/p/3561803.html

双向链表的实现——c++的更多相关文章

  1. 学习Redis你必须了解的数据结构——双向链表(JavaScript实现)

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/ 下午分享了JavaScript实现单向链表,晚上就来补充下双向链表吧.对链表 ...

  2. 双向链表、双向循环链表的JS实现

    关于链表简介.单链表.单向循环链表.JS中的使用以及扩充方法:  单链表.循环链表的JS实现 关于四种链表的完整封装: https://github.com/zhuwq585/Data-Structu ...

  3. 剑指Offer面试题:25.二叉搜索树与双向链表

    一.题目:二叉搜索树与双向链表 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.比如输入下图中左边的二叉搜索树,则输出转换之后的 ...

  4. Linux 内核数据结构:Linux 双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  5. Linux 内核数据结构:双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  6. 线性表-双向链表(LinkedList)

    双向链表:如图1-3 所示,会把当前header拆分开,重新插入一个Entry<E>. LinkedList源码 0.首先这个类中的两个变量 private transient Entry ...

  7. Shuffling Machine和双向链表

    1. 双向链表 https://github.com/BodhiXing/Data_Structure 2. Shuffling Machine https://pta.patest.cn/pta/t ...

  8. MS - 1 - 把二元查找树转变成排序的双向链表

    ## 1. 把二元查找树转变成排序的双向链表 ## ### 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表. ### 要求不能创建任何新的结点,只调整指针的指向. 10       ...

  9. javascript中的链表结构—双向链表

    1.概念 上一个文章里我们已经了解到链表结构,链表的特点是长度不固定,不用担心插入新元素的时候新增位置的问题.插入一个元素的时候,只要找到插入点就可以了,不需要整体移动整个结构. 这里我们了解一下双向 ...

  10. Java自己实现双向链表LinkList

    /** * <p> * Node 双向链表实体类 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com& ...

随机推荐

  1. jetty 8.0 add filter example

    http://zyn010101.iteye.com/blog/1679798 package com.cicc.gaf.sso.server;import java.io.IOException;i ...

  2. LaTeX 公式(转自)Iowa_Battleship 神犇

    传送门 (我这个蒟蒻只是mark一下 这个LaTex公式很全!!我是照着打数学公式的!! orz大佬Iowa

  3. crontab定时任务操作

    一.查看定时任务 crontab -l 二.添加定时任务 crontab -e (一)执行外部链接 //每隔10分钟执行1次 */ * * * * /usr/bin/curl "http:/ ...

  4. 用 npm 安装删除模块

    npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [npm install -g xxx]利用npm安装全局模块xxx: 本地安装时将模块写入pac ...

  5. async 和 await

    win8 app开发中使用async,await可以更方便地进行异步开发. async,await的使用可参考代码:Async Sample: Example from "Asynchron ...

  6. 更新vs2017 15.9.2后,在指定-T v141_xp情况下载编译会报下面warning MSB8051

    更新vs2017 15.9.2后,在指定-T v141_xp情况下载编译会报下面warning: C:\Program Files (x86)\Microsoft Visual Studio\2017 ...

  7. HTTP Error 403.14问题处理

    打开目录浏览后,点击启用.

  8. 661. Image Smoother

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. yii2 beforeAction 重定向问题

    不跳转代码:return $this->redirect('http://www.yiichina.com/'); 跳转代码:return $this->redirect('http:// ...

  10. 第二章:冠词(Les articles)

    ★定冠词(Les articles définis ): 阳性单数:le(l') 阴性单数:la(l') 阴阳性复数:les ()表示前面已经提到的人或事物: ()有关的名词已被其它的成分(补语,关系 ...