C++双向链表的建立(无头节点)
来源:互联网 发布:java生成安装包 编辑:程序博客网 时间:2024/05/16 00:36
#ifndef doublychain_#define doublychain_#include<iostream>#include<algorithm>#include"chainNode.h"using namespace std;template<typename T>class doublychain{public: doublychain(); ~doublychain();void insert(const int theIndex, const T& theElement);void erase(int theIndex);void output(ostream& out);protected:chainNode<T>*firstNode;chainNode<T>*lastNode;int listSize;};template<typename T>doublychain<T>::doublychain(){firstNode = lastNode = NULL;listSize = 0;}template<typename T>doublychain<T>::~doublychain(){while (firstNode != NULL){chainNode<T>*next = firstNode->next;delete firstNode; firstNode = next;}delete firstNode;}template<typename T>void doublychain<T>::insert(const int theIndex, const T& theElement){if (theIndex == 0){firstNode = new chainNode<T>(theElement, firstNode);lastNode = firstNode;firstNode->pre = NULL;lastNode->next = NULL;}else {chainNode<T>*p = firstNode;chainNode<T>*q;for (int i = 0; i < theIndex -1; ++i)p = p->next;p->next = new chainNode<T>(theElement, p->next);q = p->next;q->pre = p;if (theIndex ==listSize)lastNode = p->next;}++listSize;}template<typename T>void doublychain<T>::erase(int theIndex){chainNode<T>*deleteNode;chainNode<T>*q;if (theIndex = 0){deleteNode=firstNode;q = firstNode->next;firstNode = firstNode->next;q->pre = NULL;}else {chainNode<T>*p = firstNode;for (int i = 0; i < theIndex - 1; i++)p = p->next;deleteNode=p->next;q = p->next->next;p->next = p->next->next;q->pre = p;if (deleteNode == lastNode)lastNode = p;}delete deleteNode;--listSize;}//对双向链表进行测试template<typename T>void doublychain<T>::output(ostream& out){/*for (chainNode<T>*p = firstNode; p != NULL; p=p->next)cout << p->element << " ";*/for (chainNode<T>*p = lastNode; p !=NULL; p = p->pre)cout << p->element << " ";}#endif
阅读全文
0 0
- C++双向链表的建立(无头节点)
- C语言实现无头节点链表
- C++实现无头结点的双向链表
- 双向链表的建立、删除/插入节点
- 数据结构 《3》----双向链表(带有头节点)
- 双向有头节点链表
- 有头节点双向循环链表
- 无头节点,删除已知节点的前驱(循环链表)
- 线性表的链式存储结构(无头节点)
- 单链表(无头节点)的实现
- 双向链表的建立,添加节点和删除节点(注意插入和删除要分三种情况)
- 双向链表的初始化,建立,添加节点和删除节点(注意插入和删除要分三种情况)
- C++构建循环链表(无头节点)
- 单向无头节点链表操作
- 链表的归并(无头结点,c语言)
- C语言实现无头节点的单链表
- 【C++】模板实现带头节点的双向循环链表
- C语言实现无头节点单链表
- 【bzoj 2662】冻结(还是分层图+spfa)
- 百度地图api开发时显示时只有白格子,没有地图的解决方法
- Loi模拟题之Loi水题汇总 MST+DP+DP+树剖
- c++模板 template
- MVp基本使用步骤
- C++双向链表的建立(无头节点)
- 使用Golang实现栈的链式存储
- 剪花布条 (KMP)
- AMQP 0-9-1模型
- linux系统命令之系统虚拟机管理
- 二级列表仿购物车
- 深入理解Spring系列之十二:@Transactional是如何工作的
- git命令
- 异常捕获并打印