Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

help with linked

  Asked By: Darla    Date: Jul 15    Category: Java    Views: 856
  

way to implement
my methods import java.util.*; public
class Link { private Object value;
private Link next; public Link (Object v, Link
n) { value = v; next = n; }
public void setNext (Link newLink) {
next = newLink; } public Link getNext ()
{ return next; } public Object
getValue () { return value; }
public void setValue (Object newValue) { value
= newValue; } }// Link

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Dominic Murphy     Answered On: Jul 15

Don't think you can get more efficent than what
you've got, if you are doing a "one way"
link.What exactly are you trying to do? It appears that you
are doing a "linked list", but your linked  list only
goes one way. You have no way to find the "previous"
Object.If you have a variable calledLink
previous;Then, have a method:public setPrevious(Link
aPrev){ this.previous = aPrev;}In your
constructor, you would do something like this...public
Link(Object v, link  n){ value  = v; next  = n;
n.setPrevious(this);}Then, you would have a method:public
Link
getPrevious(){ return  this.previous;}Now, you have a
doubly linked list and you can go backward and forward
in the list.Does this help? There's lots
more you can do with the linked list in terms of
sorting, merging, adding things in the list
etc.You can look in the java  Collections Framework to see
some of the Data Structures that are available. They
are the classes in "java.util". The source code
is available when you download the JDK from Sun; and
Sun has some on-line tutorials in using the Java
Collections Framework.

 
Didn't find what you were looking for? Find more on help with linked Or get search suggestion and latest updates.




Tagged: