errr so my lecture said that never use static *thingy* when u want to make an LL. but the error msg said that i have to make those variable to be static. anyone ??

06-27-2010, 02:23 PM

JosAH

Static methods can only refer to static members in the same class, so either make those members static (which your professor doesn't like) or make those methods non-static; there's a whole lot more wrong in your class but first you have to fix this ...

kind regards,

Jos

06-27-2010, 04:23 PM

zaydz

i already made it non-static at the first. but when im going to call the AddLast Method it have to be static. so i just made it static but its still error..lul

06-27-2010, 04:33 PM

JosAH

Quote:

Originally Posted by zaydz

i already made it non-static at the first. but when im going to call the AddLast Method it have to be static. so i just made it static but its still error..lul

You can't make methods or other members static or non-static at will; there has to be a reason for it. You're also confusing the entire list with a single node of a list. Show us your latest code.

kind regards,

Jos

06-27-2010, 05:29 PM

Norm

Your problem starts with the main() method calling a static method vs having the main() method create an instance of the class and using a reference to that instance to call the class's methods:

In main:
LinkList aLL = new LinkList(); // create an instance
aLL.AddFirst(); // call the AddFirst method to ....

06-27-2010, 08:59 PM

zaydz

my bad. and i already solved it and made new code thx

06-27-2010, 09:03 PM

JosAH

Quote:

Originally Posted by zaydz

my bad. and i already solved it and made new code thx

Why don't you show your final code here? We can criticize it (or compliment it if it's really perfect) and maybe the final result will be better than your individual efforts? You show your code and I'll show mine, deal?

kind regards,

Jos

ps. I only have a skeleton of a singly linked list class yet. ;-)

06-27-2010, 09:07 PM

zaydz

for sure
this is the abstract data type

Code:

public class maha
{
String nama;
int NIM;
maha next;

public maha(){
this.nama= "";
this.NIM= 0;
this.next= null;
}
}

and this is the Link List *not yet finish just add and print, totally different from my first.

Now that you have the logic for how a Linked list should work, try changing it so that a program can have more than one linked list at a time.
This mean removing all the "static"s.

06-27-2010, 09:55 PM

JosAH

Cool, here is my implementation of a singly linked list. The list itself hides all nodes from the outer world and it can store all types of data elements by using generics; it also implements the Iterable<T> interface for convenience. Here it is:

If you (or anyone) else is interested I can give a method by method explanation and extend this skeleton a bit more. Study it and enjoy.

kind regards,

Jos

06-27-2010, 09:59 PM

zaydz

nice code jos. i appreciate it. and imma study it too. thanks

06-28-2010, 04:27 AM

zaydz

anyway u didnt use tail. as the circular one.?

06-28-2010, 07:20 AM

JosAH

Quote:

Originally Posted by zaydz

anyway u didnt use tail. as the circular one.?

Nope, my list implementatation is a simple singly linked list; it isn't circular, but feel free to adjust the code.

kind regards,

Jos

06-28-2010, 07:22 AM

zaydz

yayy thanks. imma going to make 1 with next and prev i just need 2 type of link list. almost done and tomorrow i'll be ready for the exam :p

thanks jos u r helpfull

06-28-2010, 12:53 PM

JosAH

Quote:

Originally Posted by zaydz

yayy thanks. imma going to make 1 with next and prev i just need 2 type of link list. almost done and tomorrow i'll be ready for the exam :p

thanks jos u r helpfull

Well, good luck with your exams; so you want to implement a doubly linked list now? You can use my code for it and change it step by step; I'd start with implementing a notion of the 'tail' of the list, not just the 'head' of the list (as it is now). The private method 'last()' can go then, it isn't needed anymore and it'll speed up appending new elements to the list. Go step by step.