当前位置: 首页 > news >正文

链表头文件大更新!!!

引言

原文章:链表简介及自制链表操作头文件_自己写一个链表头文件-CSDN博客。

此次更新添加了更多功能,让改头文件更 人性化

安装教程见原文章。

介绍 linked_list.h 头文件

linked_list.h 是一个 C++ 头文件,定义了一个模板类 LinkedList,用于实现单向链表。链表中的每个节点由 Node 结构体表示,包含数据和指向下一个节点的指针。LinkedList 类提供了多种操作链表的方法,包括插入、删除、查找、打印、获取链表大小、判断链表是否为空等。此外,还定义了链表的加法和减法运算。

头文件内容
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#pragma once#include <iostream>template <typename T>
struct Node {T data;Node* next;Node(T data) : data(data), next(0) {}  // 使用 0 替换 nullptr
};template <typename T>
class LinkedList {
public:LinkedList();~LinkedList();void insert(T data);void remove(T data);Node<T>* search(T data);void print() const;int size() const;bool isEmpty() const;Node<T>* getHead() const;Node<T>* getTail() const;// 加法运算LinkedList<T>& operator+(const LinkedList<T>& other);// 减法运算LinkedList<T>& operator-(const LinkedList<T>& other);private:Node<T>* head;
};template <typename T>
LinkedList<T>::LinkedList() : head(0) {}  // 使用 0 替换 nullptrtemplate <typename T>
LinkedList<T>::~LinkedList() {Node<T>* current = head;while (current) {Node<T>* next = current->next;delete current;current = next;}
}template <typename T>
void LinkedList<T>::insert(T data) {Node<T>* newNode = new Node<T>(data);if (!head) {head = newNode;} else {Node<T>* current = head;while (current->next) {current = current->next;}current->next = newNode;}
}template <typename T>
void LinkedList<T>::remove(T data) {if (!head) {return;}if (head->data == data) {Node<T>* temp = head;head = head->next;delete temp;return;}Node<T>* current = head;while (current->next && current->next->data != data) {current = current->next;}if (current->next) {Node<T>* temp = current->next;current->next = current->next->next;delete temp;}
}template <typename T>
Node<T>* LinkedList<T>::search(T data) {Node<T>* current = head;while (current) {if (current->data == data) {return current;}current = current->next;}return 0;  // 使用 0 替换 nullptr
}template <typename T>
void LinkedList<T>::print() const {Node<T>* current = head;while (current) {std::cout << current->data << " ";current = current->next;}std::cout << std::endl;
}template <typename T>
int LinkedList<T>::size() const {int count = 0;Node<T>* current = head;while (current) {count++;current = current->next;}return count;
}template <typename T>
bool LinkedList<T>::isEmpty() const {return head == 0;  // 使用 0 替换 nullptr
}template <typename T>
Node<T>* LinkedList<T>::getHead() const {return head;
}template <typename T>
Node<T>* LinkedList<T>::getTail() const {Node<T>* current = head;Node<T>* tail = 0;  // 使用 0 替换 nullptrwhile (current) {tail = current;current = current->next;}return tail;
}// 加法运算
template <typename T>
LinkedList<T>& LinkedList<T>::operator+(const LinkedList<T>& other) {Node<T>* current = other.head;while (current) {this->insert(current->data);current = current->next;}return *this;
}// 减法运算
template <typename T>
LinkedList<T>& LinkedList<T>::operator-(const LinkedList<T>& other) {Node<T>* current = other.head;while (current) {this->remove(current->data);current = current->next;}return *this;
}
#endif

使用样例

1. 创建和操作整数链表
#include "linked_list.h"int main() {LinkedList<int> list1;list1.insert(1);list1.insert(2);list1.insert(3);std::cout << "List1: ";list1.print();  // 输出: 1 2 3LinkedList<int> list2;list2.insert(4);list2.insert(5);std::cout << "List2: ";list2.print();  // 输出: 4 5// 加法运算list1 = list1 + list2;std::cout << "List1 + List2: ";list1.print();  // 输出: 1 2 3 4 5// 减法运算list1 = list1 - list2;std::cout << "List1 - List2: ";list1.print();  // 输出: 1 2 3// 查找元素Node<int>* found = list1.search(2);if (found) {std::cout << "Found: " << found->data << std::endl;  // 输出: Found: 2} else {std::cout << "Not found" << std::endl;}// 删除元素list1.remove(2);std::cout << "After removing 2: ";list1.print();  // 输出: 1 3return 0;
}
2. 创建和操作字符串链表
#include "linked_list.h"int main() {LinkedList<std::string> list1;list1.insert("apple");list1.insert("banana");list1.insert("cherry");std::cout << "List1: ";list1.print();  // 输出: apple banana cherryLinkedList<std::string> list2;list2.insert("date");list2.insert("elderberry");std::cout << "List2: ";list2.print();  // 输出: date elderberry// 加法运算list1 = list1 + list2;std::cout << "List1 + List2: ";list1.print();  // 输出: apple banana cherry date elderberry// 减法运算list1 = list1 - list2;std::cout << "List1 - List2: ";list1.print();  // 输出: apple banana cherry// 查找元素Node<std::string>* found = list1.search("banana");if (found) {std::cout << "Found: " << found->data << std::endl;  // 输出: Found: banana} else {std::cout << "Not found" << std::endl;}// 删除元素list1.remove("banana");std::cout << "After removing banana: ";list1.print();  // 输出: apple cherryreturn 0;
}

应用实例

1. 数据管理

链表可以用于管理动态数据,例如在内存管理中,链表可以用于管理空闲内存块。每个节点可以表示一个空闲内存块,链表可以动态地插入和删除节点,以适应内存分配和释放的需求。

2. 文件系统

在文件系统中,链表可以用于管理文件和目录的链接。每个节点可以表示一个文件或目录,链表可以动态地插入和删除节点,以适应文件系统的增删改查操作。

3. 队列和栈

链表可以用于实现队列和栈。队列可以通过在链表的尾部插入元素和在头部删除元素来实现,而栈可以通过在链表的头部插入和删除元素来实现。

4. 图形用户界面

在图形用户界面中,链表可以用于管理窗口和控件的层次关系。每个节点可以表示一个窗口或控件,链表可以动态地插入和删除节点,以适应用户界面的动态变化。

以上就是本文章的全部内容。安装教程见链表简介及自制链表操作头文件_自己写一个链表头文件-CSDN博客。

关煮一下吧你的认可就是我最大的动力~~


求关注~

http://www.lryc.cn/news/499794.html

相关文章:

  • 力扣3381.长度可被K整除的子数组的最大元素和
  • http.ServeMux多路复用器的设置
  • 优化器与优化方法:在现代科学与工程中的应用
  • 笔记本外接显示屏没声音
  • vue框架
  • Vue指令(一)--v-html、v-show、v-if、v-else、v-else-if、v-on、v-bind、v-for、v-model
  • ElK 8 收集 MySQL 慢查询日志并通过 ElastAlert2 告警至飞书
  • QT通过在线安装器安装【详细】
  • 34.1 uber开源的m3db简介
  • MATLAB 最小二乘点云拟合球 (89)
  • 【Altium Designer 】AD如何使用嘉立创元器件的3D封装
  • G15沈海高速茶白高架自动化监测
  • 网站从渗透到mssql提权全过程
  • Qt多线程与QTimer详解
  • 基于stm23的智慧宿舍系统 (DAY10)_小程序
  • 深入理解Spring事务
  • Ubuntu22.04深度学习环境安装【Anaconda+Pycharm】
  • 五、docker的网络模式
  • 使用el-row和el-col混合table设计栅格化,实现表头自适应宽度,表格高度占位
  • 【服务器监控】grafana+Prometheus+node exporter详细部署文档
  • JavaScript中todolist操作--待办事项的添加 删除 完成功能
  • Windows中MySQL8.3.4 MSI版本——详细安装教程
  • MySQL-DDL之数据库操作
  • Python 笔记之进程通信
  • 【Transformer序列预测】Pytorch中构建Transformer对序列进行预测源代码
  • 生产者-消费者模式:多线程并发协作的经典案例
  • 数据库-mysql(基本语句)
  • android12L super.img 解压缩及其挂载到ubuntu18.04
  • flask简易版的后端服务创建接口(python)
  • 小程序入门学习(四)之全局配置