Data Structures
Hello all!
I've got this problem with 2 Data Structures, Stack and Linked List.
umm i'm gonna post both programs that way i dont have to open another post..
First the linked list: This program has to use a linked list to add students, then, then once that student is added, you have to be able to delete any student. However, when i try to delete the student, it will throw an indexoutofboundexception..
import java.util.*;
publicclass Uno{
publicstatic String insertar(){
Scanner sc =new Scanner(System.in);
LinkedList lista =new LinkedList();
System.out.println("Inserte nuevo estudiante");
lista.add("Dionis Matos/2002-0824/ISC");
lista.add(sc.next());
lista.add("Perensejo de Tal/2003-1717/MER");
System.out.println(lista);
return" ";
}
publicstaticvoid borrar(){
Scanner sc =new Scanner(System.in);
LinkedList lista =new LinkedList();
System.out.println("Desea borrar un estudiante de la lista? s/n");// here i ask the user if he/she wants to delete a student from the list
String x = sc.next();
int y;
if(x.equals("s")){
System.out.println("Digite el numero del estudiante a borrar: 0 al 2");
y = sc.nextInt();
if(y == 0){
lista.remove(0);
System.out.println(lista);
}
elseif(y == 1 ){
lista.remove(1);
System.out.println(lista);
}
elseif(y == 2){
lista.remove(2);
System.out.println(lista);
}
}
else{
System.out.print("No se borro ningun estudiante" +" " + lista);
}
}
publicstaticvoid main(String args[]){
insertar();
borrar();
}
}
Now on to the Stack: The stack problem should be doing exactly the same thing as the list. but it also has to determine if the Stack is empty.. if its empty then add elementes.. and also let the user delete an element.
import java.util.*;
publicclass Cuatro{
publicstaticboolean vacia(){
Stack pila =new Stack();
return pila.empty();
}
publicstaticvoid insertar(){
System.out.println("Inserte 3 elementos");
Scanner sc =new Scanner(System.in);
Stack pila =new Stack();
pila.push("Tigres del Licey");
pila.push("Leones del Escogido");
pila.push("Aguilas cibaenas");
pila.push("Gigantes del Nordeste");
pila.push("Estrellas Orientales");
pila.push("Toros del Este");
System.out.println(pila);
borrar();
}
publicstaticvoid borrar(){
Stack pila =new Stack();
pila.pop();
System.out.println(pila);
}
publicstaticvoid main(String args[]){
vacia();
}
}

