Problem with Double Chained List

Asked

Viewed 55 times

-1

My function - for now - can create a list and add items at the beginning or end of the list. But when calling the add item function, selecting 1 or 2, and entering the item, the program simply terminates. I can’t find the problem, follow the code:

#include <bits/stdc++.h>
    using namespace std;

    struct node
    {
        int value;
        node *previous;
        node *next;
    };

    void CreatList(node *head, node *back){
        head = (node *) malloc(sizeof(node));
        back = (node *) malloc(sizeof(node));

        head->previous = NULL;
        head->next = back;
        back->previous = head;
        back->next = NULL;
    }

    int AddItem(node *head, node *back, int x, int comando){
        node *new_pointer = (node *) malloc(sizeof(node));
        if(new_pointer == NULL) return 0;
        new_pointer->value = x;

        if(comando == 1){
            new_pointer->next = head->next;
            new_pointer->next->previous = new_pointer;
            new_pointer->previous = head;
            head->next = new_pointer;
        }
        else if(comando == 2){
            new_pointer->next = back;
            new_pointer->previous = back->previous;
            new_pointer->previous->next = new_pointer;
            back->previous = new_pointer;
        }

        return 1;
    }

    void PrintList(node *head, node *back){
        node *pointer = head->next;
        while(pointer->next != NULL){
            cout << pointer->value << endl;
        }
    }

    int main(){

        node *head; node *back;
        CreatList(head, back);

        while(1){
            int comando, x;
            cout << "1- PrintList\n2- AddItem\n3- SearchList\n4- RemoveItem\n0- Exit\n";
            cout << "Comando: "; cin >> comando;

            if(comando == 0) return 0;
            else if(comando == 1){

                if(head->next->next == NULL);

            }
            else if(comando == 2){

                cout << "1- Inicio\n2- Fim\nComando: "; cin >> comando;
                cout << "Digite o item: "; cin >> x;
                int test = AddItem(head, back, x, comando);
                cout << "Sucess\n\n";

            }
            else if(comando == 3){


            }
            else if(comando == 4){


            }
            else cout << "Comando Invalido\n\n";
        }


        return 0;
    }

The program simply terminates. As in the image: inserir a descrição da imagem aqui

1 answer

0


Sorry I can not bring an explanation, I tried to follow your logic and found several passages that did not make sense, maybe you can use this code as reference.

using namespace std;

struct Node {
    int value;
    Node *previous;
    Node *next;
};

struct Lista {
    Node *inicio;
    Node *fim;
} lista;

void addItem(int x, int comandoInterno) {
    Node *new_pointer = new Node();
    new_pointer->value = x;

    if (lista.inicio == NULL) {
        lista.inicio = new_pointer;
        lista.fim = new_pointer;

        new_pointer->next = NULL;
        new_pointer->previous = NULL;

    } else if (comandoInterno == 1) {
        new_pointer->next = lista.inicio;
        new_pointer->previous = NULL;

        lista.inicio->previous = new_pointer;
        lista.inicio = new_pointer;

    } else {
        new_pointer->next = NULL;
        new_pointer->previous = lista.fim;

        lista.fim->next = new_pointer;
        lista.fim = new_pointer;
    }
}

void printList() {
    Node *pointer = lista.inicio;
    while (pointer != NULL) {
        cout << pointer->value << endl;
        pointer = pointer->next;
    }
}

int main() {

    int comando, comandoInterno, x;

    lista.inicio = NULL;
    lista.fim = NULL;

    while (1) {

        cout << "1- PrintList\n2- AddItem\n3- SearchList\n4- RemoveItem\n0- Exit\n";
        cout << "Comando: "; 
        cin >> comando;

        switch (comando) {
            case 0:
                return 0;
                break;

            case 1: 
                printList();
                break;

            case 2:
                cout << "1- Inicio\n2- Fim\nComando: ";
                cin >> comandoInterno;

                cout << "Digite o item: ";
                cin >> x;

                addItem(x, comandoInterno);
                cout << "Sucess\n\n";
                break;

            case 3:
                break;

            case 4:
                break;

            default:
                cout << "Comando Invalido\n\n";
        }
    }
    return 0;
}
  • Thank you, for some reason struct List is super necessary and I - as I am learning - did not even know what I should do. Solved the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.