diff --git a/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cbb7084045 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final int defaultGrowSize = 100; // 每次增长 100 个元素 + +// private int curIterIndex = 0; // 用于记录 Iterator的索引 + + private void CheckAndGrowUp() { + if (size+1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + defaultGrowSize); + } + } + + // 添加一个方法,检测看,添加元素的话,是否需要增长,专门用于数组长度扩展的 + public void add(Object o) { + CheckAndGrowUp(); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + // 先探测是否添加数组大小 + CheckAndGrowUp(); + // 保留后半部分,然后 全部替换即可 + + Object[] tmpObjectArr = Arrays.copyOfRange(elementData, index, elementData.length); + elementData[index] = o; + for (int i = index+1; i < size+1; i++) { + elementData[i] = tmpObjectArr[i-index-1]; + } + size++; + + } + + public Object get(int index) { + // 应该是 不需要跑出 下标越界异常的,因为数组越界会自动抛出 + return elementData[index]; + } + + public Object remove(int index) { + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size] = null; // 后续后面是 数值?那么就不能为null了,而是零? + size--; + return null; + } + + @Override + public int size() { + return size; + } + + // 覆盖toString方法,方便测试 + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < size; i++) { + sb.append(elementData[i]).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public Iterator iterator() { + + return new Iterator() { + int curIterIndex = 0; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return curIterIndex < size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[curIterIndex++]; + } + + }; + } + + public static void main(String[] args) { + ArrayList curArrList = new ArrayList(); + for (int i = 0; i <= 101; i++) { + curArrList.add(i); + } + System.err.println("Test add Arr"); + System.err.println(curArrList); + System.err.println("Test add in specific index"); + curArrList.add(10, 1010); + System.err.println(curArrList); + System.err.println("Test remove"); + curArrList.remove(10); + System.err.println(curArrList); + System.err.println("Test Iterator"); + Iterator curIter = curArrList.iterator(); + while(curIter.hasNext()){ + System.err.println(curIter.next()); + } + } + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..ff40538e64 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + // 应该只需要实现这个就可以了 + int curValue = (Integer) this.getData(); + int insertValue = (Integer) o; + + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + + if (curValue > insertValue) { + if (this.getLeft() != null) { + return this.getLeft().insert(o); + } else { + this.setLeft(newNode); + return this; + } + } else{ + if (this.getRight() != null) { + return this.getRight().insert(o); + } else { + this.setRight(newNode); + return this; + } + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(this.getData()).append("\n"); + sb.append(this.getLeft()).append("<--").append(this.getData()).append("\n"); + sb.append(this.getData()).append("-->").append(this.getRight()).append("\n"); + return sb.toString(); + } + + public static void main(String[] args) { + BinaryTreeNode btn = new BinaryTreeNode(); + btn.setData(5); +// btn.insert(5); + btn.insert(7); + btn.insert(8); + btn.insert(9); + btn.insert(4); + + System.err.println(btn); + } + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/com/github/CJ-chen/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..29ab5ecaf0 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Iterator.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..0364c16480 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java @@ -0,0 +1,423 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class LinkedList implements List { + + private Node head; + + private int size; +// private int curIterIndex; + + public LinkedList() { + head = new Node(); + size = 0; +// curIterIndex = 0; + } + + public void add(Object o) { + addLast(o); + } + + private Node getNode(int index) { + if (index == -1) { + return head; + } else { + Node returnNode = head.next; + for (int i = 0; i < index; i++) { + returnNode = returnNode.next; + } + return returnNode; + } + } + + public void add(int index, Object o) { + Node preNode = getNode(index - 1); + Node addNode = new Node(); + addNode.data = o; + addNode.next = preNode.next; + preNode.next = addNode; + size++; + } + + public Object get(int index) { + return getNode(index).data; + } + + public Object remove(int index) { + Node preNode = getNode(index - 1); + Node delNode = preNode.next; + preNode.next = delNode.next; + // 返回被删除的Node... 可能是为了测试吧 + size--; + return delNode; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node fNode = new Node(); + fNode.data = o; + fNode.next = head.next; + head.next = fNode; + size++; + } + + public void addLast(Object o) { +// System.err.println("Curr add: "+ o); + Node lastNode = getNode(size - 1); +// System.err.println(lastNode); + Node lNode = new Node(); + lNode.data = o; + lastNode.next = lNode; + size++; + } + + public Object removeFirst() { + return removeFirstNode().data; + } + + private Node removeFirstNode() { + Node firstNode = head.next; + head.next = firstNode.next; + size--; + return firstNode; + } + + public Object removeLast() { + return removeLastNode().data; + } + + private Node removeLastNode() { + Node last2Node = getNode(size - 2); + Node lastNode = last2Node.next; + last2Node.next = null; + size--; + return lastNode; + } + + public Iterator iterator() { + return new Iterator() { +// int curIterIndex = 0; + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode.next != null; + } + + @Override + public Object next() { + curNode = curNode.next; + return curNode.data; + } + }; + } + + private static class Node { + + Object data; + Node next; + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + int oriSize = size; + Node newHead = new Node(); + newHead.next = this.removeLastNode(); + Node preNode = newHead.next; + + while (size > 0) { + preNode.next = this.removeLastNode(); + preNode = preNode.next; + } + // 不考虑线程安全的情况下,恢复size + size = oriSize; + head = newHead; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + int reDirIndex = size / 2; // java会自动 取 0 + size = size - reDirIndex; + System.err.println(reDirIndex); + Node jumpNode = getNode(reDirIndex); + head.next = jumpNode; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + Node fromNode = getNode(i - 1); + for (int j = 0; j < length; j++) { + fromNode.next = fromNode.next.next; + size--; + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ +// public static int[] getElements(LinkedList list) { + // 这个似乎 不应该是 静态方法 + public int[] getElements(LinkedList list) { + int[] returnIndex = new int[list.size]; + int validCounts = 0; +// Iterator curIter = list.iterator(); +// int curIndex = 0; +// while(curIter.hasNext()){ +// returnIndex[curIndex++] =(Integer) this.get((Integer) curIter.next()); +// } + + // 已知是内外都是玩去排序好的,所以是需要一个高效的算法 + Iterator innerIter = this.iterator(); + Iterator curIter = list.iterator(); + int curCount = 0; + int curIndex; + int preIndex = 0; + + while (curIter.hasNext()) { + curIndex = (Integer) curIter.next(); + if (curIndex < preIndex) { + System.err.println("Warning: Skip index no in sorted order..."); + continue; + } +// int skipCounts = curIndex-preIndex; + for (int i = preIndex; i < curIndex; i++) { + innerIter.next(); + } + validCounts++; + returnIndex[curCount++] = (Integer) innerIter.next(); + preIndex = curIndex + 1; + } + return Arrays.copyOf(returnIndex, validCounts); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode.next = curNode.next; + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (curNode.data == preNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + // 假定 当前链表 是 从小到大 排列的 + Node preNode = head; + Node curNode; + while (preNode.next != null) { + curNode = preNode.next; + if (min < (Integer) curNode.data && max > (Integer) curNode.data) { + preNode.next = curNode.next; + } else { + preNode = curNode; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + + + LinkedList newList = new LinkedList(); + + // 假定 当前 链表 和 待删除 链表 都是顺序排序的 + Node preNode = head; + Node curNode; + Iterator element2rmIter = list.iterator(); + while (element2rmIter.hasNext()) { + int curValue2rm = (Integer) element2rmIter.next(); + while (preNode.next != null) { + curNode = preNode.next; + if ((Integer) curNode.data == curValue2rm) { + // 删除 + preNode = curNode; + newList.add(curNode.data); // 添加data + }else if((Integer) curNode.data > curValue2rm){ + break; // 跳出内层循环,从而获取下一个待删除数据 + }else { + // 更新 + preNode = curNode; + } + } + } + + + return newList; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("LinkdedList ["); + Node curNode = head; + while (curNode.next != null) { + curNode = curNode.next; + sb.append(curNode.data).append(", "); + } + sb.delete(sb.length() - 2, sb.length()).append("]"); + return sb.toString(); + } + + public void clear() { + head.next = null; + size = 0; + } + + public static void main(String[] args) { + // 先写一些测试 + LinkedList ll = new LinkedList(); + System.err.println("Test Add"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println("Size: " + ll.size); + System.err.println(ll); + System.err.println("Test Add last"); + ll.addLast(10); + System.err.println(ll); + System.err.println("Test Add First"); + ll.addFirst(-1); + System.err.println(ll); + System.err.println("Test remove "); + ll.remove(0); + System.err.println(ll); +// ll.remove(11); +// System.err.println(ll); + ll.removeFirst(); + System.err.println(ll); + ll.removeLast(); + System.err.println(ll); + System.err.println("Test reverse()"); + ll.reverse(); + System.err.println(ll); + System.err.println("Test removeFirstHalf()"); + ll.removeFirstHalf(); + System.err.println(ll); +// System.err.println(9/2); + System.err.println("Test remove()"); + ll.remove(1, 2); + System.err.println(ll); +// System.err.println(ll.); + LinkedList newList = new LinkedList(); + newList.add(3); + newList.add(4); + newList.add(6); + newList.add(5); +// System.err.println(); + ll.clear(); + System.err.println("Re Init"); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + System.err.println(ll); + for (int curI : ll.getElements(newList)) { + System.err.println(curI); + } + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + ll.add(i); + ll.add(i); + } + System.err.println(ll); + ll.removeDuplicateValues(); + System.err.println(ll); + System.err.println("Test Remove removeRange(3, 6)"); + ll.removeRange(3, 6); + System.err.println(ll); + + LinkedList rmList = new LinkedList(); + rmList.add(3); + rmList.add(4); + rmList.add(7); + rmList.add(8); + ll.subtract(rmList); + System.err.println("Test subtract(3,4,7,8)"); + System.err.println(ll); + ll.clear(); + for (int i = 0; i < 10; i++) { + ll.add(i); + } + rmList.add(11); + System.err.println(ll); + System.err.println(rmList); + System.err.println(ll.intersection(rmList)); +// System.err.println("Test substract"); +// ll.subtract(newList); +// System.err.println(ll); + + } + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/List.java b/group24/com/github/CJ-chen/coding2017/basic/List.java new file mode 100644 index 0000000000..d347e59c47 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/List.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/com/github/CJ-chen/coding2017/basic/Queue.java new file mode 100644 index 0000000000..4cbc45e065 --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Queue.java @@ -0,0 +1,58 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public class Queue { + + private final LinkedList innerList; + + public Queue(){ + innerList = new LinkedList(); + } + + public void enQueue(Object o) { + innerList.addLast(o); + } + + public Object deQueue() { + return innerList.removeFirst(); + } + + public boolean isEmpty() { + return innerList.size()==0; + } + + public int size() { + return innerList.size(); + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < innerList.size(); i++) { + sb.append(innerList.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + + public static void main(String[] args) { + Queue newQ = new Queue(); + for(int i=0;i<10;i++){ + newQ.enQueue(i); + } + System.err.println(newQ); + newQ.deQueue(); + System.err.println(newQ); + } + + +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/com/github/CJ-chen/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3ba85c6bcf --- /dev/null +++ b/group24/com/github/CJ-chen/coding2017/basic/Stack.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +/** + * + * @author CJ + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(elementData.size()-1); + } + + public Object peek() { + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty() { + return elementData.size()==0; + } + + public int size() { + return elementData.size(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("ArrayList: ["); + for (int i = 0; i < elementData.size(); i++) { + sb.append(elementData.get(i)).append(", "); + } +// System.err.println(size); + sb.delete(sb.length()-2,sb.length()).append("]"); + return sb.toString(); + } + public static void main(String[] args) { + Stack newS = new Stack(); + for(int i=0;i<10;i++){ + newS.push(i); + } + System.err.println("Test push()"); + System.err.println(newS); + newS.pop(); + System.err.println("Test pop()"); + System.err.println(newS); + System.err.println("Test peek()"); + System.err.println(newS.peek()); + System.err.println(newS); + + } + +}