链表头文件大更新!!!

引言

原文章:链表简介及自制链表操作头文件_自己写一个链表头文件-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博客

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

求关注~

相关推荐
lzx_00219 分钟前
list(全)
数据结构·list
不灭的黄金瞳12315 小时前
C语言手写顺序表
c语言·开发语言·数据结构
qeen8716 小时前
【数据结构】哈希表的C++实现与封装
数据结构·c++·散列表·哈希表
重生之后端学习16 小时前
239. 滑动窗口最大值[困难]✅
java·数据结构·算法·leetcode·职场和发展
fpcc17 小时前
算法和数据结构—动态规划法
数据结构·算法·动态规划
星星.72219 小时前
C++算法竞赛|二分查找与二分答案:边界模板、浮点二分、STL
数据结构·c++·算法
动词ing19 小时前
【学习笔记】数据结构(数组长度和关键特性+快慢指针+左右指针)
数据结构·笔记·学习
linux-hzh21 小时前
百日算法修炼 · Day 17
数据结构·算法
OPEN-F1 天前
C++入门教程:数组、字符串与指针入门
数据结构·c++·算法
pluviophile_s1 天前
数据结构:第7讲:图
数据结构·笔记