0

So I need a linked list to store multiple variables, so I want to use my hand-made Linked List ADT, but also want Java's Linked List's Comparator Sort from collections.sort();

So I tried and edited my code as following:

public class HLinkedList <HTreeNode>  extends LinkedList <HTreeNode>
{

public class HTreeNode {


    public HTreeNode left;
    public HTreeNode right;
    public HTreeNode next;
    public int frequency;
    public char value;
    public String code;
    public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
    {
        value = val;
        frequency = freq;
        left = l;
        right = r;
        next = n;
        code = ""; // just initialized ,but have to think through logic.
    }
}

but if I do this, instead of just public class HLinkedList (which is fine except I can't use Collections.sort(HList, comparatorA), one line of code that I need to have Java's Linked List for.

anyways, if I have my code as shown above it returns a

cannot make static reference to non-static type HTreeNode in following line, 

with red lining under HTreeNode. This does not happen if I do not try to extend LinkedList.

public static void insertIntoPosition(HTreeNode node, int position)

also following the above error is an error in

public HLinkedList() //constructor
{
    head = null; //inital value
    nItem = 0;//counter

}

where multiple occurences in the code indicate they cannot static reference a non-static head. Usually when stuff like this comes up I click "make that head static", but in this case when I do that even more errors pop up, now pointing to ALL the "head" references.

I would just like to know what is going on when I am trying and failing at extending LinkedList, or if I'm supposed to not extend but do something else maybe?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Sukwoo
  • 205
  • 2
  • 5
  • 9
  • 1
    This does not look like a list to me. It actually reminds me of a tree. – Captain Giraffe Oct 18 '12 at 23:56
  • yeah it's a list that will be used to make a tree so I tried to put everything in, although i don't think that matters- the professor seemed ok with this – Sukwoo Oct 19 '12 at 00:00
  • 2
    You're extending LinkedList, which has a member called Head. If I were you, I'd make my own linked list and implement the interface of iComparator. This is a matter of knowing how to use Generics. I'm not heavy into Java, but I remember that much. Good luck with your algorithm structures – Stephen J Oct 19 '12 at 00:02
  • If I were your professor, I would not be OK with this. A tree is not a list. – Captain Giraffe Oct 19 '12 at 00:02
  • 1
    if you want to use sort you can also try implementing the List interface. Collections.sort works with any List implementation that implements Comparable as well. – Martin Serrano Oct 19 '12 at 00:03
  • 1
    What you're trying to do will mess up badly if you try extending `java.util.LinkedList`, and there's no need to extend `LinkedList` to get the benefits of `Collections.sort()`. What you ought to do is extend `java.util.AbstractSequentialList`, which is a skeletal design for linked-list-style classes. – Louis Wasserman Oct 19 '12 at 00:22
  • great, I didn't realize that was the case – Sukwoo Oct 19 '12 at 00:34
  • I think I'll have to go with extending the ABsSeqList – Sukwoo Oct 19 '12 at 00:36

2 Answers2

2

The following method is a class method:

 public static void insertIntoPosition(HTreeNode node, int position)

Thus it does not need an instance of HLinkedList in order to be invoked.

However, your class HTreeNode is an inner class - and need an "attahced" instance of HLinkedList in order to be instantiated.

These two facts contradict each other (you cannot instantiate an object of this type in this method)

You can add the static keyword to the declaration of HTreeNode in order to overcome this:

public static class HTreeNode { //note the static keyword usage
    ....
}
amit
  • 175,853
  • 27
  • 231
  • 333
2

Another problem is that you should not have a generic type qualifier on HLinkedList. It's not doing what you think it is. What you are specifying is to create a class that stores "something", and this isn't the inner class. Instead, you need to move the HTreeNode class outside HLinkedList and then use this instead:

public class HLinkedList extends LinkedList <HTreeNode>

Otherwise, the generic type hides the inner class and I can also create a HLinkedList that stores strings.

HLinkedList<String> hll = new HLinkedList<String>();
hll.add("Hello World");
Gnat
  • 2,861
  • 1
  • 21
  • 30