Linked List - Deleting Element That User Specifies As A Parameter

Posted 18 March 2013 - 08:19 AM

I have a Linked List which holds Student objects, On the Register class i have a Linked List that will hold the students, But i want a method that would allow me to delete a student from the list using their StudentIDNumber.

Re: Linked List - Deleting Element That User Specifies As A Parameter

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 18 March 2013 - 12:13 PM

Your loop is kinda inefficient and will fail
Don't remove from the list while enumerating over it using a for-loop
If you use a list iterator, you will be able to remove items from the list using the built in methods provided by the iterator

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 18 March 2013 - 12:16 PM

Does your Student class has an equals() method that identifies 2 Student to be equals() when their ID is equal ?
The LinkedList.remove(object) method will scan the LinkedList and remoce the first element in the list.equals(object)

Another alternative is to use the old for() loop way and to remvove by index

*Edited: and as CasiOo mentionned your loop is really inneficient
If you the Student to remove is number 156,000 in the list, you will be scanning 1456,00 elements and then your call to remove(Student) will also have to scan those 156,000 element repeating your comparaison
Doing remove(156000) at least avoid to redo the comparaisons and will start by the end if the list has less than 312,000 elements

Re: Linked List - Deleting Element That User Specifies As A Parameter

I dont need the items to be deleted if they are the same, basically the end product is a Registartion class where you can add, delete and update students that are stores in the linkedlist.

Now when you go to delete the interface will ask for which student do you want to delete, then they enter the students id, and then i call the deletestudent method to delete that student from the list.

Re: Linked List - Deleting Element That User Specifies As A Parameter

So... the remove(Student method) will pass through all the Student in the link list and compare them

If you do not have a equals() method none of tehm will be equal() so your code fails
Here is the code of remove(object) from LinkedList.java

/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public boolean remove(Object o) {
if (o==null) {
for (Entry<E> e = header.next; e != header; e = e.next) {
if (e.element==null) {
remove(e);
return true;
}
}
} else {
for (Entry<E> e = header.next; e != header; e = e.next) {
if (o.equals(e.element)) {
remove(e); // this is another remove() that works by node
return true;
}
}
}
return false;
}

See the line 24
if (o.equals(e.element)) {
so if your Student class does not have a method equals() that returns if 2 Students are equals or not (in your case have the same ID) it will never find two Student equals

Also, that method is not good as we have both written... a lot more efficient to remove() by index for the reasons explained

This post has been edited by pbl: 18 March 2013 - 02:40 PM
Reason for edit:: Added line number

Re: Linked List - Deleting Element That User Specifies As A Parameter

No student will have the same ID though, I create the Student object which are: Student st1 = new Student("Bob","Williams",678);

The ID of this student is 678. Now i have a class called Register which has a LinkedList to Hold These Sudent objects that i craete, Example no i have 10 students:

st1 st2 .................. st10.

On my register Class i have a method to ADD a student to the LinkedList and a method to Delete a Student. To delete a student you must pass in the students IDNumber wich in my case is 678 for example. Now if i call the deleteStudent method with a parameter of 678.

This now will look through the linkedlist for an element that has an id of 678.

When this element is found the object will be removed from the list. So st1 BOB WILLIAMS 678 will be removed from the List.