链表维护(C++)

链表的增删改查功能,用于调用维护链表。

#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class myList {
    ListNode* head;
    ListNode* tail;
    int count;
public:
    myList();
    ~myList();
    int append(int value);
    int append(ListNode& newNode);
    int insert(int value, int index);
    int insert(ListNode& newNode, int index);
    int& operator[](int i) const;
    int remove(int index);
    int find(int value) const;  
    void print() const;  // 打印链表
};

class solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode dummy(0);
        ListNode* current = &dummy;

        while (list1 != nullptr && list2 != nullptr) {
            if (list1->val < list2->val) {
                current->next = list1;
                list1 = list1->next;
            }
            else {
                current->next = list2;
                list2 = list2->next;
            }
            current = current->next;
        }

        if (list1 != nullptr) {
            current->next = list1;
        }
        else {
            current->next = list2;
        }

        return dummy.next;
    }
};

void printlist(ListNode* node) {
    while (node != nullptr) {
        cout << node->val << " ";
        node = node->next;
    }
    cout << endl;
}

int main() {
    ListNode* list1 = new ListNode(1);
    list1->next = new ListNode(2);
    list1->next->next = new ListNode(3);

    ListNode* list2 = new ListNode(1);
    list2->next = new ListNode(3);
    list2->next->next = new ListNode(4);

    solution sol;
    ListNode* list3 = sol.mergeTwoLists(list1, list2);
    printlist(list3);

    return 0;
}

myList::myList() : head(nullptr), tail(nullptr), count(0) {}

myList::~myList() {
    ListNode* current = head;
    while (current != nullptr) {
        ListNode* nextNode = current->next;
        delete current;
        current = nextNode;
    }
}

int myList::append(int value) {
    ListNode* temp = new ListNode(value);
    return append(*temp);
}

int myList::append(ListNode& newNode) {
    if (head == nullptr) {
        head = &newNode;
        tail = head;
    }
    else {
        tail->next = &newNode;
        tail = tail->next;
    }
    count++;
    return count;
}

int myList::insert(int value, int index) {
    ListNode* temp = new ListNode(value);
    return insert(*temp, index);
}

int myList::insert(ListNode& newNode, int index) {
    if (index < 0 || index > count) {
        return -1;
    }

    if (index == 0) {
        newNode.next = head;
        head = &newNode;
        if (tail == nullptr) {
            tail = head;
        }
    }
    else {
        ListNode* current = head;
        for (int i = 1; i < index; ++i) {
            current = current->next;
        }
        newNode.next = current->next;
        current->next = &newNode;
        if (newNode.next == nullptr) {
            tail = &newNode;
        }
    }

    count++;
    return count;
}

int& myList::operator[](int i) const {
    if (i < 0 || i >= count) {
        throw out_of_range("Index out of range");
    }
    ListNode* current = head;
    for (int j = 0; j < i; ++j) {
        current = current->next;
    }
    return current->val;
}

int myList::remove(int index) {
    if (index < 0 || index >= count) {
        return -1;
    }

    ListNode* temp;
    if (index == 0) {
        temp = head;
        head = head->next;
        if (head == nullptr) {
            tail = nullptr;
        }
    }
    else {
        ListNode* current = head;
        for (int i = 1; i < index; ++i) {
            current = current->next;
        }
        temp = current->next;
        current->next = temp->next;
        if (temp->next == nullptr) {
            tail = current;
        }
    }

    delete temp;
    count--;
    return count;
}

int myList::find(int value) const {
    ListNode* current = head;
    int index = 0;
    while (current != nullptr) {
        if (current->val == value) {
            return index;
        }
        current = current->next;
        index++;
    }
    return -1;  // 如果找不到,返回-1
}

void myList::print() const {
    ListNode* current = head;
    while (current != nullptr) {
        cout << current->val << " ";
        current = current->next;
    }
    cout << endl;
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值