ZBLOG

使用c++代码实现以下要求 在日常生活中,有一些列表,比如待办清单或者歌单,我们经常会遇到一个需求,就是改变项目的顺序。一般来说,我们选中需要移动的条目,然后将它拖到我们想要的位置就可以啦! Pulpit rock 今天我们就使用单链表来实现这样一个功能吧! 先用C++实现含头结点的单链表,然后实现单链表中元素位置的移动。 注意不能改变每个结点包含的数据,必须通过修改指针来实现位置调换。 Input 第一行输入整数 n,表示链表中有n个数据; 第二行依次输入 n个数据,中间用空格分隔; 第...

下面是使用C++实现含头结点的单链表,并实现元素位置的移动的代码:

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

class LinkedList {
private:
    Node* head;

public:
    LinkedList() {
        head = new Node();
        head->next = nullptr;
    }

    ~LinkedList() {
        clear();
        delete head;
    }

    void insert(int val) {
        Node* newNode = new Node();
        newNode->data = val;
        newNode->next = head->next;
        head->next = newNode;
    }

    void move(int currentPos, int targetPos) {
        if (currentPos == targetPos)
            return;

        if (currentPos <= 0 || targetPos <= 0) {
            cout << "error" << endl;
            return;
        }

        // Find the nodes at current position and its previous position
        Node* currNode = head->next;
        Node* prevNode = head;

        for (int i = 1; i < currentPos; i++) {
            if (currNode == nullptr) {  // Invalid current position
                cout << "error" << endl;
                return;
            }
            prevNode = currNode;
            currNode = currNode->next;
        }

        // Find the node at target position and its previous position
        Node* targetPrevNode = head;

        for (int i = 1; i < targetPos; i++) {
            if (targetPrevNode == nullptr) {  // Invalid target position
                cout << "error" << endl;
                return;
            }
            targetPrevNode = targetPrevNode->next;
        }

        
         // Move the node from current position to target position
         prevNode->next = currNode->next;
         currNode->next = targetPrevNode->next;
         targetPrevNode->next = currNode;
    }

    void display() {
        Node* curr = head->next;

        while (curr != nullptr) {
            cout << curr->data << " ";
            curr = curr->next;
        }
        cout << endl;
    }

    void clear() {
        Node* curr = head->next;

        while (curr != nullptr) {
            Node* temp = curr;
            curr = curr->next;
            delete temp;
        }

        head->next = nullptr;
    }
};

int main() {
    int n, t, val, currentPos, targetPos;

    cin >> n;  // Number of data in the linked list

    LinkedList myList;

    for (int i = 0; i < n; i++) {
        cin >> val;  // Input the data
        myList.insert(val);
    }

    cin >> t;  // Number of moves

    myList.display();  // Output original list

    for (int i = 0; i < t; i++) {
        cin >> currentPos >> targetPos;  // Input move positions
        myList.move(currentPos, targetPos);
        myList.display();  // Output modified list after each move
    }

    return 0;
}

你可以使用这段代码实现所需功能,并根据输入输出样例进行测试。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9339

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?