链接列表 push_back 在 C++ 中具有异常行为

Linked list push_back with an unusual behavior in c++

这是代码:

#include <iostream>
#include <string.h>
using namespace std;
class List;

class Node{
    char data;

public:
    Node(char d):data(d),next(NULL){} // INICIALIZAÇÃO CONHECIDA COMO: inicialization list
    Node* next;
    char getData(){
        return data;
    }
    ~Node(){
        if(next!=NULL){
            delete next; // é tipo uma chamada recursiva, vai deletando tudo.
        }
    }
    friend class List;
};

class List{
    Node * head;
    Node * tail;

public:

    Node * begin(){
        return head;
    }
    List():head(NULL),tail(NULL){}
    void push_front(char  data){
        if(head == NULL){
            Node * n = new Node(data);
            head = tail = n;
        }
        else{
            Node * n = new Node(data);
            n->next = head;
            head = n;
        }
    }
    void push_back(char data){
        if(head==NULL){
            Node * n = new Node(data);
            head = tail = n;
        }else{
            Node * n = new Node(data);
            tail->next = n;
            tail = n;
        }
    }
    void insert(char data, int pos){
        if(pos==0){
            push_front(data);
            return;
        }
        Node* temp = head;
        for(int jump = 1; jump <=pos-1;jump++){
            temp = temp->next;
        }
        Node *n = new Node(data);
        n->next = temp->next;
        temp->next = n;
    }

    ~List(){
        if(head!=NULL){
            delete head;
            head = NULL;
        }
    }
};

int main()
{

    List l;
    int i=0;
    char temp;
    char str[10000];
    int posx=0;

    while(scanf("%s", str) != '[=11=]'){
        while(str[i] != '[=11=]'){
                temp = str[i];
            if(str[i] == '['){
                while(str[i+1] != ']' && str[i+1] != '[' && str[i+1] != '[=11=]'){

                    i++;
                    temp = str[i];
                    l.insert(str[i], posx);
                    posx++;
                }
            }else if(str[i] == ']'){
                while(str[i+1] != ']' && str[i+1] != '[' && str[i+1] != '[=11=]'){
                    i++;
                    l.push_back(str[i]);
                }
            }else{
                l.push_back(str[i]);
            }
            i++;
            posx=0;
        Node *head = l.begin();
        while(head!=NULL){
            cout << head -> getData();
            head = head->next;
        }
        printf("\n");

        }


        i=0;
        l.~List();
    }

    return 0;
}

正直:程序得到一个字符串,如果用户输入'[',新字符(字母和下划线)将被添加到前面,如果用户输入']'则光标移动到返回。

问题是:如果我输入[_text_[rinti]has[_the_]],在到达之前有我有rinti_text_,没问题,但是当它到达has,函数 push_back 被调用,它简单地覆盖了单词 text,之后我将有 rinti_has,而不是 rinti_text_has。 我不知道发生了什么,但问题似乎出在函数 push_back 上。 我会很感激任何提示或答案

错误在您的 insert 函数中。如果您尝试在 List 的末尾插入,您永远不会更新 tail。无需过多修改代码的简单解决方案是检查 temp 是否等于 tail 并直接调用 push_back

此代码似乎适用于我的系统。

    void insert(char data, int pos){
        if(pos==0){
            push_front(data);
            return;
        }
        Node* temp = head;
        for(int jump = 1; jump <=pos-1;jump++){
            temp = temp->next;
        }
        if(temp == tail) {
            push_back(data);
        }
        else {
            Node *n = new Node(data);
            n->next = temp->next;
            temp->next = n;
        }
    }