Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 210:9166735b34c9
change List.delete
author | tatsuki |
---|---|
date | Tue, 28 Jul 2015 08:55:10 +0900 |
parents | e5275a599048 |
children | 74648c746dd4 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/Node.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/TailNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/headNode.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java |
diffstat | 6 files changed, 34 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java Tue Jul 28 08:55:10 2015 +0900 @@ -48,20 +48,21 @@ if (newNode == null) return null; - return new DefaultNode(this.attribute, newNode); + return new DefaultNode(this.attribute, newNode); } @Override - public Node<T> delete(int deleteNum) { - if (this.num - 1 == deleteNum) { - return new DefaultNode(this.attribute, deleteNum, this.next.getNext()); + public Node<T> delete(int currentNum, int deleteNum) { + currentNum++; + if (currentNum == deleteNum) { + return new DefaultNode(this.attribute, this.next.getNext()); } - Node<T> newNode = next.delete(deleteNum); + Node<T> newNode = next.delete(currentNum, deleteNum); if (newNode == null) return null; - return new DefaultNode(this.attribute, this.num - 1, newNode); + return new DefaultNode(this.attribute, newNode); } @Override @@ -76,4 +77,9 @@ return new DefaultNode(this.attribute, this.num, newNode); } + + @Override + public int length() { + return next.length() + 1; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Tue Jul 28 08:55:10 2015 +0900 @@ -9,11 +9,9 @@ */ public class List<T> implements Iterable<T> { final private Node<T> head; - final private int listLength; public List() { this.head = new headNode(); - this.listLength = 0; } public List(T... attributes) { @@ -22,12 +20,10 @@ list = list.addLast(attribute); } this.head = list.getHead(); - this.listLength = head.getNext().getNum() + 1; } private List(Node<T> head) { this.head = head; - this.listLength = head.getNext().getNum() + 1; } public Node<T> getHead() { @@ -35,7 +31,7 @@ } public List<T> add(int num, T attribute) { - Node<T> newHead = head.add(0,num, attribute); + Node<T> newHead = head.add(0, num, attribute); if (newHead == null) return this; return new List<T>(newHead); @@ -54,7 +50,7 @@ if (count == num) return currentNode.getAttribute(); currentNode = currentNode.getNext(); - count ++; + count++; } return null; } @@ -100,7 +96,7 @@ public List<T> delete(int num) { - Node<T> newNode = head.delete(num); + Node<T> newNode = head.delete(0, num); if (newNode == null) return this; Node<T> newHead = new headNode<>(newNode); @@ -123,7 +119,7 @@ } public List<T> deleteLast() { - return delete(listLength - 1); + return delete(head.length() - 1); } public List<T> deleteHead() { @@ -131,7 +127,7 @@ } public int length() { - return listLength; + return head.length(); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/Node.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/Node.java Tue Jul 28 08:55:10 2015 +0900 @@ -14,7 +14,9 @@ public Node<T> addLast(T attribute); - public Node<T> delete(int num); + public Node<T> delete(int currentNum, int num); - Node<T> replaceNode(int num, T attribute); + public Node<T> replaceNode(int num, T attribute); + + public int length(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/TailNode.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/TailNode.java Tue Jul 28 08:55:10 2015 +0900 @@ -26,7 +26,7 @@ } @Override - public Node<T> delete(int num) { + public Node<T> delete(int CurentNum, int num) { return null; } @@ -40,4 +40,8 @@ return new DefaultNode<T>(attribute, this); } + @Override + public int length() { + return 0; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/headNode.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/headNode.java Tue Jul 28 08:55:10 2015 +0900 @@ -43,12 +43,12 @@ return new headNode<T>(node); } - public Node<T> delete(int deleteNum) { - if (getNext().getNum() == deleteNum) { + public Node<T> delete(int currentNum, int deleteNum) { + if (currentNum == deleteNum) { return new headNode(this.next.getNext()); } - Node<T> newNode = next.delete(deleteNum); + Node<T> newNode = next.delete(currentNum, deleteNum); if (newNode == null) return this; return new headNode(newNode); @@ -67,4 +67,9 @@ return this; return new headNode(newNode); } + + @Override + public int length(){ + return next.length(); + } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java Tue Jul 28 08:32:58 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/deleteTest.java Tue Jul 28 08:55:10 2015 +0900 @@ -15,7 +15,6 @@ for (int count = 0; count < 10; count++) { list = list.addLast(count); } - List<Integer> newList = list.delete(5); Assert.assertEquals(newList.length(),9); newList = newList.deleteHead();