Go Back   Java Programming Forums > Java Standard Edition Programming Help > Object Oriented Programming


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 25-02-2010, 06:53 PM
Junior Member
 

Join Date: Sep 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
araujo3rd is on a distinguished road
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:

Java Code
 
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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 25-02-2010, 11:58 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubt regarding LIST puneetsr Collections and Generics 1 23-02-2010 08:19 PM
list in JSP smackdown90 JavaServer Pages: JSP & JSTL 1 09-02-2010 11:58 AM
How to List all files in a directory JavaPF Java Code Snippets and Tutorials 5 03-11-2009 01:42 PM
What kind of List? Sterzerkmode Java Theory & Questions 1 07-05-2009 09:48 AM
Linked list rosh72851 Collections and Generics 1 09-03-2009 10:23 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:58 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.