C++中的链表是一种非常常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。
链表结构包括单向链表、双向链表和循环链表;
单向链表由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针;
//单向链表结构
class Node
{
public:
int data;
Node* next;
};
//单向链表类
class LinkedList {
public:
LinkedList()
{
head = nullptr;
}
private:
Node* head;
};
对于定义的单向链表类,可以通过构造函数定义元素为空的链表,也可以通过initialize
来初始化包含一个元素的链表;
示例
LinkedList()
{
head = nullptr;
}
void initialize(int value)
{
head = new Node();
head->data = value;
head->next = nullptr;
}
获取链表的长度,即计算链表中包含多少个元素,通过遍历其中的元素计算
int length()
{
int count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
插入元素
根据元素插入的位置和元素来进行元素的插入操作
示例
void insertNode(int index, int value)
{
// 判断是否满足插入条件
if (index < 0 || index > length()) {
return;
} else if (index == 0) { //首元素
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
} else {
Node* prevNode = getNode(index - 1); //根据下标获取元素的方法
Node* newNode = new Node();
newNode->data = value;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
}
元素追加
元素的追加,将元素插入到元素的末尾
void append(int value)
{
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
删除元素
根据下标对元素进行删除
void deleteNode(int index)
{
if (index < 0 || index >= length()) {
return;
} else if (index == 0) {
Node* temp = head;
head = head->next;
delete temp;
} else {
Node* prevNode = getNode(index - 1);
Node* currentNode = prevNode->next;
prevNode->next = currentNode->next;
delete currentNode;
}
}
清空链表
清空链表中的元素
void clear()
{
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
head = nullptr;
}
判断链表是否为空
bool isEmpty()
{
return (head == nullptr);
}
根据链表的下标,来获取该位置的元素
Node* getNode(int index)
{
if (index < 0 || index >= length()) {
return nullptr;
} else {
Node* current = head;
int i = 0;
while (i < index) {
current = current->next;
i++;
}
return current;
}
}
void printList()
{
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
数据缓存:单向链表可以用作缓存数据结构,新的数据可以插入到链表的头部,而旧的数据可以从尾部移除,以限制缓存大小并保持最近使用的数据在链表的头部。
队列和栈的实现:单向链表可以用来实现队列和栈这两种常见的数据结构。在队列中,数据从尾部插入(入队),从头部删除(出队)。在栈中,数据只在链表的头部插入和删除,模拟了后进先出(LIFO)的行为。
双向链表与单向链表不同之处在于每个节点除了指向下一个节点的指针外,还包含指向前一个节点的指针,这使得双向链表可以在前后两个方向上遍历和操作节点。
双向链表这里使用了模板
template<typename T>
class Node {
public:
T data;
Node<T>* prev;
Node<T>* next;
};
template<typename T>
class DoublyLinkedList {
public:
DoublyLinkedList() {
head = nullptr;
}
private:
Node<T>* head;
};
void initialize(const T& value)
{
head = new Node<T>();
head->data = value;
head->prev = nullptr;
head->next = nullptr;
}
int length()
{
int count = 0;
Node<T>* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
插入元素
void insertNode(int index, const T& value)
{
if (index < 0 || index > length()) {
return;
} else if (index == 0) {
Node<T>* newNode = new Node<T>();
newNode->data = value;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
} else {
Node<T>* prevNode = getNode(index - 1);
Node<T>* nextNode = prevNode->next;
Node<T>* newNode = new Node<T>();
newNode->data = value;
newNode->prev = prevNode;
newNode->next = nextNode;
prevNode->next = newNode;
if (nextNode != nullptr) {
nextNode->prev = newNode;
}
}
}
元素追加
void append(const T& value)
{
Node<T>* newNode = new Node<T>();
newNode->data = value;
newNode->prev = nullptr;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node<T>* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
删除元素
void deleteNode(int index)
{
if (index < 0 || index >= length()) {
return;
} else if (index == 0) {
Node<T>* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete temp;
} else {
Node<T>* currNode = getNode(index);
if (currNode == nullptr) {
return;
}
Node<T>* prevNode = currNode->prev;
Node<T>* nextNode = currNode->next;
prevNode->next = nextNode;
if (nextNode != nullptr) {
nextNode->prev = prevNode;
}
delete currNode;
}
}
清空
void clear()
{
Node<T>* current = head;
while (current != nullptr) {
Node<T>* next = current->next;
delete current;
current = next;
}
head = nullptr;
}
判断链表是否为空
bool isEmpty()
{
return (head == nullptr);
}
Node<T>* getNode(int index)
{
if (index < 0 || index >= length()) {
return nullptr;
} else {
Node<T>* current = head;
int i = 0;
while (i < index) {
current = current->next;
i++;
}
return current;
}
}
双向链表支持正向输出和反向输出
void printListForward(Node<T>* first)
{
Node<T>* current = first;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
void printListBackward(Node<T>* last)
{
Node<T>* current = last;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->prev;
}
std::cout << std::endl;
}
实现链表或双端队列:双向链表天然支持在链表头和链表尾高效地插入和删除元素,使其适用于实现链表或双端队列(deque)等数据结构。
实现LRU缓存算法:LRU(Least Recently Used)缓存算法中,当缓存已满时,最近最少使用的元素会被淘汰。双向链表可以方便地记录元素的访问顺序,并且在需要淘汰元素时,可以快速删除链表尾部的节点。
实现迭代器:双向链表的双向遍历特性使其非常适合用于实现迭代器。迭代器可以用于遍历容器中的元素,而双向链表可以提供前向和后向的遍历方式。
循环链表是一种特殊类型的链表,其中链表的最后一个节点指向第一个节点,形成一个循环。与普通链表不同,循环链表没有一个明确的末尾节点,可以通过任何一个节点遍历整个链表。
template<typename T>
class Node {
public:
T data;
Node<T>* next;
};
template<typename T>
class CircularLinkedList {
public:
CircularLinkedList() {
head = nullptr;
}
private:
Node<T>* head;
};
void initialize(const T& value)
{
head = new Node<T>;
head->data = value;
head->next = head;
}
int length()
{
if (head == nullptr) {
return 0;
} else {
int count = 1;
Node<T>* current = head->next;
while (current != head) {
count++;
current = current->next;
}
return count;
}
}
插入元素
void insertNode(int index, const T& value)
{
Node<T>* newNode = new Node<T>();
newNode->data = value;
if (isEmpty() || index <= 0) {
head = newNode;
head->next = head;
} else {
Node<T>* prevNode = getNode(index - 1);
if (prevNode == nullptr) {
delete newNode;
return;
}
newNode->next = prevNode->next;
prevNode->next = newNode;
}
}
元素追加
void append(const T& value)
{
Node<T>* newNode = new Node<T>;
newNode->data = value;
newNode->next = head;
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
Node<T>* current = head;
while (current->next != head) {
current = current->next;
}
current->next = newNode;
}
}
删除元素
void deleteNode(int index)
{
if (isEmpty() || index < 0) {
return;
} else if (index == 0) {
Node<T>* currNode = head;
if (head->next == head) {
head = nullptr;
} else {
Node<T>* lastNode = head;
while (lastNode->next != head) {
lastNode = lastNode->next;
}
lastNode->next = head->next;
head = head->next;
}
delete currNode;
} else {
Node<T>* prevNode = getNode(index - 1);
if (prevNode == nullptr || prevNode->next == head) {
return;
}
Node<T>* currNode = prevNode->next;
prevNode->next = currNode->next;
delete currNode;
}
}
清空
void clear() {
Node<T>* current = head;
while (current != nullptr && current->next != head) {
Node<T>* next = current->next;
delete current;
current = next;
}
head = nullptr;
}
判断是否为空
bool isEmpty()
{
return (head == nullptr);
}
Node<T>* getNode(int index)
{
if (index < 0 || index >= length()) {
return nullptr;
} else {
Node<T>* current = head;
int i = 0;
while (i < index) {
current = current->next;
i++;
}
return current;
}
}
void printCircularLinkedList(Node<T>* head)
{
if (head == nullptr) {
return; // 空链表
}
Node<T>* current = head;
do {
std::cout << current->data << " ";
current = current->next;
} while (current != head);
std::cout << std::endl;
}
环形缓冲区(Circular Buffer):环形缓冲区是一种常用的高效数据存储结构,常用于实现循环队列。在环形缓冲区中,最后一个元素的下一个位置是第一个元素,形成一个循环。循环链表提供了一种有效的数据结构来实现环形缓冲区。
循环播放列表:在音乐播放器或视频播放器中,可以使用循环链表来实现循环播放列表。每个歌曲或视频可以用链表节点表示,通过调整节点的顺序,可以实现循环播放功能。