diff --git a/group24/626451284Learning/pom.xml b/group24/626451284Learning/pom.xml new file mode 100644 index 0000000000..534ff39232 --- /dev/null +++ b/group24/626451284Learning/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.github.wdn + coding2017 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..3b3ea048f3 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java @@ -0,0 +1,88 @@ +package com.github.wdn.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + public ArrayList(){ + elementData = new Object[10]; + } + public ArrayList(int size){ + elementData = new Object[size]; + } + public void add(Object o){ + if(size>=elementData.length){ + elementData = Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + Object[] newElementData; + if(size()+1>=elementData.length){ + newElementData=new Object[elementData.length*2]; + + }else{ + newElementData=new Object[elementData.length]; + } + for (int i = 0; i < elementData.length; i++) { + if(index==1){ + newElementData[i]=o; + }else if(i>index) { + newElementData[i]=elementData[i-1]; + }else{ + newElementData[i]=elementData[i]; + } + } + elementData = newElementData; + size++; + } + + public Object get(int index){ + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index) { + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + Object returnO = elementData[index]; + for (int i = index; i < size; i++) { + if(i==size-1){ + elementData[i]=null; + }else { + elementData[i] = elementData[i + 1]; + } + } + size--; + return returnO; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(2); + list.add("1"); + list.add("2"); + list.remove(1); + list.add("3"); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + int[] i = {}; + System.out.println(i[0]); + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b8b613d6f0 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 代码在公司电脑里。。。后续提交 + */ +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){ + return null; + } + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..044ddf0993 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java @@ -0,0 +1,10 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 主要考虑遍历中安全删除元素的实现 + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..6040cb5a47 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -0,0 +1,233 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 只是简单实现 缺少对边界的处理 + * 参考JDK源码改为更优雅的实现 + */ + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public LinkedList(){ + this.head=null; + this.tail=null; + } + public void add(Object o){ + Node newNode = new Node(o); + if (head == null) { + head = newNode; + tail = newNode; + }else{ + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + size++; + } + public void add(int index , Object o){ + checkIndex(index); + Node indexNode = getNode(index); + Node newNode = new Node(o); + Node pre = indexNode.getPre(); + if(pre!=null){ + pre.setNext(newNode); + }else{ + head = newNode; + } + newNode.setPre(pre); + newNode.setNext(indexNode); + indexNode.setPre(newNode); + } + private void checkIndex(int index){ + if(index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + private Node getNode(int index){ + checkIndex(index); + // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 + Node result=null; + if(index==0){ + return head; + } + if(index==size-1){ + return tail; + } + for (int i = 0; i < index; i++) { + result = head.getNext(); + } + return result; + } + public Object get(int index){ + return getNode(index).getData(); + } + public Object remove(int index){ + checkIndex(index); + Node indexNode = getNode(index); + Node pre = indexNode.getPre(); + Node next = indexNode.getNext(); + if(pre!=null){ + pre.setNext(next); + }else{ + head = next; + } + if(next!=null){ + next.setPre(pre); + }else{ + tail = pre; + } + return indexNode.getData(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o); + head.setPre(newNode); + newNode.setNext(head); + head = newNode; + } + public void addLast(Object o){ + Node newNode = new Node(o); + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + public Object removeFirst(){ + Node next = head.getNext(); + Node oldHead = head; + head = next; + head.setPre(null); + return oldHead; + } + public Object removeLast(){ + Node oldTail = tail; + Node pre = tail.getPre(); + tail = pre; + tail.setNext(null); + return oldTail; + } + public Iterator iterator(){ + + return null; + } + + /** + * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 + */ + private static class Node{ + + Object data; + Node next; + Node pre; + public Node(){ + + } + public Node(Object data){ + this.data = data; + } + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node getPre() { + return pre; + } + + public void setPre(Node pre) { + this.pre = pre; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和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){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java new file mode 100644 index 0000000000..d6c9e95cb0 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.wdn.coding2017.basic; + +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/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java new file mode 100644 index 0000000000..59992f0cbd --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java @@ -0,0 +1,24 @@ +package com.github.wdn.coding2017.basic; + +/** + * 队列使用链表实现比较简单理论上是无界队列 + * 如果使用数组需要处理很多问题比如长度限制,元素的复制 + */ +public class Queue { + private LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.remove(0); + } + + public boolean isEmpty(){ + return queue.size()==0; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java new file mode 100644 index 0000000000..f0a0fe56e9 --- /dev/null +++ b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java @@ -0,0 +1,27 @@ +package com.github.wdn.coding2017.basic; + +/** + * 使用list实现比较简单 + * 可以考虑使用其它结构 + */ +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(); + } +}