aboutsummaryrefslogtreecommitdiff
path: root/Week6/src/Node.java
blob: 2a33cc4c863971b0538b002bb43085ad3a036b3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * For maintaining lists of T-elements enabling
 * a structure suited for backwards traversal
 * @author Pieter Koopman, Sjaak Smetsers
 * @version 1.2
 * @date 28-02-2015
 */
public class Node<T>
{
    // the data field
    private T item;
    // a reference to the predecessor
    private Node<T> previous;

    /* A constructor that initializes each node
     * with the specified values
     * @param from the node preceding the current node
     * @param item the initial data item
     */
    public Node (Node<T> from, T item) {
        this.previous    = from;
        this.item        = item;
    }

    /* a getter for the item
     * @return item
     */
    public T getItem() {
        return item;
    }

    /*
     * a getter for the predecessor
     * @return previous
     */
    public Node<T> getPrevious() {
        return previous;
    }

    /*
     * determines the length of the current list
     * @return the length as an integer
     */
    public int length () {
        int length = 1;
        Node<T> prev = previous;
        while ( prev != null ) {
            prev = prev.previous;
            length++;
        }
        return length;
    }
    
    @Override
    public String toString() {
        throw new UnsupportedOperationException("toString : not supported yet.");        
    }
}