链表头文件大更新!!!

引言

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

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

安装教程见原文章。

介绍 linked_list.h 头文件

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

头文件内容
cpp 复制代码
#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 替换 nullptr

template <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 替换 nullptr
    while (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. 创建和操作整数链表
cpp 复制代码
#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 3

    LinkedList<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 3

    return 0;
}
2. 创建和操作字符串链表
cpp 复制代码
#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 cherry

    LinkedList<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 cherry

    return 0;
}

应用实例

1. 数据管理

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

2. 文件系统

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

3. 队列和栈

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

4. 图形用户界面

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

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

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

求关注~

相关推荐
范纹杉想快点毕业1 小时前
以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·数据结构·c++·git·qt·链表·github
米粉03051 小时前
算法图表总结:查找、排序与递归(含 Mermaid 图示)
数据结构·算法·排序算法
黑色的山岗在沉睡2 小时前
LeetCode100.4 移动零
数据结构·算法·leetcode
霖002 小时前
PCIe数据采集系统
数据结构·经验分享·单片机·嵌入式硬件·fpga开发·信号处理
_Itachi__2 小时前
LeetCode 热题 100 114. 二叉树展开为链表
linux·leetcode·链表
敷啊敷衍2 小时前
深入探索 C++ 中的 string 类:从基础到实践
开发语言·数据结构·c++
方博士AI机器人2 小时前
算法与数据结构 - 二叉树结构入门
数据结构·算法·二叉树
{⌐■_■}2 小时前
【redis】redis常见数据结构及其底层,redis单线程读写效率高于多线程的理解,
数据结构·数据库·redis
sx2436943 小时前
day21:零基础学嵌入式之数据结构
数据结构
ai.Neo3 小时前
牛客网NC22157:牛牛学数列2
数据结构·c++·算法