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

链表头文件大更新!!!

引言

原文章:链表简介及自制链表操作头文件_自己写一个链表头文件-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 替换 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. 创建和操作整数链表
#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. 创建和操作字符串链表
#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博客。

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


求关注~


http://www.kler.cn/a/427502.html

相关文章:

  • 环形链表 (简单易懂)
  • selenium爬虫
  • 获取海康综合安防管理平台(区域,监控点,时效性rtsp流)数据
  • 1207论文速读
  • DICOM医学影象应用篇——多平面重建(MPR)在DICOM医学影像中的应用详解
  • 将html文件改成标准的html标签
  • 新增白名单赋予应用安装权限
  • 第33周:运动鞋识别(Tensorflow实战第五周)
  • shell中[] [[]] (())比较
  • 5.12【机器学习】卷积模型搭建
  • 【UE5】制作插件 并调试【vs2022】
  • android studio 读写文件操作(应用场景三)
  • zabbix监控服务的搭建与使用
  • SpringCloud-Eureka与Dubbo(Zookeeper)的深度对比:微服务架构下的服务注册与发现
  • k8s-Informer之Indexer的解析(4)
  • 循序渐进kubenetes Service(Cluster ip、Nodeport、Loadbalancer)
  • 简易图书管理系统
  • 16 Java(junit)测试+Assert(断言测试)、枚举类、注解、javac编译和javap反编译命令、常量
  • 夏普MX-4608N复印机维修模式进入方法及载体初始化方法
  • 基于STM32的Wi-Fi无人机项目