209
|
1 package jp.ac.u_ryukyu.ie.cr.jungle.data.list;
|
|
2
|
|
3 /**
|
|
4 * Created by e115731 on 15/05/18.
|
|
5 */
|
|
6 public class headNode<T> implements Node<T> {
|
|
7 private final Node next;
|
|
8
|
|
9
|
|
10 public headNode() {
|
|
11 this.next = new TailNode<T>();
|
|
12 }
|
|
13
|
|
14 public headNode(Node next) {
|
|
15 this.next = next;
|
|
16 }
|
|
17
|
|
18 public int getNum() {
|
|
19 return -1;
|
|
20 }
|
|
21
|
|
22 public Node getNext() {
|
|
23 return next;
|
|
24 }
|
|
25
|
|
26 public T getAttribute() {
|
|
27 return null;
|
|
28 }
|
|
29
|
|
30 public Node<T> add(int currentNum, int num, T attribute) {
|
|
31 if (num == 0) {
|
|
32 Node<T> newNode = new DefaultNode<>(attribute, next);
|
|
33 return new headNode<T>(newNode);
|
|
34 }
|
|
35 Node<T> newNode = next.add(currentNum + 1, num, attribute);
|
|
36 if (newNode == null)
|
|
37 return this;
|
|
38 return new headNode(newNode);
|
|
39 }
|
|
40
|
|
41 public Node<T> addLast(T attribute) {
|
|
42 Node<T> node = next.addLast(attribute);
|
|
43 return new headNode<T>(node);
|
|
44 }
|
|
45
|
210
|
46 public Node<T> delete(int currentNum, int deleteNum) {
|
|
47 if (currentNum == deleteNum) {
|
209
|
48 return new headNode(this.next.getNext());
|
|
49 }
|
|
50
|
211
|
51 Node<T> newNode = next.delete(currentNum + 1, deleteNum);
|
209
|
52 if (newNode == null)
|
|
53 return this;
|
|
54 return new headNode(newNode);
|
|
55 }
|
|
56
|
|
57 @Override
|
211
|
58 public Node<T> replaceNode(int currentNum, int num, T attribute) {
|
209
|
59 Node<T> nextNode = getNext();
|
211
|
60 Node<T> newNode = nextNode.replaceNode(currentNum, num, attribute);
|
209
|
61 if (newNode == null)
|
|
62 return this;
|
|
63 return new headNode(newNode);
|
|
64 }
|
210
|
65
|
|
66 @Override
|
211
|
67 public int length() {
|
210
|
68 return next.length();
|
|
69 }
|
209
|
70 }
|