-
Notifications
You must be signed in to change notification settings - Fork 3
/
DLLNode.java
74 lines (56 loc) · 1.92 KB
/
DLLNode.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*****************************************************
* class DLLNode
* Implements a node, for use in lists and other container classes.
*****************************************************/
public class DLLNode<T>
{
private T _cargo; //cargo may only be of type T
private DLLNode<T> _nextNode, _prevNode; //pointers to next, prev DLLNodes
// constructor -- initializes instance vars
public DLLNode( T value, DLLNode<T> prev, DLLNode<T> next ) {
_cargo = value;
_nextNode = next;
_prevNode = prev;
}
//--------------v ACCESSORS v--------------
public T getCargo() { return _cargo; }
public DLLNode<T> getNext() { return _nextNode; }
public DLLNode<T> getPrev() { return _prevNode; }
//--------------^ ACCESSORS ^--------------
//--------------v MUTATORS v--------------
public T setCargo( T newCargo ) {
T car = getCargo();
_cargo = newCargo;
return car;
}
public DLLNode<T> setNext( DLLNode<T> newNext ) {
DLLNode<T> current = getNext();
_nextNode = newNext;
return current;
}
public DLLNode<T> setPrev( DLLNode<T> newPrev ) {
DLLNode<T> current = getPrev();
_prevNode = newPrev;
return current;
}
//--------------^ MUTATORS ^--------------
// override inherited toString
public String toString() { return _cargo.toString(); }
/*********************
//main method for testing
public static void main( String[] args ) {
//Below is an exercise in creating a linked list...
//Create a node
DLLNode<String> first = new DLLNode<String>( "cat", null );
//Create a new node after the first
first.setNext( new DLLNode<String>( "dog", null ) );
//Create a third node after the second
first.getNext().setNext( new DLLNode<String>( "cow", null ) );
DLLNode temp = first;
while( temp != null ) {
System.out.println( temp );
temp = temp.getNext();
}
}//end main
***********************/
}//end class DLLNode