用C++实现一个基于模板的双向循环链表并且每个节点可以衍生出一条双向循环链表并加上详细的注释
时间: 2024-01-24 16:20:47 浏览: 59
以下是基于模板的双向循环链表的实现,每个节点可以衍生出一条双向循环链表,并且添加了详细的注释:
```c++
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class Node {
public:
T data; // 存储节点数据
Node<T> *prev; // 指向前一个节点的指针
Node<T> *next; // 指向后一个节点的指针
Node<T> *child; // 指向子链表的头节点的指针
// 构造函数
Node(T d = T(), Node<T> *p = NULL, Node<T> *n = NULL, Node<T> *c = NULL) :
data(d), prev(p), next(n), child(c) {}
// 析构函数
~Node() {}
};
template <class T>
class DoubleCircularLinkedList {
private:
Node<T> *head; // 指向头节点的指针
int size; // 链表的长度
public:
// 构造函数
DoubleCircularLinkedList() {
head = new Node<T>();
head->prev = head->next = head;
head->child = NULL;
size = 0;
}
// 析构函数
~DoubleCircularLinkedList() {
Node<T> *curr = head;
while (size > 0) {
Node<T> *temp = curr;
curr = curr->next;
delete temp;
size--;
}
delete head;
}
// 在链表末尾插入一个节点
void insert(T data) {
Node<T> *curr = head;
while (curr->next != head) {
curr = curr->next;
}
Node<T> *newNode = new Node<T>(data, curr, head, NULL);
curr->next = newNode;
head->prev = newNode;
size++;
}
// 在链表中查找指定的数据并返回节点的指针
Node<T> *search(T data) {
Node<T> *curr = head->next;
while (curr != head) {
if (curr->data == data) {
return curr;
}
curr = curr->next;
}
return NULL;
}
// 在链表中删除指定的节点
void remove(Node<T> *node) {
node->prev->next = node->next;
node->next->prev = node->prev;
delete node;
size--;
}
// 获取链表的长度
int getSize() {
return size;
}
// 在指定的节点后插入一条子链表
void insertChild(Node<T> *node, DoubleCircularLinkedList<T> *childList) {
node->child = childList->head->next;
childList->head->next->prev = node;
childList->head->prev->next = node->next;
node->next->prev = childList->head->prev;
node->next = childList->head->next;
childList->head->prev = NULL;
childList->head->next = NULL;
size += childList->getSize();
}
// 在指定的节点后删除一条子链表
void removeChild(Node<T> *node) {
DoubleCircularLinkedList<T> *childList = new DoubleCircularLinkedList<T>();
childList->head->next = node->child;
childList->head->prev = node->child->prev;
node->child->prev->next = childList->head;
node->child->prev = childList->head;
node->child = NULL;
size -= childList->getSize();
}
// 打印链表
void printList() {
Node<T> *curr = head->next;
while (curr != head) {
cout << curr->data << " ";
if (curr->child != NULL) {
cout << "(";
DoubleCircularLinkedList<T> *childList = new DoubleCircularLinkedList<T>();
childList->head->next = curr->child->next;
childList->head->prev = curr->child->prev;
childList->head->prev->next = childList->head;
childList->head->next->prev = childList->head;
childList->printList();
delete childList;
cout << ")";
}
curr = curr->next;
}
}
};
int main() {
DoubleCircularLinkedList<int> list;
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
list.insert(6);
list.insert(7);
list.search(4);
list.removeChild(list.search(2));
list.removeChild(list.search(4));
DoubleCircularLinkedList<int> childList1;
childList1.insert(8);
childList1.insert(9);
childList1.insert(10);
list.insertChild(list.search(3), &childList1);
DoubleCircularLinkedList<int> childList2;
childList2.insert(11);
childList2.insert(12);
childList2.insert(13);
list.insertChild(list.search(6), &childList2);
list.printList();
return 0;
}
```
以上代码中,`Node` 类表示链表中的一个节点,其中 `data` 存储节点数据,`prev` 指向前一个节点的指针,`next` 指向后一个节点的指针,`child` 指向子链表的头节点的指针。`DoubleCircularLinkedList` 类表示双向循环链表,其中 `head` 指向头节点的指针,`size` 表示链表的长度。该类实现了插入、查找、删除、添加子链表、删除子链表等操作,并提供了打印链表的方法。在主函数中,我们对链表进行了一些操作,并打印了链表中的所有数据。
阅读全文