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 14 of 14

Thread: Creating methods in a singly linked list?

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

    Default Creating methods in a singly linked list?

    I want to create 4 methods for my singly linked list class: public boolean removeFirstOccurence(Object o), public boolean removeLastOccurence(Object o), public int indexOf(Object o), public int lastIndexOf(Object o). I am confused about having an Object as a parameter. Therefore I don't have any idea how to write these methods. I'm not gonna ask you to write it for me but can anyone sort of give me the skeleton code of each method so I can understand what is going on? I'm desperate!


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

    Default Re: Creating methods in a singly linked list?

    You give us the skeleton code, and we'll help you make it better.

    You say you're confused about having Object as a parameter, so what parameter would you not be confused about? Write the methods with the less confusing parameter first, and then we can help you change them to use Object as a parameter.

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

    Default Re: Creating methods in a singly linked list?

    Okay well I did a little research and I think I've created the first two methods roughly. So lets start with the first two. Here is my code:
    		 public E remove(int index) {
    			 Node<E> temp = head;
    			   assert(index >= 0 && index < size); //force valid index  
    			   temp = head; //start at the beginning of the list  
    			   if(index == 0){  
    				   E result = head.data;  
    				   head = head.next;   
    				   size--;  
    				   return result;  
    			   }  
    			   else if(index == size){  
    				   E result = tail.data;  
    				   tail = getNode(size-2);  
    				   size--;  
    				   return result;  
    			   }  
    			  //iterate to the position before the index  
    			   for(int i = 0; i < index-1; i++) temp = temp.next;  
    			   Node<E> current = temp.next;  
    			   //set temp.next to point to the Node next to the Node to be removed  
    			   temp.next = current.next;   
    			   E result = current.data; //store the element to return  
    			   current = null; //remove the node  
    			   size--; //decrement size  
    			   return result; //return the element at that position  
    		 }
     
    		 public boolean removeFirstOccurence(Object o) {
    			 Node<E> temp = head;
    			 if(this == head.data) {
    				 head = head.next;
    				 size--;
    				 return true;
    			 }
    			 else if(this == tail.data) {
    				 tail = getNode(size-2);
    				 size--;
    				 return true;
    			 }
    			 for(int i = 1; i < size-2; i++) temp = temp.next;
    			 	if(this.equals(getNode(i))) {
    			 		temp = null;
                                                                          size--;
    			 		return true;
    			 	}
    			return false;
    		 }

    So I think the first method should work correctly... however for the second I'm getting an error at the if statement within the for loop. How can I fix this?

    Also, the next method I have to write is removeLastOccurence(Object o), and I'm not sure how to go about that. How do you tell the compiler to skip the first occurrence and each subsequent until you get to the last one? What if there is no second occurrence? Would the last occurrence then be the first one?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    I'm getting an error
    Please copy the full text of the error message and paste it here.

    How do you tell the compiler to skip the first occurrence and each subsequent until you get to the last one?
    The compiler will skip code that is in a comment. Put /* before and */ after the code you want skipped.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Creating methods in a singly linked list?

    So for the error, it tells me "i cannot be resolved to a variable."

    Perhaps you didn't understand my question. For the removeLastOccurence method, the program needs to skip over the first and subsequent occurrences of an object until it gets to the last occurrence, and then delete the last one. I was wondering how to write a method to do that. For removeFirstOccurence you don't need to worry about each subsequent occurrence, just delete the first that is found. But this one you do need to worry about them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    "i cannot be resolved to a variable."
    The compiler can't find a definition for the variable: i at the location it is being used. A complete compiler error message would show the line where that error happened and the line number.

    didn't understand my question.
    Yes. When you said compiler I thought you were talking about the compiler, not the program when it executes.

    How would you determine the last occurrence of an item in a list of items as you look at them one at a time ( you can only see one at a time)?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Creating methods in a singly linked list?

    Right, it shows the error at the "i" in the line if(this.equals(getNode(i))). I'm trying to compare the argument to the node at index i. Could you show me how I can do this properly to correct the method so that it executes properly?

    I'm sorry, I used the wrong word.

    The question you are asking me is the question I'm having trouble answering.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    The question you are asking me is the question I'm having trouble answering.
    How would you find the suit of the last Queen in a deck of cards if you were to look at them one at a time?

    The variable i is only defined inside of the for loop.
    To include statements in a for loop, the statements must be enclosed in {}.
    Indenting the statements does not put them inside of the for statement.

    Also it is better coding practice to put only one statement on a line:
    for(int i = 1; i < size-2; i++) temp = temp.next;
    should be:
    for(int i = 1; i < size-2; i++) 
       temp = temp.next;
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Creating methods in a singly linked list?

    Great, it worked, thank you.

    Well to find the suit of that last queen... every deck of cards should only have 4 queens, so you just look for the 4th queen and return the suit. So does this mean you should know how many of each occurrence there is in a linked list?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    Suppose the deck has been altered and the number of queens is not known.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Creating methods in a singly linked list?

    So then if you go through the deck of cards and look at each card one by one, you wouldn't know which is the last one until you go through the whole deck. Would you then have to go through the deck again to locate the last queen now that you know which one it is?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    Yes, you'd have to look at all the cards to know that you had seen the last queen.
    now that you know which one it is?
    Couldn't you remember where the last one was and not have to go back and do it again?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Creating methods in a singly linked list?

    Perhaps... depending on how big the deck of cards is. So to write this method I would start off with a loop that compares the data to the argument... and then what?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating methods in a singly linked list?

    Do you have the idea now of how to get the suit of the last Queen in a deck? Can you explain it?
    When you get the logic worked out, then move on to writing some code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Remove at index method for singly linked list... what's wrong?
    By EDale in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2013, 09:12 PM
  2. Singly Linked List
    By koala711 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 02:15 PM
  3. Singly-Linked list structure
    By blaster in forum Algorithms & Recursion
    Replies: 24
    Last Post: March 11th, 2012, 03:42 PM
  4. Singly Circular Linked List Error
    By clydefrog in forum Collections and Generics
    Replies: 7
    Last Post: March 5th, 2012, 08:17 PM