Problems with insertion in list

Hi guys,
I'm doing a program in C + + that makes the registration of patients in an ordered list according to the ID of each patient.
In function registration, I can register the first patient, but the program gives execution failure when I try to register the other patients on the list.

I have one class to Patient and other list to List.

The function of impression the list is incorrect too.

Someone help me? Thanks

The code of theirs are:

Patient:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* 
 * File:   Paciente.h
 * Author: mutiley
 *
 * Created on 11 de Junho de 2012, 22:11
 */

#ifndef PACIENTE_H
#define	PACIENTE_H
#include "string.h"
#include "iostream"
using namespace std;

class Paciente {
private:
    string Nome;
    string SobreNome;
    int Prioridade; //prioridade de atendimento
    int ID; //código de cadastro do paciente; todo cliente possui um código diferente.
    // Usado para ordenação na lista de cadastro de atendimento

public:

    Paciente() {
    };

    Paciente(int id, string nome, string sobrenome, int prioridade) {
        this->Nome = nome;
        this->ID = id;
        this->SobreNome = sobrenome;
        this->Prioridade = prioridade;
    }

    string GetNome() {
        return Nome;
    }

    void SetNome(string Nome) {
        this->Nome = Nome;
    }

    string GetSobreNome() {
        return SobreNome;
    }

    void SetSobreNome(string SobreNome) {
        this->SobreNome = SobreNome;
    }

    int GetID() {
        return ID;
    }

    void SetID(int ID) {
        this->ID = ID;
    }

    int GetPrioridade() {
        return Prioridade;
    }

    void SetPrioridade(int Prioridade) {
        this->Prioridade = Prioridade;
    }

    void imprimirPaciente() {
        cout << "ID: " << ID;
        cout << "   Nome: " << Nome << " " << SobreNome;
        cout << "   Prioridade: " << Prioridade << endl;
    }
};

#endif	/* PACIENTE_H */


List.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* 
 * File:   Lista.h
 * Author: mutiley
 *
 * Created on 11 de Junho de 2012, 22:19
 */

#ifndef LISTA_H
#define	LISTA_H
#include "Paciente.h"
#include "string.h"
#include "iostream"

using namespace std;

//classe que representa uma lista com a relação de
//todos os atendimentos do hospital, portanto todo 
//paciente cadastrado deve ser atendido.

class Lista : public Paciente{
private:
    Paciente info;
    Lista *next;

public:

    Lista() {
        Paciente p0(0,"null", "null", 0);
        this->info = p0;
        this->next = NULL;
        
    }

    void cadastrar(Paciente, bool&); //cadastra um novo paciente
    void consultar(Paciente, bool&); //consulta se o paciente está cadastrado
    void imprimir(); //imprime toda a lista
    bool listaVazia(); //verifica se a lista está vazia
    void buscaLocalParaInsercao(); //encontra o local apropriado para cadastrar um novo paciente de acordo com a ID;

};


#endif	/* LISTA_H */


List.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Lista.h"
#include "Paciente.h"

bool Lista::listaVazia() {
    if (this->info.GetID() == 0) {
        return true;
    } else
        return false;
}

void Lista::cadastrar(Paciente P, bool &deuCerto) {
    Lista *aux, *proximo; //aux é o novo elemento a ser cadastrado e proximo um ponteiro para elementos da lista
    aux = new Lista();
    aux->info = P;
    aux->next = NULL;

    if (listaVazia()) {//lista vazia, insere na primeira posição
        this->info = aux->info;
        this->next = NULL;
        deuCerto = true;
    } else {
        proximo = this; //proximo aponta para primeiro elemento
        if (aux->info.GetID() < proximo->info.GetID()) {//id a ser inserido é menor do que o primeiro id da lista
            aux->next = proximo;
            proximo = aux;
            deuCerto = true;
        } else {//enquanto ID da lista for maior que ID a ser inserido, percorre a lista
            Lista *anterior = NULL;

            while (aux->info.GetID() > proximo->info.GetID()) {
                anterior = proximo;
                proximo = proximo->next;
            }

            if (proximo != NULL) {//ou seja, achou a posição e ela não é a última
                //verifica se são iguais. Se forem não cadastra
                if (aux->info.GetID() == proximo->info.GetID()) {
                    cout << "Ja esta cadastrado." << endl;
                    deuCerto = false;
                } else { //caso contrário cadastra
                    aux->next = proximo;
                    anterior->next = aux;
                    deuCerto = true;
                }
            } else {//insere na última posição da lista
                aux->next = NULL;
                anterior->next = aux;
                deuCerto = true;
            }
        }
    }
}

void Lista::consultar(Paciente p, bool &achou) {
    achou = false;
    Lista *aux, *auxLista;
    aux->info = P;
    auxLista = this;

    if (listaVazia())
        cout << "Nenhum paciente cadastrado na lista." << endl;
    else {
        while (auxLista->next != NULL && aux->info.GetID() != auxLista->info.GetID())
            aux = aux->next;
        if (aux->info.GetID() == auxLista->info.GetID())
            achou = true;
    }
}

void Lista::imprimir() {
    Lista *aux;
    aux = this;

    if (!listaVazia()) {
        while (aux->next != NULL) {
            cout << "as" << endl;
            cout << "ID: " << aux->info.GetID();
            cout << "    Prioridade: " << aux->info.GetPrioridade() << endl;
            cout << "    Nome: " << aux->info.GetNome();
            cout << " " << aux->info.GetSobreNome();
            aux = aux->next;
        }
    } else
        cout << "lista vazia" << endl;
}


main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* 
 * File:   main.cpp
 * Author: mutiley
 *
 * Created on 11 de Junho de 2012, 22:11
 */

#include <cstdlib>
#include "Lista.h"
#include "Paciente.h"
using namespace std;

int main(int argc, char** argv) {
    bool deuCerto;
    Paciente p1(1, "Arthur", "Giovanini", 1);
    Paciente p2(2, "Nome", "Sobrenome", 0);
    Paciente p3(3, "Nome", "Sobrenome", 0);
    Paciente p4(4, "Nome", "Sobrenome", 0);
 
    Lista *l = new Lista();

    l->cadastrar(p1, deuCerto);
    l->cadastrar(p2, deuCerto);
    l->cadastrar(p3, deuCerto);
    l->cadastrar(p4, deuCerto);

    l->imprimir();

    return 0;
}


Last edited on
Guys, I studied many times my source code and I think that the error is in the while of the function cadastrar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void Lista::cadastrar(Paciente P, bool &deuCerto) {
    Lista *aux, *proximo; //aux é o novo elemento a ser cadastrado e proximo um ponteiro para elementos da lista
    aux = new Lista();
    aux->info = P;
    aux->next = NULL;

    if (listaVazia()) {//lista vazia, insere na primeira posição
        this->info = aux->info;
        this->next = NULL;
        deuCerto = true;
    } else {
        proximo = this; //proximo aponta para primeiro elemento
        if (aux->info.GetID() < proximo->info.GetID()) {//id a ser inserido é menor do que o primeiro id da lista
            aux->next = proximo;
            proximo = aux;
            deuCerto = true;
        } else {//enquanto ID da lista for maior que ID a ser inserido, percorre a lista
            Lista *anterior = NULL;

                while (aux->info.GetID() > proximo->info.GetID()) {
                       anterior = proximo;
                       proximo = proximo->next;
                 }

            if (proximo != NULL) {//ou seja, achou a posição e ela não é a última
                //verifica se são iguais. Se forem não cadastra
                if (aux->info.GetID() == proximo->info.GetID()) {
                    cout << "Ja esta cadastrado." << endl;
                    deuCerto = false;
                } else { //caso contrário cadastra
                    aux->next = proximo;
                    anterior->next = aux;
                    deuCerto = true;
                }
            } else {//insere na última posição da lista
                aux->next = NULL;
                anterior->next = aux;
                deuCerto = true;
            }
        }
    }
}


Someone could explain what is wrong?
Last edited on
Redid the code of the function "register".
The code is inserting the code of ID of the patients at the beginning of the list, at the final of the list and in the middle of the list, but in the way as I did, when I inserts it in the middle of the list he too inserts it in final their.
Could anyone help me fix this error of logic?

Here's the new code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void Lista::cadastrar(Paciente P, bool &deuCerto) {
    Lista *aux, *proximo; //aux é o novo elemento a ser cadastrado e proximo um ponteiro para elementos da lista
    Lista *anterior;
    anterior = this;
    aux = new Lista();
    aux->info = P;
    aux->next = NULL;

    if (listaVazia()) {//lista vazia, insere na primeira posição
        this->info = aux->info;
        this->next = NULL;
        deuCerto = true;
        cout << "true no inicio" << endl;
    } else {
        proximo = this;
        if (aux->info.GetID() < proximo->info.GetID()) {//inserir à esquerda
            aux->next = proximo;
            proximo = aux;
            deuCerto = true;
        } else {//inserir à direita

            while (proximo->next != NULL) {
                if ((aux->info.GetID() > proximo->info.GetID()) && (aux->info.GetID() < proximo->next->info.GetID())) {
                    cout << "inseriu no meio" << endl;
                    aux->next = proximo->next;
                    proximo->next = aux;
                    deuCerto = true;
                } else {//andando pela lista
                    proximo = proximo->next;
                    anterior = anterior->next;

                }
            }
            if (proximo->next == NULL) {
                if (aux->info.GetID() == proximo->info.GetID()) {
                    deuCerto = false;
                    cout << "Já cadastrado" << endl;
                } else {
                    cout << "no fim" << endl;
                    proximo->next = aux;
                    deuCerto = true;
                }
            }

        }

    }
}
I solved the problem.
Topic archived. No new replies allowed.