双向链表基础操作C++实现
来源:互联网 发布:智能手机看书软件 编辑:程序博客网 时间:2024/06/08 11:49
Node.h:
template <class T> struct Node{ T val; Node<T>* next; Node<T>* prev; Node(T nVal) { val = nVal; next =prev= nullptr; } Node(void){}};
#include "Node.h"#include <iostream>using namespace std;template <class> class LinkList{ int size;public: Node<T>* head; Node<T>* tail; LinkList(void) { head = tail = nullptr; size = 0; } ~LinkList(void){ Clear(); } void AddBack(T val) { Node<T>* pNode = new Node<T>(val); if (head == nullptr) { head = tail = pNode; } else { tail->next = pNode; pNode->prev = tail; tail = pNode; } size++; } void AddFront(T val) { Node<T>* pNode = new Node<T>(val); if (head == nullptr) { head = tail = pNode; } else { head->prev = pNode; pNode->next = head; head = pNode; } size++; } bool insertAt(int pos, T val) { Node<T>* pNode = nullptr; if (pos<0 || pos>size) { cout << "out of range" << endl; return false; } pNode = new Node<T>(val); if (pos == 0) AddFront(val); else if (pos = size - 1) AddBack(val); else { Node<T>* prevNode = GetPointerAt(pos - 1); pNode->next = prevNode->next; pNode->prev = prevNode; preNode->next->prev = pNode; prevNode->next = pNode; } size++; return true; } bool RemoveAt(int pos) { Node<T>* pNode = nullptr; if (size == 0) { cout << "list is empty" << endl; return false; } if (pos<0 || pos>size - 1) { cout << "out of range" << endl; return false; } if (size == 1) Clear(); else { if (pos == 0) { pNode = head; head = head->next; head->prev = nullptr; delete pNode; } else { Node<T>* prevNode = GetPointerAt(pos - 1); pNode = prevNode->next; prevNode->next = pNode->next; pNode->next->prev = prevNode; delete pNode; if (pos == size - 1) tail = prevNode; } } } bool RemoveBack() { return RemoveAt(size - 1); } bool RemoveFront() { return RemoveAt(0); } int Find(T val) { int index = 0; Node<T>* ip = head; while (ip != nullptr) { if (ip->val == val) return index; ip = ip->next; index++; } return -1; } bool IsEmpty() { return size == 0 ? true : false; } void Clear() { while (head != nullptr) { Node<T>* tmp = head->next; delete head; head = tmp; } tail = nullptr; size = 0; } private: Node<T>* GetPointerAt(int pos) { Node<T>* pNode = nullptr; if (pos<0 || pos>size - 1) cout << "out of range" << endl; else { pNode = head; for (int i = 1; i <= pos; i++) pNode = pNode->next; } return pNode; }};
0 0
- 双向链表基础操作C++实现
- 双向链表的实现与操作(C语言实现)
- [C++]双向链表操作
- C语言实现双向链表的基本操作
- C语言实现双向链表的基本操作
- 双向链表的操作实现(c语言)
- 双向链表操作大全C语言实现
- 双向链表的相关操作--C语言实现
- 数据结构之---c语言实现双向链表操作
- c语言实现双向链表的基本操作
- C语言实现双向链表删除节点、插入节点、双向输出等操作
- 双向链表-C/C++-多项式操作
- 双向循环链表---基础操作
- C/C++/JAVA 双向链表实现
- 基础数据结构--双向链表的实现
- C语言实现双向链表删除、插入、双向输出
- java实现双向链表的操作
- 双向循环链表操作的实现
- 【Window】 Screen capture with GDI&DX
- 深度学习(二十七)可视化理解卷积神经网络-ECCV 2014
- Convolutional Neural Networks for Visual Recognition
- linux grep命令
- 如何修改ubuntu的密码
- 双向链表基础操作C++实现
- You must reset your password using ALTER USER statement before executing this statement.
- oracle 存储过程in out inout三种参数模式和PROMPT用法
- Java中java.lang.ClassNotFoundException: org.springframework.web异常
- 深度学习(二十八)基于多尺度深度网络的单幅图像深度估计-NIPS 2014
- java虚拟机总结
- IOS8 Spring Animation API 动画
- iOS之单例模式详解
- FlyBoys优化过程10.8-10.16