Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Remove at index method for singly linked list... what's wrong?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Remove at index method for singly linked list... what's wrong?

    Hi,

    I'm trying to create a public E remove(int index) method. What am I doing wrong here?

    		 /** removes the element at the specified position in this list.
    		  *  Shifts any subsequent elements to the left (subtracts one
    		  *  from their indices.
    		  *  @return the element that was removed from the list
    		  *  @throws IndexOutOfBoundsException
    		  */
    		 public E remove(int index) {
    			 Node<E> temp = head;
    		     if (index < 0 || index >= size) {
    		         throw new IndexOutOfBoundsException(Integer.toString(index));
    		     }
    		     if (index == 0) {
    		    	 E result = head.data;
    		    	 head = head.next;
    		    	 size--;
    		    	 return result;
    		     }
    		     else if (index == size) {
    		    	 E result = tail.data;
    		    	 tail = tail.next;
    		    	 size--;
    		    	 return result;
    		     }
    		     for(int i = 0; i < index-1; i++) {
    		    	 temp = temp.next;
    		    	 Node<E> current = temp.next;
    		    	 temp.next = current.next;
    		    	 E result = current.data;
    		    	 current = null;
    		    	 size--; 
    		    	 return result;
    		     }
     
    		 }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Remove at index method for singly linked list... what's wrong?

    Why do you think what you've posted is incorrect?

Similar Threads

  1. Singly Linked List
    By koala711 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 02:15 PM
  2. trouble with add and remove method on doubly linked list
    By BetterCallSaul in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2012, 07:12 PM
  3. trouble with add and remove method on doubly linked list
    By BetterCallSaul in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2012, 02:51 PM
  4. Singly-Linked list structure
    By blaster in forum Algorithms & Recursion
    Replies: 24
    Last Post: March 11th, 2012, 03:42 PM
  5. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM