首先,根据要求设计一个单链表的类,包括链表的节点类和链表类。

节点类的设计如下:

class Node {
public:
    int data;
    Node* next;
};

链表类的设计如下:

class LinkedList {
private:
    Node* head;
public:
    LinkedList();
    ~LinkedList();
    void insert(int data);
    void remove(int data);
    void traverse();
    Node* search(int data);
    void merge(LinkedList& list);
};

接下来,实现建立单链表、向单链表中输入数据以及遍历、查找、新元素插入、元素删除等功能的函数。

建立单链表的函数如下:

void LinkedList::insert(int data) {
    Node* newNode = new Node;
    newNode->data = data;
    newNode->next = nullptr;

    if (head == nullptr) {
        head = newNode;
    } else {
        Node* current = head;
        while (current->next != nullptr) {
            current = current->next;
        }
        current->next = newNode;
    }
}

向单链表中输入数据的函数如下:

void LinkedList::input() {
    int data;
    cout << '请输入数据(以-1结束输入):' << endl;
    while (true) {
        cin >> data;
        if (data == -1) {
            break;
        }
        insert(data);
    }
}

遍历单链表的函数如下:

void LinkedList::traverse() {
    Node* current = head;
    while (current != nullptr) {
        cout << current->data << ' ';
        current = current->next;
    }
    cout << endl;
}

查找元素的函数如下:

Node* LinkedList::search(int data) {
    Node* current = head;
    while (current != nullptr) {
        if (current->data == data) {
            return current;
        }
        current = current->next;
    }
    return nullptr;
}

新元素插入的函数如下:

void LinkedList::insert(int data, int position) {
    Node* newNode = new Node;
    newNode->data = data;
    newNode->next = nullptr;

    if (position == 0) {
        newNode->next = head;
        head = newNode;
    } else {
        Node* current = head;
        for (int i = 0; i < position - 1; i++) {
            if (current->next == nullptr) {
                break;
            }
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
}

元素删除的函数如下:

void LinkedList::remove(int data) {
    if (head == nullptr) {
        return;
    }

    if (head->data == data) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return;
    }

    Node* current = head;
    while (current->next != nullptr) {
        if (current->next->data == data) {
            Node* temp = current->next;
            current->next = current->next->next;
            delete temp;
            return;
        }
        current = current->next;
    }
}

最后,实现将两个有序单链表合并为一个有序单链表的函数。

void LinkedList::merge(LinkedList& list) {
    Node* current1 = head;
    Node* current2 = list.head;
    LinkedList mergedList;

    while (current1 != nullptr && current2 != nullptr) {
        if (current1->data < current2->data) {
            mergedList.insert(current1->data);
            current1 = current1->next;
        } else {
            mergedList.insert(current2->data);
            current2 = current2->next;
        }
    }

    while (current1 != nullptr) {
        mergedList.insert(current1->data);
        current1 = current1->next;
    }

    while (current2 != nullptr) {
        mergedList.insert(current2->data);
        current2 = current2->next;
    }

    head = mergedList.head;
}

以上就是根据要求设计和实现的单链表相关功能的代码。根据具体的问题分析和测试要求,可以使用这些函数来进行单链表的建立、遍历、查找、新元素插入、元素删除以及两个有序单链表的合并等操作。根据具体需求,可以编写相应的输入和输出内容。

C++ 单链表实现:建立、遍历、查找、插入、删除及合并操作

原文地址: https://www.cveoy.top/t/topic/uQN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录