Double-chained List Problem

-1

My function - for now - can create a list and add items at the beginning or end of the list. However, when calling the add item function, selecting 1 or 2, and typing the item, the program simply exits. I can not find what the problem is, 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 picture:

    
asked by anonymous 31.12.2018 / 05:54

1 answer

0

I'm sorry I could not explain, I tried to follow your logic and I found a lot of bits that did not make sense, maybe you could use that code as a 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;
}
    
31.12.2018 / 07:44