Sorted list C++ struct

Asked

Viewed 803 times

0

I need to do a job with this struct and that contains insertion, search and exclusion by year and printing. Only that I am doubtful in the following code. I need right after insertion to be ordered.

struct mundial{
   int ano;
   char sede[10];
   char campeao[10];
struct mundial *prox;
};
struct mundial *inicio;
void iniciaLista (){
    inicio = NULL;
}
bool testaListaVazia (){
     return (inicio == NULL);
}
void insereLista(int dado1, char dado2, char dado3){
     struct mundial *pt;
     pt = new struct mundial;
     pt -> ano = dado1; 
     pt -> sede[10] = dado2;
     pt -> campeao[10] = dado3;
     pt -> prox = NULL;
     if(testaListaVazia()){
       inicio = pt;
     }else {
       pt -> prox = inicio;
       inicio = pt;
     }
       return 1;
}
void viewInsere(){
     int t,x;
     char sd[10];
     char cp[10];
     cout<<"\nDigite o ano:";
     cin>> t;
     cout << "\n Digite a sede:";
     cin >> sd[10];
     cout << "\nDigite o campeao:";
     cin >> cp[10];
     x = insereLista(t,sd,cp);
     if (x!=1) {
        cout<<"Erro na insercao";
     }else {
              cout<<" inserido com sucesso!";
     }
 }

What I’m doing wrong?

When I get to headquarters he closes the program

void viewInsere(){
     int t,x;
     char *sd;
     char *cp;
     cout<<"\nDigite o ano:";
     cin>> t;
     cout << "\nDigite a sede:";
     cin >> *sd;
     cout << "\nDigite o campeao:";
     cin >> *cp;
     x = insereLista(t, sd, cp);
     if (x!=1) {
        cout<<"Erro na insercao";
     }else {
              cout<<" inserido com sucesso!";
     }
 }
  • Possible duplicate of Sorted list with struct C++

  • There’s a lot of things wrong with the code, instead of char dado2, char dado3 should be const char *dado2, const char *dado3, and instead of using [10], use pointers *

  • explains me better this instead use [10] to use pointers, I did not understand very well

  • I’m writing the example

1 answer

2


There are some things wrong with your code, the unordered list would be so

struct mundial {
    int ano;
    const char* sede; //usar ponteiro em vez de array
    const char* campeao; //usar ponteiro em vez de array
    struct mundial *prox;
};

struct mundial *inicio = NULL;

bool insereLista(int dado1,const char *dado2, const char *dado3) { //usar const char*
    struct mundial *pt;
    pt = new struct mundial;
    pt->ano = dado1;
    pt->sede = dado2;
    pt->campeao = dado3;
    pt->prox = NULL;
    if (testaListaVazia()) {
        inicio = pt;
    } else {
        pt->prox = inicio;
        inicio = pt;
    }
    return true;
}

void main() {
    insereLista(2018, "Unesp", "Diogo");
    insereLista(2014, "USP", "Luiz");
    insereLista(2016, "Unicamp", "Carol");
    insereLista(2020, "Uninter", "Beatriz");
}

For the main code, the result would be

2020 - Uninter - Beatriz
2016 - Unicamp- Carol
2014 - USP- Luiz
2018 - Unesp- Diogo

To the orderly list that would be it:

bool insereLista(int dado1,const char *dado2, const char *dado3) {
    struct mundial *pt;
    pt = new struct mundial;
    pt->ano = dado1;
    pt->sede = dado2;
    pt->campeao = dado3;
    pt->prox = NULL;

    struct mundial *leitor = inicio;
    struct mundial *anterior = nullptr;

    while (leitor != nullptr && leitor->ano < dado1) {
        anterior = leitor;
        leitor = leitor->prox;
    }

    if (leitor != nullptr) {
        if (anterior == nullptr) {
            pt->prox = inicio;
            inicio = pt;
        } else {
            anterior->prox = pt;
            pt->prox = leitor;
        }
    } else {
        if (anterior == nullptr) {
            inicio = pt;
        } else {
            anterior->prox = pt;
        }
    }

    return true;
}

The result would be ordered:

   2014 - USP- Luiz
   2016 - Unicamp- Carol
   2018 - Unesp- Diogo
   2020 - Uninter - Beatriz

To print you can do the following (for iostream):

void print() {
    struct mundial *leitor = inicio;
    while (leitor != nullptr) {
        cout << leitor->ano << " - " << leitor->sede << " - " << leitor->campeao << endl;
        leitor = leitor->prox;
    }
}

or (for stdafx. h)

void print() {
    struct mundial *leitor = inicio;
    while (leitor != nullptr) {
        printf("%d - %s - %s\n", leitor->ano, leitor->sede, leitor->campeao);
        leitor = leitor->prox;
    }
}

Using your Viewinsere you could use the following for 4 entries, for example:

void viewInsere() {
    int t, x;

    char *sd = (char*)malloc(sizeof(char) * 10);
    char *cp = (char*)malloc(sizeof(char) * 10);
    std::cout << "\nDigite o ano:";
    std::cin >> t;
    std::cout << "\nDigite a sede:";
    std::cin >> sd;
    std::cout << "\nDigite o campeao:";
    std::cin >> cp;
    x = insereLista(t, sd, cp);
    if (x != 1) {
        std::cout << "Erro na insercao";
    } else {
        std::cout << " inserido com sucesso!";
    }
}

void main() {
    viewInsere();
    viewInsere();
    viewInsere();
    viewInsere();

    print();

    system("pause");
}

Source:

Cadilag

  • aaaaaaaaah got it, help me out on one more thing, how would I make the viewInsere in this style? Because I did and when he arrives at headquarters, he closes the program and does not ask me the champion

  • I’ll edit the viewInsere to take a look

  • the viewInsere will use the same functions as the main

  • He doesn’t go past headquarters, he shuts down the program

  • edited the question with my function, see if Voce discovers the error

  • When you use pointer, before reading you need to allocate the string

  • THANKS, HELPED A LOT. I already managed to do the other functions

Show 2 more comments

Browser other questions tagged

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