Stacks and queues in C ++?

-1

I have this code in which it is to verify queue data in C ++ Codeblocks:

#include <iostream>

using namespace std;

class nodo {
 public:
 nodo(int v, nodo *sig = NULL) {
 valor = v;
 siguiente = sig;
 }
 private:
 int valor;
 nodo *siguiente;

 friend class cola;
};
typedef nodo *pnodo;
class cola {
 public:
 cola() : ultimo(NULL), primero(NULL) {}
 ~cola();

 void Push(int v);
 int Pop();
 private:
 pnodo ultimo;
};
cola::~cola() {
 while(primero) Leer();
}
void cola::Anadir(int v) {
 pnodo nuevo;
 /* Crear un nodo nuevo */
 nuevo = new nodo(v);
 /* Si la cola no estaba vacía, añadimos el nuevo a continuación de
ultimo */
 if(ultimo) ultimo->siguiente = nuevo;
 /* Ahora, el último elemento de la cola es el nuevo nodo */
 ultimo = nuevo;
 /* Si primero es NULL, la cola estaba vacía, ahora primero apuntará
también al nuevo nodo */
 if(!primero) primero = nuevo;
}
int cola::Leer() {
 pnodo nodo; /* variable auxiliar para manipular nodo */
 int v; /* variable auxiliar para retorno */

 /* Nodo apunta al primer elemento de la pila */
 nodo = primero;
 if(!nodo) return 0; /* Si no hay nodos en la pila retornamos 0 */
 /* Asignamos a primero la dirección del segundo nodo */
 primero = nodo->siguiente;
 /* Guardamos el valor de retorno */
 v = nodo->valor;
 /* Borrar el nodo */
 delete nodo;
 /* Si la cola quedó vacía, ultimo debe ser NULL también*/
 if(!primero) ultimo = NULL;
 return v;
}
int main() {
 cola Cola;
 Cola.Anadir(20);
 cout << "Añadir(20)" << endl;
 Cola.Anadir(10);
 cout << "Añadir(10)" << endl;
 cout << "Leer: " << Cola.Leer() << endl;
 Cola.Anadir(40);
 cout << "Añadir(40)" << endl;
 Cola.Anadir(30);
 cout << "Añadir(30)" << endl;
 cout << "Leer: " << Cola.Leer() << endl;
 cout << "Leer: " << Cola.Leer() << endl;
 Cola.Anadir(90);
 cout << "Añadir(90)" << endl;
 cout << "Leer: " << Cola.Leer() << endl;
 cout << "Leer: " << Cola.Leer() << endl;
 cin.get();
 return 0;
}

The problem it gives me is that compiling it gives me this error. "Error class queue does not have any field name first. I already verified and I can not get the program to run.

    
asked by José T. 27.04.2017 в 23:44
source

1 answer

1

As they have already written in a comment, the first error that appears in compilation is because the compiler does not find a member named primero within the class cola . That is why you must add the declaration of that node, in the same way that you have ultimo . However, the same thing will happen once you fix that error with the methods you have implemented below. That is, you have some methods of the class cola implemented ( Leer and Anadir(int v) ) but you do not have them defined in that class.

In short, this would be cola :

typedef nodo *pnodo;
class cola {
    private:
        pnodo primero;
        pnodo ultimo;
    public:
        cola() : primero(NULL), ultimo(NULL) {}
        int Leer();
        void Anadir(int v);
        ~cola();
        void Push(int v);
        int Pop();
};
    
answered by 30.04.2017 / 11:18
source