|
-
Oct 19th, 2005, 05:09 PM
#1
Thread Starter
Member
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
-
Oct 19th, 2005, 05:37 PM
#2
Frenzied Member
Re: Linked Lists
I'm guessing you need to specify an element to get 
temp.getElement(i);
-
Oct 19th, 2005, 06:22 PM
#3
Thread Starter
Member
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
-
Oct 19th, 2005, 08:02 PM
#4
Frenzied Member
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(==).
-
Oct 24th, 2005, 11:55 PM
#5
Dazed Member
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?
-
Oct 25th, 2005, 12:54 AM
#6
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.
-
Oct 25th, 2005, 12:57 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|