Home arrow static arrow Java Programming [Archive] - LinkedList
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - LinkedList
This topic has 7 replies on 1 page.

Posts:357
Registered: 8/5/04
LinkedList  
Aug 7, 2004 1:59 PM



 
Hi:
I've got a little problem in the "remove" method of this LinkedList class.

Please have a look at the comments in the remove method. thanks.

import java.util.Iterator; public class MyLinkedList {    private class MyIterator implements Iterator {      private Node where;       public MyIterator() {        where = head;      }       public boolean hasNext() {        System.out.println("head " + head);      return (where.getNext() != null);      }       public Object next() {        Node n = where;        where = where.getNext();        return n;      }       public void remove() throws UnsupportedOperationException {        throw new UnsupportedOperationException("remove not supported");      }    }     private class Node {	private Node next;	private Object o; 	public Node(Object o) {	    this.o = o;	    this.next = null;	} 	public Object getObject() {	    return o;	} 	public Node getNext() {	    return next;	}  	public void setNext(Node next) {	    this.next = next;	} 	public void setObject(Object o) {	    this.o = o;	} 	public boolean equals(Object o) {	    return this.equals(o);	}    }     private Node head;     public MyLinkedList() {	head = null;    }     public void add(Object o) {	Node tmp = new Node(o);	Node pre = null;	for(Node cur=head; cur!= null;pre=cur,cur=cur.getNext())	    ;	if(pre ==null)	    head = tmp;	else	    pre.setNext(tmp);    }     public int indexOf(Object o) {	int i=0;	for(Node cur=head; cur != null; cur=cur.getNext()){	    if(cur.getObject().equals(o))		return i;	}	return -1;    }     public void remove(int index) {	Node pre = null; 	Node cur = head; 	for(int i=0;i<index;i++){	    pre = cur;	    cur = cur.getNext(); //Stops when the element to be removed is reached	}	if(pre == null) // if the element to be removed is null	    head = head.getNext(); //What is this For? head.getNext???	else	    pre.setNext(cur.getNext()); /* otherwise update the link of the element before the element removed with the element after the element removed.*/    }}	
 

Posts:8,813
Registered: 10/4/00
Re: LinkedList  
Aug 7, 2004 2:16 PM (reply 1 of 7)



 
I'm guessing the summer semester ended Aug 6th and you are trying to make up all the homework you were supposed to do during the semester this weekend.

That sounds like a lot of work. You deserve a six pack or three.
 

Posts:357
Registered: 8/5/04
Re: LinkedList  
Aug 7, 2004 2:19 PM (reply 2 of 7)



 
It's not "homework"; this is the code show by the teacher "during" the class on the slide with projector.
 

Posts:18,384
Registered: 21.03.00
Re: LinkedList  
Aug 7, 2004 2:19 PM (reply 3 of 7)



 
Hi,

You reach:
  head = head.getNext(); //What is this For? head.getNext???


When you call remove with inde 0, and that means that you want to remove the first element in the list. So the line changes so that head points to the element after the current head.

/Kaj
 

Posts:357
Registered: 8/5/04
Re: LinkedList  
Aug 7, 2004 2:20 PM (reply 4 of 7)



 
... without my presence ; )
 

Posts:357
Registered: 8/5/04
Re: LinkedList  
Aug 7, 2004 2:22 PM (reply 5 of 7)



 
So the line changes so that head points to the element after the current head.

Oh so its specially for element at index 1 right?
 

Posts:18,384
Registered: 21.03.00
Re: LinkedList  
Aug 7, 2004 2:25 PM (reply 6 of 7)



 
At index 0, yes :)

The first element has index 0.

/Kaj
 

Posts:8,813
Registered: 10/4/00
Re: LinkedList  
Aug 7, 2004 2:39 PM (reply 7 of 7)



 
It's not "homework"; this is the code show by the
teacher "during" the class on the slide with
projector.

... without my presence ; )

I'm impressed. Have you considered a career in the Psychic Arts?
 
This topic has 7 replies on 1 page.