泛型双向链表

双向链表(doublyLinkedList.h)

/*************************************************************************
> File Name : doublyLinkedList.h
> Author : Harold
> Mail : 2106562095@qq.com
> Github : www.github.com/Haroldcc
> Created Time : 2020年02月28日 10时23分06秒
************************************************************************/
/***** 双向链表 *****/ #ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H #include "linearList.h" // ADT
#include "myExceptions.h" // 异常类
#include <sstream> // for ostringstream /*** 链表节点类 ***/
template <typename T>
class Node
{
public:
T element;
Node<T> *prev;
Node<T> *next; Node(){};
Node(const T &element) { this->element = element; }
Node(const T &element, Node<T> *prev, Node<T> *next)
{
this->element = element;
this->prev = prev;
this->next = next;
}
}; /*** 双向链表类 ***/
template <typename T>
class doublyLinkedList : public linearList<T>
{
private:
void checkIndex(int index) const; // 索引异常检测 Node<T> *firstNode; // 链表头节点指针
Node<T> *lastNode; // 链表尾节点指针
int listSize; // 链表的元素个数 public:
/* 构造函数 */
doublyLinkedList(int initialCapacity = 10);
~doublyLinkedList(); /* ADT */
bool isEmpty() const { return this->listSize == 0; }
int size() const { return this->listSize; }
T &getElement(int index) const;
void setElement(int index, const T &element);
int indexOf(const T &element) const;
void removeByIndex(int index);
void removeByElement(const T &element);
void add(const T &element);
void add(int index, const T &element);
void clear();
void output(std::ostream &out) const;
}; // 默认构造
template <typename T>
doublyLinkedList<T>::doublyLinkedList(int initialCapacity)
{
if (initialCapacity < 1)
{
std::ostringstream s;
s << "初始化容量 = " << initialCapacity << "必须 > 0";
throw illegalParameterValue(s.str());
}
this->firstNode = nullptr;
this->lastNode = nullptr;
this->listSize = 0;
} // 析构函数
template <typename T>
doublyLinkedList<T>::~doublyLinkedList()
{
} template <typename T>
void doublyLinkedList<T>::checkIndex(int index) const
{
if (index < 0 || index >= this->listSize)
{
std::ostringstream s;
s << "index = " << index << "size = " << this->listSize;
throw illegalIndex(s.str());
}
} // 根据索引获取对应的节点,如未找到,抛异常
template <typename T>
T &doublyLinkedList<T>::getElement(int index) const
{
checkIndex(index); Node<T> *currentNode = this->firstNode;
for (int i = 0; i < index; i++)
currentNode = currentNode->next; return currentNode->element;
} // 根据索引设置元素值
template <typename T>
void doublyLinkedList<T>::setElement(int index, const T &element)
{
checkIndex(index); Node<T> *currentNode = this->firstNode;
for (int i = 0; i < index; i++)
currentNode = currentNode->next; currentNode->element = element;
} // 返回给定元素的索引值,若未找到返回 -1
template <typename T>
int doublyLinkedList<T>::indexOf(const T &element) const
{
Node<T> *currentNode = this->firstNode;
int index = 0;
while (currentNode != nullptr && currentNode->element != element)
{
currentNode = currentNode->next;
index++;
} if (currentNode == nullptr)
{
return -1;
}
else
{
return index;
}
} // 删除指定索引处的元素值,未找到元素抛异常
template <typename T>
void doublyLinkedList<T>::removeByIndex(int index)
{
checkIndex(index); Node<T> *deleteNode;
if (index == 0)
{ // 删除链表中的第一个元素
deleteNode = this->firstNode;
this->firstNode = this->firstNode->next;
}
else
{
Node<T> *p = this->firstNode;
for (int i = 0; i < index - 1; i++)
p = p->next; deleteNode = p->next;
p->next = p->next->next;
}
this->listSize--;
delete deleteNode;
} template <typename T>
void doublyLinkedList<T>::removeByElement(const T &element)
{
removeByIndex(indexOf(element));
} template <typename T>
void doublyLinkedList<T>::add(const T &element)
{
add(this->listSize, element);
} // 在指定索引处添加元素
template <typename T>
void doublyLinkedList<T>::add(int index, const T &element)
{
if (index < 0 || index > this->listSize)
{
std::ostringstream s;
s << "index = " << index << "size = " << this->listSize;
throw illegalIndex(s.str());
} if (index == 0)
{ // 头部插入元素
this->firstNode = new Node<T>(element, nullptr, this->firstNode);
}
else
{
Node<T> *p = this->firstNode;
for (int i = 0; i < index - 1; i++)
{
p = p->next;
}
p->next = new Node<T>(element, p->prev, p->next);
}
this->listSize++;
} template <typename T>
void doublyLinkedList<T>::clear()
{
while (this->firstNode != nullptr)
{
Node<T> *nextNode = this->firstNode->next;
delete this->firstNode;
this->firstNode = nextNode;
}
this->firstNode = this->lastNode = nullptr;
this->listSize = 0;
} template <typename T>
void doublyLinkedList<T>::output(std::ostream &out) const
{
for (Node<T> *currentNode = this->firstNode;
currentNode != nullptr;
currentNode = currentNode = currentNode->next)
out << currentNode->element << " ";
} // 重载 << 运算符
template <typename T>
std::ostream &operator<<(std::ostream &out, const doublyLinkedList<T> &list)
{
list.output(out);
return out;
} #endif

测试(testDoublyLinkedList.cpp)

/*************************************************************************
> File Name : testDoublyLinkedList.cpp
> Author : Harold
> Mail : 2106562095@qq.com
> Github : www.github.com/Haroldcc
> Created Time : 2020年02月28日 20时38分11秒
************************************************************************/
#include <iostream>
#include "linearList.h"
#include "doublyLinkedList.h" using namespace std; int main()
{
doublyLinkedList<int> list; list.add(1);
list.add(2); cout << list << endl; // test constructor
linearList<double> *x = new doublyLinkedList<double>;
doublyLinkedList<int> y, z; // test size
cout << "Initial size of x, y, and z = "
<< x->size() << ", "
<< y.size() << ", "
<< z.size() << endl; // test isEmpty
if (x->isEmpty())
cout << "x is isEmpty" << endl;
else
cout << "x is not isEmpty" << endl;
if (y.isEmpty())
cout << "y is isEmpty" << endl;
else
cout << "y is not isEmpty" << endl; // test add
y.add(0, 2);
y.add(1, 6);
y.add(0, 1);
y.add(2, 4);
y.add(3, 5);
y.add(2, 3);
cout << "Inserted 6 integers, list y should be 1 2 3 4 5 6" << endl;
cout << "Size of y = " << y.size() << endl;
if (y.isEmpty())
cout << "y is isEmpty" << endl;
else
cout << "y is not isEmpty" << endl;
y.output(cout);
cout << endl
<< "Testing overloaded <<" << endl;
cout << y << endl; // test indexOf
int index = y.indexOf(4);
if (index < 0)
cout << "4 not found" << endl;
else
cout << "The index of 4 is " << index << endl; index = y.indexOf(7);
if (index < 0)
cout << "7 not found" << endl;
else
cout << "The index of 7 is " << index << endl; // test getElement
cout << "Element with index 0 is " << y.getElement(0) << endl;
cout << "Element with index 3 is " << y.getElement(3) << endl; // test removeByIndex
y.removeByIndex(1);
cout << "Element 1 erased" << endl;
cout << "The list is " << y << endl;
y.removeByIndex(2);
cout << "Element 2 erased" << endl;
cout << "The list is " << y << endl;
y.removeByIndex(0);
cout << "Element 0 erased" << endl;
cout << "The list is " << y << endl; cout << "Size of y = " << y.size() << endl;
if (y.isEmpty())
cout << "y is isEmpty" << endl;
else
cout << "y is not isEmpty" << endl; try
{
y.add(-3, 0);
}
catch (illegalIndex e)
{
cout << "Illegal index exception" << endl;
cout << "Insert index must be between 0 and list size" << endl;
e.outputMessage();
} // a few more inserts, just for fun
y.add(0, 4);
y.add(0, 5);
y.add(0, 6);
y.add(0, 7);
cout << "y is " << y << endl; y.clear(); return 0;
}

输出

1 2
Initial size of x, y, and z = 0, 0, 0
x is isEmpty
y is isEmpty
Inserted 6 integers, list y should be 1 2 3 4 5 6
Size of y = 6
y is not isEmpty
1 2 3 4 5 6
Testing overloaded <<
1 2 3 4 5 6
The index of 4 is 3
7 not found
Element with index 0 is 1
Element with index 3 is 4
Element 1 erased
The list is 1 3 4 5 6
Element 2 erased
The list is 1 3 5 6
Element 0 erased
The list is 3 5 6
Size of y = 3
y is not isEmpty
Illegal index exception
Insert index must be between 0 and list size
index = -3size = 3
y is 7 6 5 4 3 5 6

C++泛化双向链表的更多相关文章

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

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

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

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

  3. UML类图关系--继承(泛化)、实现、关联、聚合、组合、依赖

    在UML类图中,常见的有以下几种关系:  泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregation),组合(Composi ...

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

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

  5. UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)

    UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父 ...

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

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

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

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

  8. [UML]UML系列——类图class的泛化关系

    系列文章       [UML]UML系列——用例图Use Case       [UML]UML系列——用例图中的各种关系(include.extend)       [UML]UML系列——类图C ...

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

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

随机推荐

  1. Gson、jackson 序列化,反序列化(单个、集合)

    实体类: package com.nf.redisDemo1.entity; public class News { private long id; private String title; pr ...

  2. Educational Codeforces Round 48 (Rated for Div. 2)异或思维

    题:https://codeforces.com/contest/1016/problem/D 题意:有一个 n * m 的矩阵, 现在给你 n 个数, 第 i 个数 a[ i ] 代表 i 这一行所 ...

  3. Java 网址短链接服务原理及解决方案

    一.背景 现在在各种圈的产品各种推广地址,由于URL地址过长,不美观.不方便收藏.发布.传播以及各种发文字数限制等问题,微信.微博都在使用短链接技术.最近由于使用的三方的生成.解析短链接服务开始限制使 ...

  4. 关于dubbo接口性能测试

      最初的压测这个dubbo接口有三种思路: .第一种就是基于业务,比如注册业务,注册成功后,会发送短信消息到用户手机,通过业务调用消息服务,最容易实现,但是业务瓶颈最大导致测试结果不准 .第二种是通 ...

  5. android应用市场、社区客户端、漫画App、TensorFlow Demo、歌词显示、动画效果等源码

    Android精选源码 MVP架构Android应用市场项目 android刻度盘控件源码 Android实现一个社区客户端 android商品详情页上拉查看详情 基于RxJava+Retrofit2 ...

  6. Win7如何查看nvidia显卡(GPU)的利用率

    1.在文件夹C:\Program Files\NVIDIA Corporation\NVSMI里找到文件nvidia-smi.exe2.把该文件拖到命令提示符窗口(win+R,再输入‘CMD’进入), ...

  7. Vue+elementUI build打包部署后字体图标丢失问题

    错误描述:Vue+elementUI  build打包部署后字体图标丢失,控制台显示文件element-icons.woff和element-icons.ttf文件404 错误展现: 控制台报错截图 ...

  8. [LC] 246. Strobogrammatic Number

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  9. hosts原理及作用

    https://blog.csdn.net/qq_41356488/article/details/82190372 手机也可以修改hosts,但是需要root权限,百度

  10. Javascript面试题&知识点汇总

    问题&答案 什么时候 a ==1 && a== 2 && a==3 为 true? var a = { i: 1, toString: function () ...