doubt about displaying values and desfilerar in the Thread Queued?

4

How to display the correct values in the queue because it displays from the last to the first.

#include <iostream>
#include <cstdlib>

using namespace std;

struct Item
{
    int dado;
    Item *prox;
};
struct Fila
{
    Item *head;
    Item *tail;
    int tamanho;

};

Fila *fila = (Fila*)malloc(sizeof(Fila));

Fila *criaFila()
{
    fila->head = NULL;
    fila->tail = NULL;
    fila->tamanho =0;

    return fila;
}

void Enqueve (Fila *fila, int valor)
{
    Item *aux = (Item*)malloc(sizeof(Item));
    if(fila->head == NULL && fila->tail == NULL)
    {

        aux->dado = valor;
        aux->prox = fila->head;
        aux->prox = fila->tail;
        fila->head = aux;
        fila->tail = aux;
        fila->tamanho++;
    }
    else
    {
        aux->dado = valor;
        aux->prox = fila->tail;
        fila->tail = aux;
        fila->tamanho++;
    }
}
void exibeFila(Fila *fila)
{
    Item *aux;
    aux = fila->tail;
    while(aux != NULL)
    {
        cout << aux->dado << endl;
        aux = aux->prox;

    }
}


int main()
{
    Fila teste = *criaFila();

    Enqueve(&teste,10);
    Enqueve(&teste,17);
    Enqueve(&teste,8);
    exibeFila(&teste);
    return 0;
}
    
asked by anonymous 21.11.2015 / 16:00

1 answer

3

The stack is a data structure that provides the following operations:

push (enqueue) - Inserir um elemento no final (tail) da fila 
pop (dequeue)  - Remove um elemento do inicio (head) de uma fila não vazia
empty - Devolve um boleanon indicando se a pilha está vazia
size - Devolve o tamanho da fila

Your code contains a mixture of C (use of the malloc function) and C ++ (% with_%). I would suggest that you choose one of the two languages and change your code accordingly.

Here is an alternative version using C ++ that implements the methods indicated above:

#include <iostream>
#include <stdexcept>

class Fila {
public:

    struct Item {

            int dado;
            Item *prox;

        Item() 
            : dado(0)
            , prox(nullptr) {
        }

        Item(int valor, Item* p) 
            : dado(valor)
            , prox(p) {
        }

    };

    ~Fila() {
        Item* i = head;
        while( i != nullptr ) {
                Item* aux = i->prox;
                delete i;
            i = aux;
        }
        head = nullptr; 

    };

    bool empty() const { return elementos == 0; };
    size_t size() const { return elementos; };

    void push(int valor) {

        Item *i = new Item(valor, nullptr);

        if(head == nullptr) 
            head = i;
        else
            tail->prox = i;
        tail=i;
        ++elementos;
    };

    void pop() {
            if(head == nullptr)
                throw std::runtime_error("Accao invalida");

        Item* remove = head;
            head = head->prox;
            delete remove;

            --elementos;

    };
    //Nota: esta funcao nao deve fazer parte da API 
    void exibeFila() {

        Item *aux = head;
        while(aux != nullptr) {

            std::cout << aux->dado << std::endl;
            aux = aux->prox;
        }
    }

private:
    size_t elementos = 0;
    Item *head = nullptr;
    Item *tail = nullptr;

};

int main(int argc, char* argv[]) {

    Fila teste;

    teste.push(10);
    teste.push(17);
    teste.push(8);

    teste.exibeFila();    

    return 0;
}

The problem with your code was apparently in the functions classes, iostream and enqueue(Fila *fila, int valor) - The only functions I've changed. After my change they were like this;

void Enqueve (Fila *fila, int valor)
{
    Item *aux = (Item*)malloc(sizeof(Item));

    aux->dado = valor;
    aux->prox = NULL;

    if(fila->head == NULL)
    {
        fila->head = aux;
    }
    else
    {
    fila->tail->prox = aux;
    }

    fila->tail = aux;
    fila->tamanho++;
}
void exibeFila(Fila *fila)
{
    Item *aux;
    aux = fila->head;
    while(aux != NULL)
    {
        cout << aux->dado << endl;
        aux = aux->prox;

    }
}
    
21.11.2015 / 16:46