Advertisement

08.08.2004 at 03:37AM PDT, ID: 21086120
[x]
Attachment Details

LinkedList implements the Collections interface?

Asked by anosTekbot in Java Programming Language, New to Java Programming

Hello
i want to my LinkedList class implements Collection interface but need help on that
any suggestion/help.

this is the LinkedList class

/**
*  Linked list implementation
*/

public class LinkedList
{
      protected Node firstNode = null;
      
      /**
      *  Returns true if list is empty
      */
      
      public boolean isEmpty()
      {
            return firstNode==null;
      }
      
      /**
      *  Adds a new node to the front of the list
      *  @param o object to add
      */
      
      public void add(Object o)
      {
            Node node = new Node(o, firstNode);
            firstNode = node;
      }

      /**
      *  Adds an object to list at specified index
      */
      
      public void add(Object o, int index)
      {
            if (index==0)
            {
                  // add as first node
                  
                  add(o);
            }
            else
            {
                  // find node before it in list
                  
                  Node prev = get(index-1);
                  if (prev!=null)
                  {
                        // and insert new node
                        
                        prev.setNext(new Node(o, prev.getNext()));
                  }
            }
      }
      
      /**
      *  Removes an indexed element from the list
      *  @param index index of node to remove
      */
      
      public Object remove(int index)
      {
            Object result = null;
            
            // find node in list
            // also need to remember previous node so we can update it's next pointer
            
            Node prev = null;
            Node node = firstNode;
            for (int i=0; node!=null && i<index; i++)
            {
                  prev = node;
                  node = node.getNext();
            }
            if (node!=null)
            {                  
                  // remove element from list
                  
                  if (prev==null)
                  {
                        // remove first node
                        
                        firstNode = node.getNext();
                  }
                  else
                  {
                        // remove node
                        
                        prev.setNext(node.getNext());
                  }
                  
                  result = node.getData();
                  node.setNext(null);
                  
            }
            return result;
      }
      
      /**
      *  Returns node at a specified index
      */
      
      protected Node get(int index)
      {
            Node node = firstNode;
            for (int i=0; node!=null && i<index; i++)
            {
                  node = node.getNext();
            }
            return node;
      }

      /**
      *  Returns a string representation of the elements in the list
      */
      
      public String toString()
      {
            StringBuffer result = new StringBuffer();
            result.append("");
            Node next = firstNode;
            while (next!=null)
            {
                  result.append(next);
                  next = next.getNext();
                  if (next!=null)
                  {
                        result.append("");
                  }
            }
            result.append("");
            return result.toString();
      }
}
Start Free Trial
 
Keywords: LinkedList implements the Collections in…
 
Loading Advertisement...
 
[+][-]08.08.2004 at 03:41AM PDT, ID: 11745909

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, New to Java Programming
Sign Up Now!
Solution Provided By: girionis
Participating Experts: 7
Solution Grade: A
 
 
[+][-]08.08.2004 at 03:52AM PDT, ID: 11745943

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.08.2004 at 04:03AM PDT, ID: 11745971

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.08.2004 at 07:25AM PDT, ID: 11746388

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.08.2004 at 07:25AM PDT, ID: 11746390

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.08.2004 at 03:07PM PDT, ID: 11748726

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.08.2004 at 03:51PM PDT, ID: 11748909

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08.08.2004 at 04:22PM PDT, ID: 11749022

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.09.2004 at 05:07AM PDT, ID: 11751583

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08.31.2004 at 03:07AM PDT, ID: 11940257

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]09.04.2004 at 09:08AM PDT, ID: 11980536

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]02.08.2005 at 03:28PM PST, ID: 13259865

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.08.2005 at 10:49PM PST, ID: 13261517

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32