Question with dynamic lists

0

In case I need to generate 5 random numbers, and insert them into a dynamic list, I have been trying, but the maximum I can is to generate and display them

follow code:

#include <iostream>
#include <cstdlib>//Biblioteca responsavel por numeros aleatorios
#include <ctime>//Biblioteca responsavel por numeros aleatorios
#include <list>//Biblioteca responsavel por criar listas

#define PCT 5
using namespace std;

typedef struct Album{
    int figura;
    Album *prox;
/*  list<int> album;
    int tam;

    tam=681;

    for(int i=tam; i>0; i--){
    album.push_front(i);
    }
    cout<<"Album da copa 2018 - total "<<album.size()<<" figurinhas"<<endl<<endl;

    tam=album.size();
    for(int i=0; i < tam; i++){
        cout<<album.front()<<endl;
        album.pop_front();
    }*/
};
    Album *inicio = NULL;
    Album *fim = NULL;
    Album *aux;
    Album *anterior;

void Pacote(){
    system("cls");

    srand((unsigned)time(0)); //para gerar numeros aleatorios reais.
    for (int i=0; i<PCT; i++){
        int maior = 681;
        int menor = 1;
        int Figurinha = rand()%(maior-menor+1) + menor;
        cout << Figurinha << endl;  }
        //Valida();

    Album *novo = new Album();
    cin>>novo->figura;
        if (inicio==NULL){
            inicio = novo;
            novo->prox=NULL;
        }
        else{
            anterior=NULL;
            aux=inicio;
            while (aux != NULL && novo -> figura > aux->figura){
                anterior = aux;
                aux = aux->prox;
            }
            if (anterior==NULL){
                novo->prox=inicio;
                inicio=novo;
            }
            else if (aux==NULL){
                fim->prox=novo;
                fim = novo;
                fim->prox=NULL;
            }
            else{
                anterior->prox=novo;
                novo->prox=aux;
            }


        }
}


/*void Valida(int Figurinha, list<int> album){
    if (Figurinha ){

    }*/

void Menu(){
    int x=0;
    system("color F0");
    do{
        cout<<"==========ALBUM DA COPA========="<<endl;
        cout<<"1 - Inserir novas figurinhas"<<endl;
        cout<<"2 - Exibir album"<<endl;

        cout<<endl<<endl<<"Selecione uma das opcoes acima :"<<endl;
        cin>>x;

        switch (x){
            case 1:
                Pacote();
                break;
            case 2:
                Album();
                break;
        }
    }while (x!=5);
}

int main(int argc, char** argv) {
    Menu();
    return 0;
}
    
asked by anonymous 13.06.2018 / 05:01

1 answer

0

You can do as below:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <utility>
#include <list>

int main() {
    std::srand(std::time(nullptr));
    int quantidade = 5;
    int cap = 682;
    std::list<int> lista;
    float rand_number;
    for(int i=0; i<quantidade; ++i) {
        rand_number = rand()%cap;
        lista.push_front(std::move(rand_number));
    }
    for (const int& l : lista)
        std::cout << l << ' ';
    std::cout << '\n';
    return 0;
}
    
15.06.2018 / 19:47