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: need help with a list class

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with a list class

    iv created a small class that creates linked lists. however i was told to make it more efficient. apparently its inefficient bcuz we have to start at the beginning of the linked list everytime we do a search..how can i make the class "remember" its previous position and start from there...this should allow the position method to start searching from the given position..

    how do i do this...code follows:

    public class IntegerList
     
      { private class ListNode
     
          { public int data;
     
            public ListNode next;
     
          } // inner class ListNode
     
     
     
        /** Reference to the first ListNode in a IntegerList.
     
          */
     
        private ListNode first;
     
        /** Number of elements in a IntegerList.
     
          */
     
        private int numElements;
     
     
     
        /** Create an empty IntegerList.
     
          * <BR><I>Postcondition:</I> The list is empty.
     
          */
     
        public IntegerList ()
     
          { first = null;
     
            numElements = 0;
     
          } // IntegerList constructor
     
     
     
        public void add (int item, int position)
     
          { assert position >= 0 : "Position is non-negative";
     
            ListNode node = new ListNode();
     
            node.data = item;
     
            ListNode curr = first,
     
                     prev = null;
     
            for (int k = 0; k < position && curr != null; k++) // Find position
     
              { prev = curr;
     
                curr = curr.next;
     
              }
     
            node.next = curr;
     
            if (prev != null)
     
              prev.next = node;
     
            else
     
              first = node;
     
            numElements++;
     
         } // add
     
     
     
        /** Place a new item at the end of an IntegerList.
     
          * <BR><I>Postcondition:</I> The value <CODE>item</CODE> appears at
     
          * the end of the list.
     
          * @param item The integer value to be added to the list.
     
          */
     
        public void add (int item)
     
          { add(item, numElements);
     
          } // add
     
     
     
        /** Remove the item at a given position in an IntegerList.
     
          * <BR><I>Precondition:</I> The position is that of a valid item.
     
          * <BR><I>Postcondition:</I> The item at <CODE>position</CODE> has been
     
          *  removed from the list.
     
          * @param position The position of the item to be removed from the
     
          * list.
     
          * @throws AssertionError If <CODE>position</CODE> is invalid.
     
          */
     
        public void remove (int position)
     
          { assert position >= 0 && position < numElements : "Position is in range";
     
            ListNode curr = first,
     
                     prev = null;
     
            for (int k = 0; curr != null && k < position; k++)
     
              { prev = curr;
     
                curr = curr.next;
     
              }
     
            assert curr != null;
     
            if (prev != null)
     
              prev.next = curr.next;
     
            else
     
              first = curr.next;
     
            numElements--;
     
          } // remove
     
     
     
        /** Return the current number of elements in an IntegerList.
     
          * @return The number of elements in the list.
     
          */
     
        public int length ()
     
          { return numElements;
     
          } // length
     
     
     
        /** Retrieve an element from an IntegerList.
     
          * <BR><I>Precondition:</I> <CODE>index</CODE> is in range.
     
          * @return The element at position <CODE>index</CODE> in the list.
     
          * @throws AssertionError If <CODE>index</CODE> is invalid.
     
          */
     
        public int get (int index)
     
          { assert (index >= 0 && index < numElements) : "Index is in range";
     
            ListNode curr = first;
     
            for (int k = 0; curr != null && k < index; k++)
     
              curr = curr.next;
     
            assert curr != null;
     
            return curr.data;
     
          } // get
     
     
     
        /** Change the value of an element in an IntegerList.
     
          * <BR><I>Precondition:</I> <CODE>index</CODE> is in range.
     
          * @param index The position of the item to be changed in the list.
     
          * @param item The new value for the item.
     
          * @throws AssertionError If <CODE>index</CODE> is invalid.
     
          */
     
        public void set (int index, int item)
     
          { assert (index >= 0 && index < numElements) : "Index is in range";
     
            ListNode curr = first;
     
            for (int k = 0; curr != null && k < index; k++)
     
              curr = curr.next;
     
            assert curr != null;
     
            curr.data = item;
     
          } // get
     
     
     
        /** Find item in an IntegerList.
     
          * @param item The item to be searched for.
     
          * @return The position of this item if it is found, otherwise -1
     
          */
     
        public int position (int item)
     
          { ListNode curr = first;
     
            int k;
     
            for (k = 0; curr != null && curr.data != item; k++, curr = curr.next)
     
              ; // Search for item in IntegerList
     
            if (curr == null) // item was not found
     
              return -1;
     
            else
     
              return k;
     
          } // position
     
     
    	public int position (int item, int pos)
     
          { ListNode curr = first;
     
            int k;
    	for (int i = 0; i<=pos;i++)
    	curr = curr.next();
           for (k = pos; curr != null && curr.data != item; k++, curr =curr.next)
     
              ; // Search for item in IntegerList
     
            if (curr == null) // item was not found
     
              return -1;
     
            else
     
              return k;
     
          }
     
        /** Return string representation of the IntegerList.
     
          * The format is: <CODE>[ <I>item</I>, <I>item</I>, ... ]</CODE>
     
          * @return A string representing the contents of this list
     
          */
     
        public String toString ()
     
          { StringBuffer s = new StringBuffer("[");
     
            for (ListNode curr = first; curr != null; curr = curr.next)
     
              { s.append("" + curr.data);
     
                if (curr.next != null)
     
                  s.append(", ");
     
              }
     
            s.append("]");
     
            return s.toString();
     
          } // toString
     
     
     
      } // class IntegerList


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need help with a list class

    You can create another node inside your list that acts like a "cursor" or "iterator". Then, when you perform a search start looking from where that cursor node is. You will need to be careful about what will happen when you remove the cursor node, though.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 04:38 AM
  2. list in JSP
    By smackdown90 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 01:08 PM
  3. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  4. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 05:48 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:23 PM