C++day7

发布于:2025-02-27 ⋅ 阅读:(15) ⋅ 点赞:(0)

使用类模板封装一个链表,模板如下

class List{

public:

        struct node{

                T val;

                node* next;

                node* prev;可选

        };

private:

        node* head;

        node* tail;

        构造函数

        析构函数

        增删改查排遍历 6个函数

}

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

template <typename T>
class List{
public:
    struct Node
	{
        T val;
        Node* next;
        Node* prev;
        Node(T value) : val(value), next(nullptr), prev(nullptr) {}
    };
private:
    Node* head;
    Node* tail;

public:
    List() : head(nullptr), tail(nullptr) {}
    ~List() 
	{
        Node* current = head;
        while (current != nullptr) 
		{
            Node* next = current->next;
            delete current;
            current = next;
        }
    }

    void add(T value) 
	{
        Node* newNode = new Node(value);
        if (tail == nullptr) 
		{
            head = tail = newNode;
        } 
		else 
		{
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }

    void remove(T value) 
	{
        Node* current = head;
        while (current != nullptr) 
		{
            if (current->val == value) 
			{
                if (current->prev) 
				{
                    current->prev->next = current->next;
                } 
				else 
				{
                    head = current->next;
                }
                if (current->next) 
				{
                    current->next->prev = current->prev;
                } 
				else 
				{
                    tail = current->prev;
                }
                delete current;
                return;
            }
            current = current->next;
        }
        cout << "链表中未找到该值" << endl;
    }

    void update(int position, T value) 
	{
        Node* current = head;
        int index = 0;
        while (current != nullptr)
		{
            if (index == position) 
			{
                current->val = value;
                return;
            }
            current = current->next;
            index++;
        }
        cout << "下标越界" << endl; 
    }

    int find(T value) 
	{
        Node* current = head;
        int index = 0;
        while (current != nullptr) 
		{
            if (current->val == value) 
			{
                return index;
            }
            current = current->next;
            index++;
        }
		cout<<"找不到"<<endl;
        return -1;
    }

    void sort() 
	{
        if (head == nullptr || head->next == nullptr) return;

        bool swapped;
        do {
            swapped = false;
            Node* current = head;
            while (current->next != nullptr) {
                if (current->val > current->next->val) {
                    swap(current->val, current->next->val);
                    swapped = true;
                }
                current = current->next;
            }
        } while (swapped);
    }

    void show() 
	{
        Node* current = head;
        while (current != nullptr) 
		{
            cout << current->val << " ";
            current = current->next;
        }
        cout << endl;
    }
};

int main() {
    List<int> myList;

    myList.add(5);
    myList.add(3);
    myList.add(8);
    myList.add(1);
    myList.add(7);
    myList.add(4);
    myList.add(2);
    myList.add(6);

    myList.show();

    myList.sort();
    myList.show();

    cout << myList.find(8) << endl;

    myList.update(0, 10);
    myList.show();

    myList.remove(3);
    myList.show();

    return 0;
}

输出结果: