Results 1 to 7 of 7

Thread: Linked Lists

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    47

    Linked Lists

    i am doing a university assigment and im having some problems. Basically theres a series of linked lists in the setup of a bank account. i have to search for a sepcific account.

    i have the basic set up okay, a for statement thats going through the accounts and compares the account number with the search string:

    Code:
    	/**
         * Search Accounts
         */	
    	public static int seqSearch(CompNode L, Comparable target)
    	{
    	CompNode temp = L;
    	boolean found = false;
    	int position = 1;
    	for (int i = 0; i<countList(L); i++)
    	{
    		if (target == temp.getElement())
    			{
    			System.out.println ("ITEM FOUND");
    			found = true;
    			return position;
    		}
    		else
    		{
    			System.out.println (temp.getElement());
    			temp = temp.getNext();
    			System.out.println ("ITEM NOT FOUND");
    		}			
    	}
    	return position;		
    	}
    problem is temp.getelement() returns the full account where as i only need the account number.

    Now i dont want someone to give me the code i need straight off, id never learn that way but basically a point in the right direction or a sample bit of code

    Thanks

    nino

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Linked Lists

    I'm guessing you need to specify an element to get

    temp.getElement(i);

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    47

    Re: Linked Lists

    it was working without out the need to specify an element the above code didnt find the results but as went went through the for loop it printed out the records followed by "item not found"

    ive noticed the lecturer added a compareTo method in another class:

    Code:
    // compareTo method to implement Comparable interface 
    public int compareTo(Object acc)
    {
       if(acctNumber < ((Account) acc).getAccountNumber()) return -1;
       else if(acctNumber == ((Account) acc).getAccountNumber()) return 0;
              else return 1;
    }
    so i could work with:
    Code:
    if (temp.getElement().compareTo(target)==0)
    its compiling but im getting an error, the compareTo method says it needs a object to be passed in?

    Nino

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Linked Lists

    What is target? Based on the context I'm guessing it's an integer. When using compareTo() you want to see if two objects are of the same type. In this case, I would simply recommend using the equals operator(==).

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Linked Lists

    I guess you could just do a binary search using the binarySearch() method in the Collections class but it takes a List. Could you use a HashTable? Like map lname to account number?

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Linked Lists

    what's the purpose of your position? u never incremented it or something.

    and it will print out.. record not found for all records not matched.

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Linked Lists

    Code:
    public static int seqSearch(MyNode mn, String accnum){
    	int position = 0;
    	while(mn != null){
    		position++;
    		if(((Account)mn.getElement()).getAccNum() == accnum){
                            System.out.println("Record Found");
    			break;
    		}
    		mn = mn.getNext();
    	}
            return position;
    }
    above is my idea of your problem, but this is an untested code. quite different but you might learn from it too just work on your code.

    and i don't see the need for the found flag as well.
    Last edited by oceanebelle; Oct 25th, 2005 at 01:03 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width