Home > Enterprise >  Linked list push_back with an unusual behavior in c
Linked list push_back with an unusual behavior in c

Time:03-02

That's the code:

#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) != '\0'){
        while(str[i] != '\0'){
                temp = str[i];
            if(str[i] == '['){
                while(str[i 1] != ']' && str[i 1] != '[' && str[i 1] != '\0'){

                    i  ;
                    temp = str[i];
                    l.insert(str[i], posx);
                    posx  ;
                }
            }else if(str[i] == ']'){
                while(str[i 1] != ']' && str[i 1] != '[' && str[i 1] != '\0'){
                    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;
}

Being straight: The program gets a string, if the user types '[' the new characters (letters and underscores) will be added to the front, if the user inputs ']' then the cursor will move to the back.

The problem is: if I input [_text_[rinti]has[_the_]], before reaching has I have rinti_text_, and it's fine, but when it gets to has, the function push_back is called and it simply overwrites the word text, and after that I'll have rinti_has, instead of rinti_text_has. I do not know whats happening, but the problem seem to be with the function push_back. I would appreacite any hints or answers

CodePudding user response:

The bug is in your insert function. If you try to insert at the end of your List, you never update tail. A simple solution without modifying your code too much is to check if temp is equal to tail and just call push_back directly.

This code seems to work on my system.

    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;
        }
    }
  • Related