Nevets0423
Nevets0423

Reputation: 69

Java Programing Data field "head"

Im in a Java class at school and for the next program we have to edit a list. However there is one part of the instructions I don't understand.
Instructions from Homework:

It has a single data field "head" with the data type of MyNode, which is defined as follows:

public class MyNode<E extends Comparable<E>> {
    E element;
MyNode<E> next;

public MyNode(E item) {
    element = item;
    next = null;
    }
}

It contains a non-argument constructor that initialize head to be null.

I don't understand what my instructor means by "head"? Is he referring to the list as the "head"? Any ideas will help. Thank you.

Upvotes: 0

Views: 176

Answers (2)

Jason
Jason

Reputation: 11822

That looks like the implementation of a linked list where each item (or node) contains a link to the next item (or node). Often the first item in a linked list is referred to as the 'head'.

So the instructions are asking you to write a class that contains a variable of type MyNode called head.

Something like this:

public class MyAnswer {
    private MyNode head;

    public MyAnswer() {
        head = null;
    }
}

Upvotes: 1

dramzy
dramzy

Reputation: 1429

In a linked list, head is the first element or node in the list. The head serves as an entry point to your list as you can reach any element (let's say the nth element) of the list by starting from head and accessing the next field of the node objects n times.

Upvotes: 1

Related Questions