I have made this program that is a stack. In this case I decided to make a menu in which I called the methods but when it comes to calling them it does not work for me, it makes me wrong all the cases.
I think my mistake is when calling the methods but I do not understand what my error is. I would appreciate a help, thanks. (It is not necessary that I solve it well but I would like to understand why I do it wrong and thus not make mistakes again.)
import java.util.Scanner;
public class Pila_1 {
class Nodo{
int info;
Nodo sig;
}
private Nodo raiz;
public Pila_1(){
raiz=null;
}
public void insertar(int x){
Nodo nuevo;
nuevo=new Nodo();
nuevo.info=x;
if(raiz!=null){
nuevo.sig=raiz;
raiz=nuevo;
}
else{
nuevo.sig=null;
raiz=nuevo;
}
}
public int extraer(){
if(raiz!=null){
int informacion=raiz.info;
raiz=raiz.sig;
return informacion;
}else{
return Integer.MAX_VALUE;
}
}
public void imprimir(){
Nodo recorrer=raiz;
while(recorrer!=null){
System.out.println(recorrer.info+"---");
recorrer=recorrer.sig;
}
}
public static void main(String[] args) {
Scanner entrada=new Scanner(System.in);
int op;
System.out.println("Que opcion desea tomar: 1-Insertar 2-Extraer 3-Imprimir");
op=entrada.nextInt();
do{
switch(op){
case 1:
System.out.println("Declara una variable a introducir :");
int variable;
variable=entrada.nextInt();
insertar(variable);
break;
case 2:
extraer();
break;
case 3:
imprimir();
break;
}
}while(op==1||op==2||op==3);
}
}