Help to add an option to a menu

0

Hello good night or early morning. You can help me know that maybe it's something obvious but I need to add option 5 to my program but I can not think of how to do it. Help please.

This is the code, I've already put the option Mostar capped but I do not know what to do anymore:

#include<iostream>

using namespace std;

struct nodo
{
    int numero;
    struct nodo *siguiente;
};

typedef nodo *ptrPila;

void imprimir (ptrPila);
void eliminaPila (ptrPila &);
void push (ptrPila &, int);
int pop (ptrPila &);
void mostrartope (ptrPila &)

int main()
{
    ptrPila p=NULL;
    int opcion,n;
    do
    {
        cout<<endl<<"\tImplementacion de una Pila"<<endl;




        cout<<"1) Apilar" <<endl;
        cout<<"2) Desapilar" <<endl;
        cout<<"3) Ver Pila" <<endl;
        cout<<"4) Destruir Pila" <<endl;
        cout<<"5) Mostrar Tope" <<endl;
        cout<<"6) Salir" <<endl;
        cout<<"Elegir una Opcion"<<endl;
        cin>>opcion;

        switch(opcion)

        {
        case 1: cout<<"Aplicar valor:";cin>>n;
        push (p,n);
        break;

        case 2: if(p !=NULL)
        {
            cout<<"Valor Eliminado";
            n=pop(p);
            cout<<n<<endl;
        }
        else
            cout<<"Pila vacia"<<endl;
        break;

        case 3: cout<<"Contenido de pila:"<<endl;
        imprimir (p);
        break;

        case 4: eliminaPila (p);
        cout<<"Pila destruida"<<endl;
        break;

        case 5: cout<<"Mostrar tope:"; cin>>n;
        imprimir (n);
        break;

    }
} while (opcion !=6);
return 0;
}

void push(ptrPila &p, int n)
{
    ptrPila q = new (struct nodo);
    q->numero =n;
    q->siguiente =p;
    p = q;
}

int pop (ptrPila &p)
{
    int n = p->numero;
    ptrPila q=p;
    p=p->siguiente;
    delete(q);
    return n;
}

void eliminaPila (ptrPila &p)
{
    ptrPila q;

    while(p!=NULL)
    {
        q=p;
        p=p->siguiente;
        delete(q);
    }
}

void imprimir(ptrPila p)
{
    while (p!=NULL)
    {
        cout<<p->numero<<endl;
        p=p->siguiente;
    }
    cout<<endl;

void mostartope 
}
    
asked by Ricardo Rf 20.10.2017 в 09:29
source

1 answer

2

Surely it's hard to see what you have to do because your program is confusing. I will advise you some things to improve readability.

In C ++, struct is not part of the type.

In the C language, it is necessary to prepend struct before the name of a structure, because struct is part of the data type, but in C ++ this is not the case, therefore you can change your node declaration to :

struct nodo
{
    int numero;
    nodo *siguiente; // No lleva 'struct' antes de 'nodo'.
};

You confuse nodes with batteries.

One of the first things you do with your type nodo is to create an alias to pointer it:

typedef nodo *ptrPila;

The problem is that you call the pointer to a node as a pointer to a stack and a node is not a stack ... it's as if you said that a step is a staircase: the stairs are made of steps but the steps are not stairs. I advise you to declare a Pila object that handles the nodes.

An object Pila .

Your Pila can contain all the functions you have defined loose:

struct Pila
{
    void imprimir ();
    void eliminaPila ();
    void push (int);
    int pop ();
    void mostrartope ()

private:
    nodo *tope = nullptr; // el tope de la pila.
};

If you notice, I have removed all parameters of type ptrPila of the functions, simply because the stack already stores the node and therefore does not need to receive it as a parameter.

Definitions change slightly.

Now the functions belong to the Pila object, and they do not receive an external node but they use the one belonging to the function, so the definitions will change as well:

//   vvvvvv <--- La funcion push pertenece a Pila
void Pila::push(int n)
{
    nodo *nuevo_nodo = new nodo; // No lleva 'struct' antes de 'nodo'.
    //    ^^^^^^^^^^ <--- El nombre 'nuevo_nodo' es mas claro que 'q'.

    nuevo_nodo->numero =n;
    nuevo_nodo->siguiente =tope;
    //                     ^^^^ <--- Usamos el nodo tope, no uno que recibimos por parametros
    tope = nuevo_nodo;
}

You do not need to put struct before a structure type because we are in C ++. Avoid the names of a single letter, because they are confusing. This data stacking algorithm is placing nodes under of the top node:

//  vvvvvv <--- La funcion pop pertenece a Pila
int Pila::pop ()
{
    int n = 0;
    //      ^^^^ <--- Usamos el nodo tope, no uno que recibimos por parametro
    if (nodo *antiguo_tope = tope)
    //        ^^^^^^^^^^^^ <--- El nombre 'antiguo_tope' es mas claro que 'q'.
    {
        tope = antiguo_tope->siguiente
        n = antiguo_tope->numero;
        delete antiguo_tope; // delete no es una funcion, no requiere parentesis
    }

    return n;
}

Your unstack algorithm moves the stop one position down and clears the old stop. Do not use parentheses to call delete because it is an operator, not a function. Unstack only if the cap is not a null pointer.

//   vvvvvv <--- La funcion eliminaPila pertenece a Pila
void Pila::eliminaPila ()
{
    //    vvvv <--- El 'tope' de esta pila.
    while(tope!=nullptr)
    //          ^^^^^^^ <-- nullptr es el literal de puntero nulo de C++
    {
        pop(); // Ya tienes una funcion que borra el tope de la pila, usala.
    }
}

In C ++ the macro NULL is not used to refer to a null pointer, the literal nullptr is used which is more appropriate. On the other hand you do not need to duplicate the deletion logic of nodes since you already have it implemented in the unstack function, go unstacking data until the stack is empty.

//   vvvvvv <--- La funcion imprimir pertenece a Pila
void Pila::imprimir()
{
    for (nodo *actual = tope; actual != nullptr; actual = actual->siguiente)
    {
        cout<<actual->numero<<endl;
    }
    cout<<endl;
}

Remember, do not use one-letter names, it's confusing. Change NULL by nullptr . And your function of mostartope , because it is so simple (with the right names) as showing the top:

//   vvvvvv <--- La funcion imprimir pertenece a Pila
void Pila::mostartope()
{
    cout << tope->numero << endl;
}

Conclusion.

You were not understanding your algorithm, you always had the top of your stack in sight.

    
answered by 22.10.2017 в 14:24