Results 1 to 4 of 4

Thread: Linked Lists

  1. #1
    ashplusham is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Linked Lists

    Hey guys,
    I have another assignment and this time it's on linked lists. I have to write a few methods in and I'm having trouble with 3 of them:
    *public void setElement(int index, Object element)
    The setElement sets the parameter element at the parameter specified index by deleting the element at the index.
    *public void removeFirstFew(int howMany)
    The removeFirstFew method removes the parameter specified number of elements from the beginning of the linked list. For instance, if the parameter integer is 3, then it should remove the first three elements of the linked list. If the parameter integer is larger than the current size of the linked list, then the linkedlist should become empty. If the parameter integer is less than or equals to 0, nothing should be removed from the linked list.
    *public Object findLargest( )
    The findLargest should return the largest element. In this case, since all elements in the linked list will be strings, it needs to compare strings based on their lexical order (if you use the compareTo( ) method of the String class, you will automatically get this lexical order). If the linked list is empty, then it should throw an object of the NoSuchElementException class.

    I'll just post the class that these methods go into, but there are two other classes in the whole program as well.
    Thanks in advance!!

    Java Code:
    import java.util.NoSuchElementException;
    
    public class LinkedList
    {
       //nested class to represent a node
       private class Node
       {
              public Object data;
              public Node next;
       }
    
       //only instance variable that points to the first node.
       private Node first;
    
       // Constructs an empty linked list.
       public LinkedList()
       {
          first = null;
       }
    
    
       // Returns the first element in the linked list.
       public Object getFirst()
       {
          if (first == null)
           {
             NoSuchElementException ex
                 = new NoSuchElementException();
             throw ex;
           }
          else
             return first.data;
       }
    
       // Removes the first element in the linked list.
       public Object removeFirst()
       {
          if (first == null)
           {
             NoSuchElementException ex = new NoSuchElementException();
             throw ex;
           }
          else
           {
             Object element = first.data;
             first = first.next;  //change the reference since it's removed.
             return element;
           }
       }
    
       // Adds an element to the front of the linked list.
       public void addFirst(Object element)
       {
          //create a new node
          Node newNode = new Node();
          newNode.data = element;
          newNode.next = first;
          //change the first reference to the new node.
          first = newNode;
       }
    
       // Returns an iterator for iterating through this list.
       public ListIterator listIterator()
       {
          return new LinkedListIterator();
       }
    
    //toString method
       public String toString(){
    	   LinkedListIterator iterator = new LinkedListIterator();
    	   String result = "{ ";
    	   while(iterator.hasNext())
    		   result += (iterator.next()+" ");
    	   result += "}\n";
    	   return result;
       }
      
    //isEmpty method
       public boolean isEmpty(){
    	   LinkedListIterator it = new LinkedListIterator();
    
    	   if(!it.hasNext())
    		   return true;
    	   else
    		   return false;
       }
    
    //addElement method
       public void addElement(int index, Object element){
    	   if(index < 0)
    	   {IndexOutOfBoundsException ex = new IndexOutOfBoundsException();
    		throw ex;}
    
       LinkedListIterator iterator = new LinkedListIterator();
    
       for(int i = 0; i<index; i++)
       {
    	   if(iterator.hasNext())
    	      iterator.next();
    	   else
    	{
    	NoSuchElementException ex = new NoSuchElementException();
         	throw ex;
    	}
       }
    
       if(iterator.hasNext())
       {
          Object a = iterator.next();
          iterator.set(element);
          while(iterator.hasNext())
          {
    	      Object b = iterator.next();
    	      iterator.set(a);
    	      a = b;
          }
          iterator.add(a);
       }
       else
    	   iterator.add(element);
       }
    
    //setElement method
       
    //getElement method (to be used in removeElement)
       public Object getElement(int index)
       {
    	   LinkedListIterator it = new LinkedListIterator();
    
    	   for(int i = 0; i<index; i++)
    		   {it.next();}
    	   return it.next();
       }
       
    //size method (to be used in removeElement)
       public int size()
       {
    	   LinkedListIterator iterator = new LinkedListIterator();
    	   int a = 0;
    	   while(iterator.hasNext())
    	   {
    		   iterator.next();
    		   a++;
    	   }
    	   return a;
       }
    
    //removeElement method
       public Object removeElement(int index)
       {
    	   if(index<0)
    		   {NoSuchElementException ex = new NoSuchElementException();
             throw ex;}
    
    	   if(index>size()-1)
    		{NoSuchElementException ex = new NoSuchElementException();
             throw ex;}
    
    	   Object result = null;
    
    	  LinkedListIterator it = new LinkedListIterator();
    	   result = getElement(index);
    	   if(index<size()-1)
    	   {
    		   for(int i = 0; i<index+1; i++)
    			   it.next();
    		   while(index<size()-2)
    		   {
    			   it.set(getElement(index+1));
    			   it.next();
    			   index++;
    		   }
    		   it.remove();
    	   }
    	   else
    	   {
    		   for(int i = 0; i<index+1; i++)
    			   it.next();
    		   it.remove();
    	   }
    
    	  return result;
       }
    
    //removeFirstFew method
       
    //findLargest method
       
    //removeAllOccurrences method
      public void removeAllOccurrences(Object stringToBeRemoved){
    	   for(int i = 0; i<size(); i++){
    		   if(getElement(i) == stringToBeRemoved){
    			   removeElement(i);
    			   i--;
    		   }
    	   }
       }
       
    //nested class to define its iterator
       private class LinkedListIterator implements ListIterator
       {
          private Node position; //current position
          private Node previous; //it is used for remove() method
    
          // Constructs an iterator that points to the front
          // of the linked list.
    
          public LinkedListIterator()
          {
             position = null;
             previous = null;
          }
    
         // Tests if there is an element after the iterator position.
         public boolean hasNext()
          {
             if (position == null) //not traversed yet
              {
                 if (first != null)
                    return true;
                 else
                    return false;
              }
             else
               {
                  if (position.next != null)
                     return true;
                  else
                     return false;
               }
          }
    
          // Moves the iterator past the next element, and returns
          // the traversed element's data.
          public Object next()
          {
             if (!hasNext())
              {
               NoSuchElementException ex = new NoSuchElementException();
               throw ex;
              }
             else
              {
                previous = position; // Remember for remove
    
                if (position == null)
                   position = first;
                else
                   position = position.next;
    
                return position.data;
              }
          }
    
          // Adds an element before the iterator position
          // and moves the iterator past the inserted element.
          public void add(Object element)
          {
             if (position == null) //never traversed yet
             {
                addFirst(element);
                position = first;
             }
             else
             {
                //making a new node to add
                Node newNode = new Node();
                newNode.data = element;
                newNode.next = position.next;
                //change the link to insert the new node
                position.next = newNode;
                //move the position forward to the new node
                position = newNode;
             }
             //this means that we cannot call remove() right after add()
             previous = position;
          }
    
          // Removes the last traversed element. This method may
          // only be called after a call to the next() method.
          public void remove()
          {
             if (previous == position)  //not after next() is called
              {
                IllegalStateException ex = new IllegalStateException();
                throw ex;
              }
             else
              {
               if (position == first)
                {
                  removeFirst();
                }
               else
                {
                  previous.next = position.next; //removing
                }
               //stepping back
               //this also means that remove() cannot be called twice in a row.
               position = previous;
          }
          }
    
          // Sets the last traversed element to a different value.
          public void set(Object element)
          {
             if (position == null)
              {
                NoSuchElementException ex = new NoSuchElementException();
                throw ex;
              }
             else
              position.data = element;
          }
       } //end of LinkedListIterator class
    } //end of LinkedList class

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Linked Lists

    You didn't bother to reply to quad64bit's response in Recursive method help?. Why should we expect any better behavior this time round?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ashplusham is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Linked Lists

    I never got an email saying he replied so I hadn't noticed. My apologies.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Linked Lists

    I wasn't getting notifications for a few days myself, no worries.

Similar Threads

  1. Combining linked lists.
    By syle_q in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 01:52 AM
  2. concatenating linked lists
    By coder94 in forum New To Java
    Replies: 1
    Last Post: 02-28-2011, 06:16 AM
  3. Linked Lists
    By Dee in forum New To Java
    Replies: 18
    Last Post: 02-02-2011, 03:14 AM
  4. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 08:23 AM
  5. Single linked lists - help
    By Srcee in forum New To Java
    Replies: 10
    Last Post: 10-29-2009, 05:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •