What method can I do to invert the first element of the stack at the top of the stack?

1
   class Program
    {
        class Nodo
        {
            public int info;
            public Nodo sig;

        }

        private Nodo raiz;

        public Program()
        {
            raiz = null;
        }
        public void Insertar(int x)
        {
            Nodo nuevo;
            nuevo = new Nodo();
            nuevo.info = x;
            if (raiz == null)
            {
                nuevo.sig = null;
                raiz = nuevo;
            }
            else
            {
                nuevo.sig = raiz;
                raiz = nuevo;
            }


        }

        public int Extraer()

        {
            if (raiz != null)
            {
                int informacion = raiz.info;
                raiz = raiz.sig;
                return informacion;
            }
            else
            {
                return int.MaxValue;
            }

        }

        public void Imprimir()
        {
            Nodo reco = raiz;
            Console.WriteLine("Listado de todos los elementos de la pila.");
            while (reco != null)
            {
                Console.Write(reco.info + "-");
                reco = reco.sig;

            }

            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            int opcion = 0;

            Program pila1 = new Program();
            do
            {
                Console.WriteLine("Bienvenido al menù de opciones");
                Console.WriteLine("\n" +
                    "\n 1.- Insertar" +
                    "\n 2.- Extraer" +
                    "\n 3.- Imprimir" +
                    " \n 4.- Tope de la pila"+
                    " \n 5.- Primer elemento de la pila"+
                    " \n 6.- Imprimir del primer elemento al tope de la pila"+
                    " \n 7.-Salir  \n ");
                Console.WriteLine("Seleccione una opciòn");
                opcion = Convert.ToInt32(Console.ReadLine());

                switch (opcion)
                {
                    case 1:

                        Console.WriteLine("Inserte un número.");
                        int x = Convert.ToInt32(Console.ReadLine());
                        pila1.Insertar(x );
                        pila1.Imprimir();
                        break;
                    case 2:
                        Console.WriteLine("Extraemos de la pila:" + pila1.Extraer());
                        pila1.Imprimir();
                        break;
                    case 3:

                        pila1.Imprimir();
                        break;

                    case 6: 

                    case 7:
                        Console.OpenStandardOutput();
                        break;


                }

            } while (opcion != 4);
            Console.ReadKey();
        }

    }
    
asked by Mari Paz 17.04.2018 в 01:46
source

2 answers

0

The method would be similar to your Print method:

public void Invertir()
{
    Nodo reco = raiz;
    Nodo actual = raiz;
    Console.WriteLine("Listado de todos los elementos de la pila.");
    while (reco != null)
    {            
        actual = reco;
        reco = reco.sig;
    }

    Console.WriteLine(actual.info + "-");
    reco = raiz;

    while (reco != null)
    {    
        if (reco.sig != null) {
            Console.Write(reco.info + "-");
        }
        reco = reco.sig;
    }
}
    
answered by 17.04.2018 в 02:04
0

If the problem is just having the stop element inverted, only the stop element, you could do the following:

public void Insertar(int x)
{
    Nodo nuevo;
    nuevo = new Nodo();
    nuevo.info = x * -1; //De esta manera aseguraremos que el elemento del tope siempre esté invertido
    if(raiz == null)
    {
        nuevo.sig = null;
        raiz = nuevo;
    }
    else
    {
        raiz.info = raiz.info * - 1; //como ya no será el elemento del tope lo devolvemos a su valor original
        nuevo.sig = raiz;
        raiz = nuevo;
    }
}

In this way what would happen is the following:

1.-We insert the number 3:

pila:
-3  -> raiz

2.-Insert the number -5:

pila:
5   -> raiz
3

3.-We insert the number 4:

pila:
-4  -> raiz
-5
 3

And so on.

I hope it serves you!

    
answered by 17.04.2018 в 18:58