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();

}

}

[5449 byte] By [River_Platea] at [2007-11-27 9:54:14]
# 1
One problem exists with lines such as the following:lista.remove(2);If the list only contains two students, this line will fail (lists are zero-indexed, so the elements will be '0' and '1').~
yawmarka at 2007-7-13 0:24:06 > top of Java-index,Java Essentials,Java Programming...
# 2
Can i set that index to null insteand of deleting it?
River_Platea at 2007-7-13 0:24:06 > top of Java-index,Java Essentials,Java Programming...
# 3
> Can i set that index to null insteand of deleting it?That index doesn't exist. It's outside the range of available indices, which is why you get an IndexOutOfBoundsException.~
yawmarka at 2007-7-13 0:24:06 > top of Java-index,Java Essentials,Java Programming...
# 4
That's why i have a hard time dealing with linked lists.. i just don't know where and how they store the data.. there's a method called toArray(); do you think i can use it to somehow make sure that the values i put in will have a real index?what else do i think i could
River_Platea at 2007-7-13 0:24:06 > top of Java-index,Java Essentials,Java Programming...
# 5

> That's why i have a hard time dealing with linked

> lists.. i just don't know where and how they store

> the data..

http://java.sun.com/docs/books/tutorial/collections/

> there's a method called toArray(); do you think i

> can use it to somehow make sure that the values i put

> in will have a real index?

No, that's not necessary.

> what else do i think i could do?

Validate the user input. If it's a negative number or pertains to an index that is out of range (i.e., greater than the size of the list minus one), notify the user and try again.

~

yawmarka at 2007-7-13 0:24:06 > top of Java-index,Java Essentials,Java Programming...