diff --git a/group05/1377699408/.gitignore b/group05/1377699408/.gitignore new file mode 100644 index 0000000000..5deea7e781 --- /dev/null +++ b/group05/1377699408/.gitignore @@ -0,0 +1,4 @@ +/bin +.classpath +.project +.settings/ diff --git "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" new file mode 100644 index 0000000000..f015ad8623 --- /dev/null +++ "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" @@ -0,0 +1,54 @@ +package list; + +import java.util.Arrays; + +public class ArrayList { + private transient static int INITIAL_SIZE = 10; + private transient int arrayLength; + private transient int size; + private transient E[] array; + public ArrayList(){ + array = (E[]) new Object[INITIAL_SIZE]; + arrayLength = INITIAL_SIZE; + } + public ArrayList(int size){ + if(size<=0){ + throw new IllegalArgumentException("参数不可以小于0"); + } + array = (E[])new Object[size]; + arrayLength = array.length; + ensureCapacity(size); + this.size = size; + } + public int size(){ + return size; + } + public void add(E e){ + ensureCapacity(size+1); + array[size] = e; + size++; + } + public E get(int index){ + if(index<0 || index > size){ + throw new IllegalArgumentException("索引越界"); + } + return array[index]; + + } + public E set(int index, E e){ + if(index<0 || index>size){ + throw new IllegalArgumentException("索引越界"); + } + E result = array[index]; + array[index] = e; + return result; + } + private void ensureCapacity(int size){ + E[] oldArray = array; + int oldSize = arrayLength; + while(size>arrayLength){ + arrayLength = arrayLength + (arrayLength >> 1); + array = Arrays.copyOf(oldArray, arrayLength); + } + } +} diff --git "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" new file mode 100644 index 0000000000..e0d277eb10 --- /dev/null +++ "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" @@ -0,0 +1,59 @@ +package test; + +import junit.framework.TestCase; +import list.ArrayList; + +public class ArrayListTest extends TestCase { + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testArrayList() { + ArrayList a = new ArrayList(); + assertEquals(a.size(), 0); + } + + public void testArrayListInt() { + ArrayList a = new ArrayList(9); + assertEquals(a.size(), 9); + } + + public void testSize() { + ArrayList a = new ArrayList(9); + assertEquals(a.size(), 9); + ArrayList a2 = new ArrayList(100); + assertEquals(a2.size(), 100); + } + + public void testAdd() { + ArrayList a = new ArrayList(); + for (int i = 0; i < 1000; i++) { + a.add(5); + assertEquals(a.size(), i+1); + assertEquals(a.get(i), new Integer(5)); + } + } + + public void testGet() { + ArrayList a = new ArrayList(); + a.add(6); + assertEquals(a.get(0), new Integer(6)); + + } + + public void testSet() { + ArrayList a = new ArrayList(); + for (int i = 0; i < 100; i++) { + a.add(56); + } + a.set(5, 66); + assertEquals(a.get(5), new Integer(66)); + assertEquals(a.get(7), new Integer(56)); + } + +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" new file mode 100644 index 0000000000..620e8ead95 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" @@ -0,0 +1,119 @@ +package com.coding.basic; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + //默认容量 + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList(int capacity){ + if(capacity >= 0){ + elementData = new Object[capacity]; + }else { + throw new IllegalArgumentException("Illegal Capacity: " + + capacity); + } + + } + public ArrayList(){ + this(DEFAULT_CAPACITY); + } + + /** + * 保证集合容量 + * @param minCapacity + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + //扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; + if(minCapacity - newCapacity > 0){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + private void checkIndexRange(int index) + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException("Index out of bounds, index : " + index); + } + } + public void add(E o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + public void add(int index, E o){ + checkIndexRange(index);//检查下标 + ensureCapacity(size+1);//保证数组容量 + System.arraycopy(elementData,index,elementData,index + 1,size-index);//数组复制,把index后的元素全部向后移一位 + elementData[index] = o;//插入元素值 + size++;//元素size加一 + } + + @Override + public E get(int index) { + checkIndexRange(index);//检查下标 + return (E)elementData[index]; + } + + @Override + public E remove(int index) { + E e = this.get(index); + int numMoved = size - index - 1; + if(numMoved > 0) + { + System.arraycopy(elementData, index+1, elementData, index, numMoved);//数组复制,把index后的元素全部向前移一位 + } + elementData[--size] = null;//最后一位赋值为null,size-1 + return e; + } + + + public int size(){ + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + + private Object [] array; + private int endIndex = 0; + private int index = 0; + + public ArrayListIterator(ArrayList list){ + this.array=list.elementData; + this.endIndex = list.size(); + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + return (E)Array.get(this.array, this.index++); + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" new file mode 100644 index 0000000000..a1653b6e9f --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" @@ -0,0 +1,68 @@ +package com.coding.basic; + + +public class BinaryTreeNode implements Comparable { + public BinaryTreeNode(Object data) { + this.data = data; + } + + 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) { + //左子节点的值永远比父节点的值小 + //右子节点的值永远比父节点的值大 + BinaryTreeNode node = new BinaryTreeNode(o); + insertNode(node); + return node; + } + private void insertNode(BinaryTreeNode node){ + insertNode(this,node); + } + private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { + if (parentNode.compareTo(node) <= 0) {//数字大于父节点 + if (parentNode.right == null) { + parentNode.right = node; + return; + } + insertNode(parentNode.right, node); + } else { + if (parentNode.left == null) { + parentNode.left = node; + return; + } + insertNode(parentNode.left, node); + } + } + + @Override + public int compareTo(BinaryTreeNode o) { + Integer i = (Integer) this.data; + return i.compareTo((Integer) o.data); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" new file mode 100644 index 0000000000..09e5b73661 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" new file mode 100644 index 0000000000..208fa8390d --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by Mori on 2017/2/21. + */ +public class JavaTest { + @Test + public void testBinaryTreeNode(){ + BinaryTreeNode node = new BinaryTreeNode(5); + node.insert(4);//左 + node.insert(7);//右 + node.insert(2);//左左 + node.insert(6);//右左 + node.insert(5);//右左左 + node.insert(6);//右左右 + System.out.println(node.getData()); + System.out.println(node.getLeft().getData()); + System.out.println(node.getRight().getData()); + System.out.println(node.getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getData()); + System.out.println(node.getRight().getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getRight().getData()); + } + @Test + public void testArrayList(){ + ArrayList list =new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(5); + Assert.assertEquals((Object) list.get(2),3); + Assert.assertEquals((Object) list.remove(2),3); + Assert.assertEquals((Object) list.get(2),5); + Iterator listIterator = list.iterator(); + while (listIterator.hasNext()){ + System.out.println(listIterator.next()); + } + } + @Test + public void testLinkedList(){ + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(5); + linkedList.addFirst(10); + linkedList.add(1,6); + // linkedList.removeLast(); + //linkedList.removeFirst(); + Iterator linkedListIterator = linkedList.iterator(); + while (linkedListIterator.hasNext()){ + System.out.println(linkedListIterator.next()); + } + System.out.println("----"); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList.remove(2)); + //System.out.println(linkedList.get(3)); + //System.out.println(linkedList.get(4)); + } +} \ No newline at end of file diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" new file mode 100644 index 0000000000..760fec91e9 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" @@ -0,0 +1,165 @@ +package com.coding.basic; + +import jdk.nashorn.internal.ir.ReturnNode; + +import java.lang.reflect.Array; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private int size;//记录当前元素的size + + public void add(E e) { + Node node = new Node(e); + if (head == null) { + head = node; + } else { + tail.next = node; + } + tail = node; + tail.next = null; + size++; + } + + private void checkIndexRange(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(int index, E e) { + checkIndexRange(index); + Node node = new Node(e); + Node temp = head; + Node temp2 = null; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + temp2 = temp.next; + temp.next = node; + node.next = temp2; + size++; + } + + @Override + public E get(int index) { + checkIndexRange(index); + Node temp = head; + + for (int i = 0; i < index; i++) { + temp = temp.next; + } + return (E) temp.data; + } + + @Override + public E remove(int index) { + checkIndexRange(index); + if (index == 0) { + E e = removeFirst(); + return e; + } + if (index == size) { + E e = removeLast(); + return e; + } + Node temp = head; + Node temp2 = null; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + E e = (E) temp.next.data; + temp2 = temp.next.next; + temp.next = null; + temp.next = temp2; + size--; + return e; + } + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + Node node = new Node(e); + node.next = head; + head = node; + size++; + } + + public void addLast(E e) { + this.add(e); + } + + public E removeFirst() { + E e = (E) head.data; + head = head.next; + size--; + return e; + } + + public E removeLast() { + Node temp = head; + for (int i = 0; i < size - 1; i++) { + temp = temp.next; + } + temp.next = null; + E e = (E) tail.data; + tail = temp; + size--; + return e; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + E data; + Node next; + public Node(E e) { + this.data = e; + } + } + private class LinkedListIterator implements Iterator{ + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private Node node;//当前遍历的node + private int index; + private int endIndex; + + public LinkedListIterator(LinkedList list){ + this.head=list.head; + this.tail=list.tail; + this.endIndex = list.size(); + node=head; + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + if(index == 0){ + node = head; + }else { + node = node.next; + } + index++; + return (E)node.data; + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" new file mode 100644 index 0000000000..899ba2bd3e --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void add(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); + boolean isEmpty(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" new file mode 100644 index 0000000000..a8d5741846 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * 进队列 + * @param o + */ + public void enQueue(E o){ + elementData.addLast(o);//添加到队尾 + } + + /** + * 出队列 + * @return + */ + public E deQueue(){ + return elementData.removeFirst();//移除队首 + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" new file mode 100644 index 0000000000..de407c8548 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + public void push(E o){ + elementData.add(o); + } + + /** + * 移除栈顶并返回他 + * @return + */ + public E pop(){ + return elementData.remove(elementData.size()-1); + } + /** + * 得到栈顶元素 + * @return + */ + public E peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.isEmpty(); + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/205301442/src/com/coding/week1/ArrayList.java b/group10/205301442/src/com/coding/week1/ArrayList.java new file mode 100644 index 0000000000..db50f7ef46 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/ArrayList.java @@ -0,0 +1,96 @@ +package com.coding.week1; + + + + +public class ArrayList implements List{ + private int size = 0; + + private Object[] elementData = {}; + + public void add(Object o){ + extendIndex(); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + + if(index>size){ + System.out.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return; + } + int length = elementData.length; + if(size==elementData.length){ + length=elementData.length+1; + } + Object[] newElement = new Object[length]; + System.arraycopy(elementData, 0, newElement, 0, index); + System.arraycopy(elementData, index, newElement,index+1,size-index ); + newElement[index]=o; + elementData = newElement; + size++; + } + + public Object get(int index){ + boolean isRange=rangeCheck(index); + if(!isRange){ + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + boolean isRange=rangeCheck(index); + if(!isRange){ + return null; + } + Object rmData = elementData[index]; + Object[] newElement = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElement, 0, index);; + System.arraycopy(elementData, index+1, newElement,index,size-index-1 ); + elementData = newElement; + size--; + return rmData; + } + + public int size(){ + return size; + } + + public com.coding.week1.Iterator iterator(){ + return new Ito(); + } + public boolean rangeCheck(int index){ + + if(index>size-1||index<0){ + System.err.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return false; + } + return true; + } + public void extendIndex(){ + Object[] newElement = new Object[elementData.length+1]; + System.arraycopy(elementData, 0, newElement, 0, size); + elementData = newElement; + + } + public class Ito implements com.coding.week1.Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o=elementData[cursor]; + cursor++; + return o; + } + } + + +} diff --git a/group10/205301442/src/com/coding/week1/BinaryTreeNode.java b/group10/205301442/src/com/coding/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..547e09d42f --- /dev/null +++ b/group10/205301442/src/com/coding/week1/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package com.coding.week1; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + public BinaryTreeNode(Object data){ + this.data = data; + } + 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){ + BinaryTreeNode newBTN = new BinaryTreeNode(o); + Integer insert = (Integer)o; + + BinaryTreeNode cursor = this; + while(true){ + if(insert.compareTo((Integer)cursor.data)==-1){ + if(cursor.left==null){ + cursor.left = newBTN; + break; + } + cursor = cursor.left; + }else if(insert.compareTo((Integer)cursor.data)==1){ + if(cursor.right==null){ + cursor.right = newBTN; + break; + } + cursor = cursor.right; + } + } + return newBTN; + } + +} diff --git a/group10/205301442/src/com/coding/week1/Iterator.java b/group10/205301442/src/com/coding/week1/Iterator.java new file mode 100644 index 0000000000..7f6f333443 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.week1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group10/205301442/src/com/coding/week1/LinkedList.java b/group10/205301442/src/com/coding/week1/LinkedList.java new file mode 100644 index 0000000000..2c14a0e9cb --- /dev/null +++ b/group10/205301442/src/com/coding/week1/LinkedList.java @@ -0,0 +1,162 @@ +package com.coding.week1; + +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + public static class Node{ + Object data; + Node next; + Node prev; + public Node(Node prev,Object data,Node next){ + this.data = data; + this.next = next; + this.prev = prev; + } + } + + @Override + public void add(Object o) { + final Node l = last; + Node newNode = new Node(last,o,null); + last = newNode; + if(first==null){ + first = newNode; + }else{ + l.next = newNode; + } + size++; + + } + + @Override + public void add(int index, Object o) { + + if(index>size){ + System.out.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return; + } + Node newNode = new Node(null,o,null); + Node nodePre = node(index-1); + Node oldNode = node(index); + if(nodePre!=null){ + nodePre.next =newNode; + newNode.prev = nodePre; + }else{ + first = newNode; + } + if(oldNode!=null){ + oldNode.prev = newNode; + newNode.next = oldNode; + }else{ + last = newNode; + } + size++; + } + + @Override + public Object get(int index) { + if(!rangeCheck(index)){ + return null; + } + + return node(index).data; + } + + @Override + public Object remove(int index) { + if(!rangeCheck(index)){ + return null; + } + Node prevNode = node(index-1); + Node nextNode = node(index+1); + Node rmNode = node(index); + if(prevNode!=null){ + prevNode.next = nextNode; + }else{ + first=nextNode; + } + if(nextNode!=null){ + nextNode.prev = prevNode; + }else{ + last = prevNode; + } + size--; + return rmNode.data; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return size; + } + public Object head(){ + return first.data; + } + public Object last(){ + return last.data; + } + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + Node f = first; + remove(0); + return f.data; + } + public Object removeLast(){ + Node l = last; + remove(size-1); + return l.data; + } + public Node node(int index){ + if(index<0){ + return null; + } + Node x =null; + if(index<(size<<1)){ + x = first; + for(int i=0;iindex;i--){ + x = x.prev; + } + } + return x; + } + public boolean rangeCheck(int index){ + + if(index>size-1||index<0){ + System.err.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return false; + } + return true; + } + public Ito iterator(){ + return new Ito(); + } + public class Ito implements Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o=node(cursor).data; + cursor++; + return o; + } + + } +} diff --git a/group10/205301442/src/com/coding/week1/List.java b/group10/205301442/src/com/coding/week1/List.java new file mode 100644 index 0000000000..e06b7c8ab9 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/List.java @@ -0,0 +1,9 @@ +package com.coding.week1; + +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/group10/205301442/src/com/coding/week1/Queue.java b/group10/205301442/src/com/coding/week1/Queue.java new file mode 100644 index 0000000000..e3c4a44a42 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Queue.java @@ -0,0 +1,23 @@ +package com.coding.week1; + +public class Queue { + ArrayList list = new ArrayList(); + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + return list.remove(0); + } + + public boolean isEmpty(){ + if(list.size()==0){ + return true; + } + return false; + } + + public int size(){ + return list.size(); + } +} diff --git a/group10/205301442/src/com/coding/week1/Stack.java b/group10/205301442/src/com/coding/week1/Stack.java new file mode 100644 index 0000000000..307b3630df --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Stack.java @@ -0,0 +1,28 @@ +package com.coding.week1; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int top = elementData.size()-1; + return elementData.remove(top); + } + + public Object peek(){ + int top = elementData.size()-1; + return elementData.get(top); + } + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/205301442/test/com/coding/week1/AllTests.java b/group10/205301442/test/com/coding/week1/AllTests.java new file mode 100644 index 0000000000..fb54214d4d --- /dev/null +++ b/group10/205301442/test/com/coding/week1/AllTests.java @@ -0,0 +1,11 @@ +package com.coding.week1; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ TestArrayList.class, TestLinkedList.class, TestQueue.class, TestStack.class,TestBiranyTreeNode.class }) +public class AllTests { + +} diff --git a/group10/205301442/test/com/coding/week1/TestArrayList.java b/group10/205301442/test/com/coding/week1/TestArrayList.java new file mode 100644 index 0000000000..0c7040d335 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestArrayList.java @@ -0,0 +1,31 @@ +package com.coding.week1; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class TestArrayList { + ArrayList arrayList =new ArrayList(); + + @Test + public void Test(){ + //add + arrayList.add("MM"); + arrayList.add(1,"YY"); + arrayList.add(2,"ZZ"); + //get + assertEquals((String)arrayList.get(0), "MM"); + assertEquals(arrayList.size(),3 ); + //remove + assertEquals(arrayList.remove(2), "ZZ"); + assertEquals(arrayList.size(),2 ); + //iterator + Iterator ito = arrayList.iterator(); + int i=0; + while(ito.hasNext()){ + assertEquals(ito.next(), arrayList.get(i)); + i++; + } + } + + +} diff --git a/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java b/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java new file mode 100644 index 0000000000..5968ebf562 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java @@ -0,0 +1,21 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestBiranyTreeNode { + + @Test + public void test() { + BinaryTreeNode node = new BinaryTreeNode(5); + node.insert(2); + node.insert(7); + node.insert(1); + node.insert(6); + System.out.println(" "+node.getData()); + System.out.println(" "+node.getLeft().getData()+" "+node.getRight().getData()); + System.out.println(node.getLeft().getLeft().getData()+" null "+node.getRight().getLeft().getData()+" null"); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestLinkedList.java b/group10/205301442/test/com/coding/week1/TestLinkedList.java new file mode 100644 index 0000000000..db6524ac53 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestLinkedList.java @@ -0,0 +1,42 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + + +import org.junit.Test; + +public class TestLinkedList { + LinkedList linkedList = new LinkedList(); + + @Test + public void test() { + //add + linkedList.add("AA"); + linkedList.add(0,"BB"); + linkedList.add(1,"CC"); + linkedList.add(3,"DD"); + + assertEquals(linkedList.get(0), "BB"); + assertEquals(linkedList.get(1), "CC"); + assertEquals(linkedList.get(2), "AA"); + assertEquals(linkedList.last(), "DD"); + //add first last + linkedList.addFirst("EE"); + assertEquals(linkedList.get(0), "EE"); + linkedList.addLast("FF"); + assertEquals(linkedList.get(5), "FF"); + //remove + assertEquals(linkedList.remove(1), "BB"); + assertEquals(linkedList.removeFirst(), "EE"); + assertEquals(linkedList.removeLast(), "FF"); + //iterator + Iterator ito = linkedList.iterator(); + int i=0; + while(ito.hasNext()){ + assertEquals(linkedList.get(i), ito.next()); + i++; + } + assertEquals(i, linkedList.size()); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestQueue.java b/group10/205301442/test/com/coding/week1/TestQueue.java new file mode 100644 index 0000000000..525eaf5886 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestQueue.java @@ -0,0 +1,17 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestQueue { + + @Test + public void test() { + Queue queue = new Queue(); + queue.enQueue("MM"); + assertEquals(queue.deQueue(), "MM"); + assertEquals(queue.isEmpty(), true); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestStack.java b/group10/205301442/test/com/coding/week1/TestStack.java new file mode 100644 index 0000000000..ababf23df6 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestStack.java @@ -0,0 +1,23 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestStack { + + @Test + public void test() { + Stack stack = new Stack(); + stack.push("AA"); + stack.push("BB"); + stack.push("CC"); + stack.push("DD"); + assertEquals(stack.pop(), "DD"); + assertEquals(stack.pop(), "CC"); + assertEquals(stack.peek(), "BB"); + assertEquals(stack.size(), 2); + assertEquals(stack.isEmpty(),false); + } + +} diff --git "a/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..fab8d99a58 --- /dev/null +++ "b/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +first week http://blog.csdn.net/kellyfun/article/details/57504808 \ No newline at end of file diff --git a/group10/3314793852/.classpath b/group10/3314793852/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group10/3314793852/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/3314793852/.gitignore b/group10/3314793852/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/3314793852/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/3314793852/.project b/group10/3314793852/.project new file mode 100644 index 0000000000..e09255853a --- /dev/null +++ b/group10/3314793852/.project @@ -0,0 +1,17 @@ + + + FirstWeek + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/3314793852/src/myList/MyArrayList.java b/group10/3314793852/src/myList/MyArrayList.java new file mode 100644 index 0000000000..7b5a883190 --- /dev/null +++ b/group10/3314793852/src/myList/MyArrayList.java @@ -0,0 +1,103 @@ +package myList; + +/* + * ArrayListĵײһ飬ͨ´ķ̬ + * ArrayListղݡ + */ + +public class MyArrayList { + private int theSize; //ǰС + private static final int DEFAULT_CAPACITY=10; //Ĭ + private Object[] theArr=new Object[10]; //ײ + + //ʼ + public MyArrayList(){ + clear(); + } + + // + public void clear(){ + theSize=0; + capacityBigger(DEFAULT_CAPACITY); + } + + //ȡС + public int size(){ + return theSize; + } + + //ȡײ + public Object[] getArr(){ + return theArr; + + } + //룬ֱӲ뵽β + public void add(Object a){ + add(theSize, a); + } + + //±ȡ + public Object get(int i){ + if(i<0||i>=theSize){ + throw new ArrayIndexOutOfBoundsException(); + } + return theArr[i]; + } + + //룬ָ±롣 + public void add(int i,Object a){ + + if(theSize==theArr.length){ //ʼΪ10ÿɹһʱsize+1,sizeĴСͬʱ󷽷̬ĴС + capacityBigger(size()); + } + for(int j=theSize-1;j>=i;j--){ + theArr[j+1]=theArr[j]; + } + theArr[i]=a; + + theSize++; + } + + //ɾ,±ɾݡ + public void remove(int i){ + + for(int j=i;j0){ + return contains(x,aNode.left); + } + else if(comparaResult<0){//ݴڵǰڵʱӦڵǰڵҺӽڵС + return contains(x,aNode.right); + } + else{ //ݵڵǰڵʱӦڵǰڵС + return true; + } + } + + //ݡ + public void insert(Object x){ + root=insert(x,root); + } + + public BinaryNode insert(Object x,BinaryNode aNode){ + + if(aNode==null){//ǰΪµݽڵ㣬ΪҶӽڵ㣬ҽڵΪnull. + return new BinaryNode(x,null,null); + } + + //͵ǰĽڵбȽϡ + Integer comparaResult=(Integer)aNode.element-(Integer)x; + + //СڵǰڵʱӦڵǰڵӽڵС + if(comparaResult>0){ + aNode.left= insert(x,aNode.left); + } + else if(comparaResult<0){//ݴڵǰڵʱӦڵǰڵҺӽڵС + aNode.right=insert(x,aNode.right); + } + else{ //ݵڵǰڵʱӦڵǰڵ,κβ + ; + } + return aNode; + } + + //ӡ + public void getData(){ + getData(root); + } + public void getData(BinaryNode root){ + if (root != null) { + // + this.getData(root.left); + + //Һ + this.getData(root.right); + //ڵ + this.print(root); + } + + } + + //ӡڵ㡣 + public void print(BinaryNode root){ + System.out.println( + (Integer)(root.element) + ); + } + } diff --git a/group10/3314793852/src/myList/MyIterator.java b/group10/3314793852/src/myList/MyIterator.java new file mode 100644 index 0000000000..8c97809af8 --- /dev/null +++ b/group10/3314793852/src/myList/MyIterator.java @@ -0,0 +1,40 @@ + + package myList; + + public class MyIterator { + + private Object aData; + private int i=0; + private int l=0; + MyLinkedList.Node node; + public MyIterator(Object aDate){ + this.aData=aDate; + } + + public boolean hasNext(){ + if(aData instanceof MyArrayList){//MyArrayListIterator + + Object[] arr=((MyArrayList) aData).getArr(); + int a=((MyArrayList)aData).size(); + return a>i; + } + else{//MyLinkedListIterator + node=((MyLinkedList)aData).getHeadNode();//ͷڵ + int a=((MyLinkedList)aData).size(); + return a>l; + } + + + } + public Object next(){ + if(aData instanceof MyArrayList){//MyArrayListIterator + + Object[] arr=((MyArrayList) aData).getArr(); + return arr[++i]; + } + else{//MyLinkedListIterator + l++; + return node.getDate(); + } + } + } diff --git a/group10/3314793852/src/myList/MyLinkedList.java b/group10/3314793852/src/myList/MyLinkedList.java new file mode 100644 index 0000000000..0bad8c8953 --- /dev/null +++ b/group10/3314793852/src/myList/MyLinkedList.java @@ -0,0 +1,122 @@ + package myList; + + /* + * õͷڵġ + */ + + public class MyLinkedList { + + private int theSize; //ĴС + private Node headNode; //ͷڵ + + //ڵࡣ + public static class Node{ + + private Object data; + private Node node; + + public Node(){ + + } + + public Node(Object data, Node node) { + this.data = data; + this.node = node; + } + public Object getDate(){ + return data; + } + + } + + //췽ʼʱһͷڵĿյ + public MyLinkedList(){ + clear(); + } + + //ͷڵ + public Node getHeadNode(){ + return headNode; + } + + // + public void clear(){ + headNode=new Node(null,null); //ͷʼdateָnodeȫΪnull. + theSize=0; //ĴС + } + + //ȡĴС + public int size(){ + return theSize; + } + + //ӽڵ㵽β + public void add(Object aData){ + add(theSize+1,aData); + } + + //ӽڵ㵽ָλá + public void add(int idx,Object aDate){ + + //һµĽڵ + Node newNode=new Node(); + newNode.data=aDate; + + //ҵָλõĽڵ㣬½ڵ嵽ָλýڵǰһλá + Node p,q; + p=headNode; + + for(int i=1;i(arr.length-1)){//tailѾβʱͷΪʱµ뵽ͷ + tail=0; + } + theSize++; + } + } + + //pop,С + public Object pop(){ + Object a=null; + if(theSize!=0){ //ΪգܽгеIJ + a=arr[head]; + arr[head]=null; + head++; + if(head>(arr.length-1)){ + head=0; + } + theSize--; + } + return a; + } + + //ӡС + public void print(){ + for(int i=0;i + + + + + + + diff --git a/group10/353261578/.gitignore b/group10/353261578/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/353261578/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/353261578/.project b/group10/353261578/.project new file mode 100644 index 0000000000..251ba32e96 --- /dev/null +++ b/group10/353261578/.project @@ -0,0 +1,17 @@ + + + 353261578Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/353261578/src/com/sx/structures/BinaryNode.java b/group10/353261578/src/com/sx/structures/BinaryNode.java new file mode 100644 index 0000000000..a5d2e45bdd --- /dev/null +++ b/group10/353261578/src/com/sx/structures/BinaryNode.java @@ -0,0 +1,44 @@ +package com.sx.structures; + +public class BinaryNode implements Comparable{ + private Object data; + private BinaryNode left; + private BinaryNode right; + + public BinaryNode() { + } + public BinaryNode(Object o){ + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryNode getLeft() { + return left; + } + public void setLeft(BinaryNode left) { + this.left = left; + } + public BinaryNode getRight() { + return right; + } + public void setRight(BinaryNode right) { + this.right = right; + } + @Override + public int compareTo(BinaryNode o) { + Integer to = (Integer)this.data; + Integer co = (Integer)o.data; + if(toco) + return 1; + return 0; + } +} diff --git a/group10/353261578/src/com/sx/structures/BinaryTree.java b/group10/353261578/src/com/sx/structures/BinaryTree.java new file mode 100644 index 0000000000..7af8f8edd5 --- /dev/null +++ b/group10/353261578/src/com/sx/structures/BinaryTree.java @@ -0,0 +1,74 @@ +package com.sx.structures; + +public class BinaryTree { + + private BinaryNode root; + + public BinaryTree(Object o) { + root = new BinaryNode(o); + } + + public void insert(Object o) { + BinaryNode node = new BinaryNode(o); + insert(root, node); + } + + private void insert(BinaryNode root, BinaryNode node) { + if (node.compareTo(root) > 0) { + if (root.getRight() == null) { + root.setRight(node); + return; + } + insert(root.getRight(), node); + } else { + if (root.getLeft() == null) { + root.setLeft(node); + return; + } + insert(root.getLeft(), node); + } + + } + + public void preOrder(BinaryNode root){ + order(root); + if(root.getLeft()!=null) + preOrder(root.getLeft()); + if(root.getRight()!=null) + preOrder(root.getRight()); + } + + public void postOrder(BinaryNode root){ + if(root.getLeft()!=null) + postOrder(root.getLeft()); + if(root.getRight()!=null) + postOrder(root.getRight()); + order(root); + } + + public void inOrder(BinaryNode root){ + if(root.getLeft()!=null) + inOrder(root.getLeft()); + order(root); + if(root.getRight()!=null) + inOrder(root.getRight()); + } + + /** + * 未实现 + * @param root + * @param node + * @return + */ + private boolean remove(BinaryNode root, BinaryNode node){ + return false; + } + + public BinaryNode getRoot(){ + return root; + } + + private void order(BinaryNode root){ + System.out.print(root.getData()+" "); + } +} diff --git a/group10/353261578/src/com/sx/structures/Iterator.java b/group10/353261578/src/com/sx/structures/Iterator.java new file mode 100644 index 0000000000..9965e652bc --- /dev/null +++ b/group10/353261578/src/com/sx/structures/Iterator.java @@ -0,0 +1,6 @@ +package com.sx.structures; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group10/353261578/src/com/sx/structures/MyArrayList.java b/group10/353261578/src/com/sx/structures/MyArrayList.java new file mode 100644 index 0000000000..952776b5cb --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyArrayList.java @@ -0,0 +1,83 @@ +package com.sx.structures; + +public class MyArrayList implements MyList { + + private int size; + private int ex=10; + private int last=-1; + private Object [] arr; + + public MyArrayList() { + size = 10; + arr = new Object[size]; + } + + @Override + public void add(Object o) { + last++; + if(last==size){ + size += ex; + Object[] temp = new Object[size]; + System.arraycopy(arr, 0, temp, 0, arr.length); + arr = temp; + } + arr[last]=o; + } + + @Override + public void add(int index, Object o) { + add(o); + for(int i=arr.length-1;i>index;i--) + arr[i]=arr[i-1]; + arr[index]=o; + } + + @Override + public Object get(int index) { + return arr[index]; + } + + @Override + public Object remove(int index) { + Object element = arr[index]; + for(int i=index;ilist.size()) + return false; + return true; + } + + @Override + public Object next() { + if(hasNext()) + return list.get(p-1); + return -1; + } + + } + +} diff --git a/group10/353261578/src/com/sx/structures/MyLinkedList.java b/group10/353261578/src/com/sx/structures/MyLinkedList.java new file mode 100644 index 0000000000..3d9932ee92 --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyLinkedList.java @@ -0,0 +1,105 @@ +package com.sx.structures; + +public class MyLinkedList implements MyList{ + private Node head; + private int size = 0; + + public MyLinkedList() { + head = new Node(); + } + @Override + public void add(Object o) { + Node node = createNode(o); + Node pre = head; + while(pre.next!=null){ + pre = pre.next; + } + pre.next = node; + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0){ + System.out.println("����Խ��");return; + } + Node node = createNode(o); + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index--; + } + node.next = pointer.next; + pointer.next = node; + size++; + } + + @Override + public Object get(int index) { + Node pointer = head; + while(index>=0){ + pointer = pointer.next; + index--; + } + return pointer.data; + } + + @Override + public Object remove(int index) { + Object data = null; + Node pre = head; + while(index>0){ + pre = pre.next; + index--; + } + data = pre.next.data; + pre.next = pre.next.next; + size--; + return data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ +// Node node = createNode(o); +// Node p = head; +// while(p.next!=null) +// p = p.next; +// p.next = node; +// size++; + add(o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + Object data = null; + Node re = head; + Node pre = head; + while(re.next!=null){ + re = re.next; + pre = re; + } + data = re.data; + re=null; + pre.next = null; + size--; + return data; + } + private Node createNode(Object o){ + Node node = new Node(); + node.data=o; + return node; + } + private static class Node{ + Object data = null; + Node next = null; + } + +} diff --git a/group10/353261578/src/com/sx/structures/MyList.java b/group10/353261578/src/com/sx/structures/MyList.java new file mode 100644 index 0000000000..c880f5c8ff --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyList.java @@ -0,0 +1,13 @@ +package com.sx.structures; + +public interface MyList { + 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/group10/353261578/src/com/sx/structures/MyQueue.java b/group10/353261578/src/com/sx/structures/MyQueue.java new file mode 100644 index 0000000000..9d0cf9018b --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyQueue.java @@ -0,0 +1,25 @@ +package com.sx.structures; + +public class MyQueue { + private MyLinkedList elements ; + public MyQueue() { + elements = new MyLinkedList(); + } + + public void enQueue(Object o){ + elements.add(o); + } + + public Object deQueue(){ + return elements.removeFirst(); + } + + public boolean isEmpty(){ + if(size()>0) + return false; + return true; + } + public int size(){ + return elements.size(); + } +} diff --git a/group10/353261578/src/com/sx/structures/MyStack.java b/group10/353261578/src/com/sx/structures/MyStack.java new file mode 100644 index 0000000000..b81930604d --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyStack.java @@ -0,0 +1,37 @@ +package com.sx.structures; + +public class MyStack { + private int pointer; + private MyArrayList element; + public MyStack() { + element = new MyArrayList(); + pointer = -1; + } + + public void push(Object o){ + pointer++; + element.add(pointer); + } + public Object pop(){ + if(pointer<0) + return -1; + Object p = element.get(pointer); + pointer--; + return p; + } + /** + *只返回栈顶元素 + * @return + */ + public Object peek(){ + if(pointer<0) + return -1; + return element.get(pointer); + } + public boolean isEmpty(){ + if(pointer<0) + return true; + return false; + } + +} diff --git a/group10/353261578/test/com/test/BinaryTreeTest.java b/group10/353261578/test/com/test/BinaryTreeTest.java new file mode 100644 index 0000000000..714c71798c --- /dev/null +++ b/group10/353261578/test/com/test/BinaryTreeTest.java @@ -0,0 +1,31 @@ +package com.test; + + +import org.junit.Test; + +import com.sx.structures.BinaryTree; + +public class BinaryTreeTest { + + private BinaryTree bt; + + @Test + public void test() { + bt = new BinaryTree(55); + bt.insert(23); + bt.insert(44); + bt.insert(16); + bt.insert(78); + bt.insert(99); + //先序 + System.out.println("先序遍历:"); + bt.preOrder(bt.getRoot()); + //中序遍历 + System.out.println("\n中序遍历:"); + bt.inOrder(bt.getRoot()); + //后序 + System.out.println("\n后序遍历:"); + bt.postOrder(bt.getRoot()); + } + +} diff --git a/group10/353261578/test/com/test/MyArrayListTest.java b/group10/353261578/test/com/test/MyArrayListTest.java new file mode 100644 index 0000000000..9c8369f91b --- /dev/null +++ b/group10/353261578/test/com/test/MyArrayListTest.java @@ -0,0 +1,64 @@ +package com.test; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.sx.structures.MyArrayList; +import com.sx.structures.MyList; + +public class MyArrayListTest { + + private MyArrayList list ; + + @Test + public void testAddObject() { + for(int j=0;j<12;j++){ + list.add(j); + } + } + + @Test + public void testAddIntObject() { + list.add(5, 12); + list.add(10, 11); + } + + @Test + public void testGet() { + System.out.println(list.get(5)); + } + + @Test + public void testRemove() { + System.out.println("\nremoved 5:"+list.remove(5)+"."); + } + + @Test + public void testSize() { + System.out.println("\nlist.size:"+list.size()); + } + + @After + public void Print(){ + System.out.println("最终结果:List:"); + PrintList(list); + } + @Before + public void createlist(){ + list = new MyArrayList(); + for(int j=0;j<12;j++){ + list.add(j); + } + System.out.println("初始list:"); + PrintList(list); + } + + public static void PrintList(MyList list){ + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+" "); + } + } + +} diff --git a/group10/353261578/test/com/test/MyLinkedListTest.java b/group10/353261578/test/com/test/MyLinkedListTest.java new file mode 100644 index 0000000000..62dcbbc8d1 --- /dev/null +++ b/group10/353261578/test/com/test/MyLinkedListTest.java @@ -0,0 +1,83 @@ +package com.test; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.sx.structures.MyLinkedList; +import com.sx.structures.MyList; + +public class MyLinkedListTest { + + private MyLinkedList list; + + @Test + public void testAddObject() { + list.add(3); + } + + @Test + public void testAddIntObject() { + list.add(0,"t-0"); + list.add(1, "t-1"); + } + + @Test + public void testGet() { + System.out.println(list.get(1)); + } + + @Test + public void testRemove() { + list.remove(0); + } + + @Test + public void testSize() { + System.out.println(); + System.out.println(" list-size="+list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst("t-1"); + } + + @Test + public void testAddLast() { + list.addLast("T-last"); + } + + @Test + public void testRemoveFirst() { + list.removeFirst(); + } + + @Test + public void testRemoveLast() { + list.removeLast(); + } + + @After + public void Print(){ + System.out.println("\n操作之后,List:"); + PrintList(list); + } + @Before + public void createlist(){ + list = new MyLinkedList(); + for(int j=0;j<11;j++){ + list.add(j); + } + System.out.println("初始list:"); + PrintList(list); + } + + public static void PrintList(MyList list){ + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+" "); + } + } + +} diff --git a/group10/353261578/test/com/test/StaQueTest.java b/group10/353261578/test/com/test/StaQueTest.java new file mode 100644 index 0000000000..fef9ba8ff7 --- /dev/null +++ b/group10/353261578/test/com/test/StaQueTest.java @@ -0,0 +1,44 @@ +package com.test; + + +import org.junit.Test; + +import com.sx.structures.MyQueue; +import com.sx.structures.MyStack; + +public class StaQueTest { + private MyStack s; + private MyQueue queue; + + @Test + public void Stacktest() { + + s = new MyStack(); + + for(int i=0;i<10;i++){ + s.push(i); + } + System.out.println("\npop:"); + while(s.isEmpty()==false){ + System.out.println("-"+s.isEmpty()+":"+s.pop()); + } + + System.out.println("\n"+"-"+s.isEmpty()+":"+s.pop()); + + System.out.println("\npeek"); + for(int i=1;i<3;i++){ + System.out.print(s.peek()+" "); + } + } + + @Test + public void queueTest(){ + queue = new MyQueue(); + for(int i=0;i<10;i++) + queue.enQueue(i); + while(queue.size()>0) + System.out.print(queue.deQueue()+" "); + } + + +} diff --git a/group10/364298692/article.md b/group10/364298692/article.md new file mode 100644 index 0000000000..01af1f5151 --- /dev/null +++ b/group10/364298692/article.md @@ -0,0 +1,3 @@ +# ÿ +## week01 + diff --git a/group10/364298692/cs/.classpath b/group10/364298692/cs/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group10/364298692/cs/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/364298692/cs/.gitignore b/group10/364298692/cs/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/364298692/cs/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/364298692/cs/.project b/group10/364298692/cs/.project new file mode 100644 index 0000000000..8536996016 --- /dev/null +++ b/group10/364298692/cs/.project @@ -0,0 +1,17 @@ + + + cs + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/364298692/cs/.settings/org.eclipse.jdt.core.prefs b/group10/364298692/cs/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group10/364298692/cs/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group10/364298692/cs/src/com/coding/basic/ArrayList.java b/group10/364298692/cs/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..71761666aa --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList(int initialCapacity){ + elementData = new Object[initialCapacity]; + } + + public ArrayList(){ + elementData = new Object[10]; + } + + public void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + Object[] oldData = elementData; + int newCapacity = (oldCapacity * 3) / 2 + 1; + if(minCapacity > newCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + ensureCapacity(size + 1); + for(int i = size-1; i >= index; i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size-1){ + return null; + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + if(index > size-1){ + return null; + }else{ + Object obj = elementData[index]; + for(int i=index; i o.hashCode()){ + this.right = node; + }else{ + this.left = node; + } + return this; + } + + + +} diff --git a/group10/364298692/cs/src/com/coding/basic/Iterator.java b/group10/364298692/cs/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group10/364298692/cs/src/com/coding/basic/LinkedList.java b/group10/364298692/cs/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..976eb88fa3 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/LinkedList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = node.next; + node.next = newNode; + size++; + } + public Object get(int index){ + if(index > size-1){ + return null; + }else{ + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + return node.data; + } + } + public Object remove(int index){ + if(index > size-1){ + return null; + }else if(index == 0){ + Object obj = head.data; + head = head.next; + size--; + return obj; + }else{ + Node node = head; + //ñɾڵǰһڵ + for(int i = 0; i < index-1; i++){ + node = node.next; + } + Object obj = node.next.data; + node.next = node.next.next; + size--; + return obj; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newHead = new Node(); + newHead.data = o; + newHead.next = head; + head = newHead; + size++; + } + public void addLast(Object o){ + Node node = head; + while(node.next != null){ + node = node.next; + } + Node newNode = new Node(); + newNode.data = o; + node.next = newNode; + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group10/364298692/cs/src/com/coding/basic/List.java b/group10/364298692/cs/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.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/group10/364298692/cs/src/com/coding/basic/Queue.java b/group10/364298692/cs/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..1a96515399 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList; + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + Object obj = linkedList.removeFirst(); + return obj; + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group10/364298692/cs/src/com/coding/basic/Stack.java b/group10/364298692/cs/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7043ba9386 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + public Object pop(){ + Object obj = elementData.remove(elementData.size()-1); + return obj; + } + + public Object peek(){ + Object obj = elementData.get(0); + return obj; + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/569420966/struct/pom.xml b/group10/569420966/struct/pom.xml new file mode 100644 index 0000000000..2fc5902e7a --- /dev/null +++ b/group10/569420966/struct/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.rishy + struct + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java b/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java new file mode 100644 index 0000000000..19f0fdc26f --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java @@ -0,0 +1,162 @@ +package com.myutil; + + +import java.text.MessageFormat; +import java.util.NoSuchElementException; + +/** + * 数组列表 + */ +public class ArrayList implements List { + private Object[] elementData; + private int size = 0; + private static final int DEFAULT_SIZE = 10; + + /** + * 判断边界 + *

+ *

+     *      若 index < 0 或者 index > size 则抛出非法参数异常
+     * 
+ * + * @param index 当前索引 + */ + private void judgeRange(int index) { + if (index < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be great or equal then 0. index:{0}", index)); + } + if (index >= this.size) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be less then size(). index:{0}", index)); + } + if (this.size == Integer.MAX_VALUE) { + throw new IllegalArgumentException("Array already can not Expansion."); + } + } + + /** + * 扩充数组容量 + *

+ *

+     *     若 size >= elementData.length 则对数组进行扩容
+     *     扩容至原(elementData.length+1) * 2
+     * 
+ */ + private void capacityExpansion() { + if (this.size >= elementData.length) { + Object[] tmpData = new Object[(elementData.length + 1) * 2]; + System.arraycopy(elementData, 0, tmpData, 0, elementData.length); + elementData = tmpData; + } + } + + public ArrayList() { + elementData = new Object[DEFAULT_SIZE]; + } + + public ArrayList(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Capacity is must be great or equal 0. capacity:{0}", capacity)); + } + this.elementData = new Object[capacity]; + } + + public void add(T element) { + capacityExpansion(); + elementData[this.size] = element; + this.size++; + } + + public void add(T element, int index) { + judgeRange(index); + capacityExpansion(); + if (this.size - index > 0) { + System.arraycopy(elementData, index, elementData, index + 1, this.size - index); + } + elementData[index] = element; + this.size++; + } + + public T remove(int index) { + judgeRange(index); + T tmpObject = (T) elementData[index]; + if (this.size - index > 0) { + System.arraycopy(elementData, index + 1, elementData, index, this.size - index - 1); + } + this.size--; + return tmpObject; + } + + public T get(int index) { + judgeRange(index); + return (T) elementData[index]; + } + + public int size() { + return this.size; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < this.size; i++) { + sb.append((T) elementData[i]); + if (i < this.size - 1) { + sb.append(","); + } + } + sb.append("]"); + + return sb.toString(); + } + + /** + * 获取迭代器 + * + * @return 迭代器 + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + int position = 0; + int lastRet = -1; + + public boolean hasNext() { + return position < ArrayList.this.size(); + } + + public T next() { + if (position >= size) { + throw new NoSuchElementException(); + } + int i = position; + T element = ArrayList.this.get(position++); + lastRet = i; + return element; + } + + public T remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + T removeElement = ArrayList.this.remove(lastRet); + position = lastRet; + lastRet = -1; + return removeElement; + } + } + + public static void main(String[] args) { + ArrayList ids = new ArrayList<>(); + for (int i = 0; i < 11; i++) { + ids.add(i); + } + Iterator iterator = ids.iterator(); + System.out.println(ids); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java b/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java new file mode 100644 index 0000000000..0e862a199d --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java @@ -0,0 +1,167 @@ +package com.myutil; + +import java.util.Random; + +/** + * 二叉树 + */ +public class BinaryTreeNode> { + private T element; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public T getElement() { + return element; + } + + public void setElement(T element) { + this.element = element; + } + + 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; + } + + /** + * 将元素插入二叉树 + * + * @param element 元素 + * @return 插入后的节点 + */ + public BinaryTreeNode insert(T element) { + if (element == null) { + throw new IllegalArgumentException("Element must be not null."); + } + + BinaryTreeNode currentNode = null; + if (this.element == null) { + currentNode = this; + currentNode.element = element; + } else { + currentNode = compareToElement(element, this); + } + + return currentNode; + } + + private BinaryTreeNode compareToElement(T element, BinaryTreeNode curr) { + if (element.compareTo(curr.element) == -1) { + if (curr.left == null) { + BinaryTreeNode node = new BinaryTreeNode<>(); + node.element = element; + curr.left = node; + return node; + } else { + return compareToElement(element, curr.left); + } + } else { + if (curr.right == null) { + BinaryTreeNode node = new BinaryTreeNode<>(); + node.element = element; + curr.right = node; + return node; + } else { + return compareToElement(element, curr.right); + } + } + } + + /** + * 先序遍历 + * + * @return 按先序遍历顺序展示节点值 + */ + public String preOrderTraversal() { + return concatPreOrder(this); + } + + private String concatPreOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + if (node.left != null) { + ret.append(concatPreOrder(node.left)); + } + + ret.append(node.element).append(" "); + + if (node.right != null) { + ret.append(concatPreOrder(node.right)); + } + + return ret.toString(); + } + + /** + * 中序遍历 + * + * @return 按中序遍历顺序展示节点值 + */ + public String inOrderTraversal() { + return concatInOrder(this); + } + + private String concatInOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + + ret.append(node.element).append(" "); + + if (node.left != null) { + ret.append(concatInOrder(node.left)); + } + + if (node.right != null) { + ret.append(concatInOrder(node.right)); + } + + return ret.toString(); + } + + /** + * 后序遍历 + * + * @return 按后序遍历顺序展示节点值 + */ + public String postOrderTraversal() { + return concatPostOrder(this); + } + + private String concatPostOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + + if (node.right != null) { + ret.append(concatPostOrder(node.right)); + } + + ret.append(node.element).append(" "); + + if (node.left != null) { + ret.append(concatPostOrder(node.left)); + } + + return ret.toString(); + } + + public static void main(String[] args) { + BinaryTreeNode binaryTree = new BinaryTreeNode<>(); + Random random = new Random(); + for (int i = 0; i < 5; i++) { + binaryTree.insert(random.nextInt(100)); + } + + + System.out.println(binaryTree.preOrderTraversal()); + System.out.println(binaryTree.inOrderTraversal()); + System.out.println(binaryTree.postOrderTraversal()); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Iterator.java b/group10/569420966/struct/src/main/java/com/myutil/Iterator.java new file mode 100644 index 0000000000..412a1ef0aa --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Iterator.java @@ -0,0 +1,28 @@ +package com.myutil; + +/** + * 迭代器 + */ +public interface Iterator { + + /** + * 是否有下一个元素 + * + * @return true-有 false-无 + */ + boolean hasNext(); + + /** + * 获取下一个元素 + * + * @return 下一个元素 + */ + T next(); + + /** + * 删除当前迭代的元素 + * + * @return 被删除的元素 + */ + T remove(); +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java b/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java new file mode 100644 index 0000000000..dcbce429ec --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java @@ -0,0 +1,207 @@ +package com.myutil; + +import java.text.MessageFormat; +import java.util.NoSuchElementException; + +/** + * 链表列表 + */ +public class LinkedList implements List { + private Node header = new Node(); + private int size = 0; + + private Node lastNode() { + return findNode(this.size); + } + + private Node findNode(int index) { + int current = 0; + Node targetNode = header; + while (current < index) { + targetNode = targetNode.next; + current++; + } + return targetNode; + } + + /** + * 判断边界 + *

+ *

+     *      若 index < 0 或者 index > size 则抛出非法参数异常
+     * 
+ * + * @param index 当前索引 + */ + private void judgeRange(int index) { + if (index < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be great or equal then 0. index:{0}", index)); + } + if (index >= this.size) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be less then size(). index:{0}", index)); + } + if (this.size == Integer.MAX_VALUE) { + throw new IllegalArgumentException("Array already can not Expansion."); + } + } + + public LinkedList() { + + } + + @Override + public void add(T element) { + Node lastNode = lastNode(); + Node addNode = new Node(); + addNode.element = element; + lastNode.next = addNode; + this.size++; + } + + @Override + public void add(T element, int index) { + judgeRange(index); + Node targetNode = findNode(index); + Node addNode = new Node(); + addNode.element = element; + addNode.next = targetNode.next; + targetNode.next = addNode; + this.size++; + } + + @Override + public T remove(int index) { + judgeRange(index); + Node targetNode = findNode(index); + Node removeNode = targetNode.next; + targetNode.next = targetNode.next.next; + T element = (T) removeNode.element; + this.size--; + return element; + } + + @Override + public T get(int index) { + judgeRange(index); + return (T) findNode(index).next.element; + } + + @Override + public int size() { + return this.size; + } + + /** + * 添加一个元素到最开始的位置 + * + * @param element 元素 + */ + public void addFirst(T element) { + add(element, 0); + } + + /** + * 添加一个元素到最后 + * + * @param element 元素 + */ + public void addLast(T element) { + add(element, this.size - 1); + } + + /** + * 删除第一个元素 + * + * @return 第一个元素 + */ + public T removeFirst() { + if (this.size == 0) { + throw new ArrayIndexOutOfBoundsException("This list is empty, don't to remove."); + } + + return remove(0); + } + + /** + * 删除最后一个元素 + * + * @return 最后一个元素 + */ + public T removeLast() { + if (this.size == 0) { + throw new ArrayIndexOutOfBoundsException("This list is empty, don't to remove."); + } + return remove(this.size - 1); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + Node indexNode = header.next; + while (indexNode != null) { + sb.append((T) indexNode.element); + if (indexNode.next != null) { + sb.append(","); + } + indexNode = indexNode.next; + } + sb.append("]"); + + return sb.toString(); + } + + /** + * 获取迭代器 + * + * @return 迭代器 + */ + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int position = 0; + int lastRet = -1; + + public boolean hasNext() { + return position < LinkedList.this.size(); + } + + public T next() { + if (position >= size) { + throw new NoSuchElementException(); + } + int i = position; + T element = LinkedList.this.get(position++); + lastRet = i; + return element; + } + + public T remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + T removeElement = LinkedList.this.remove(lastRet); + position = lastRet; + lastRet = -1; + return removeElement; + } + } + + private static class Node { + T element; + Node next; + } + + public static void main(String[] args) { + LinkedList ids = new LinkedList<>(); + for (int i = 0; i < 11; i++) { + ids.add(i); + } + Iterator iterator = ids.iterator(); + System.out.println(ids); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/List.java b/group10/569420966/struct/src/main/java/com/myutil/List.java new file mode 100644 index 0000000000..8dced86dc7 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/List.java @@ -0,0 +1,52 @@ +package com.myutil; + +/** + * 列表基本操作 + */ +public interface List { + /** + * 添加一个元素到列表 + * + * @param element 元素 + */ + void add(T element); + + /** + * 添加一个元素至指定位置 + * + *
+     *     指定位置范围: index >= 0 && index < size
+     *     否则回抛出非法参数异常
+     * 
+ * + * @param element 元素 + * @param index 指定位置 + */ + void add(T element, int index); + + /** + * 删除指定位置元素 + * + *
+     *     指定位置范围: index >= 0 && index < size
+     *     否则回抛出非法参数异常
+     * 
+ * + * @param index 指定位置 + * @return 删除的元素的引用 + */ + T remove(int index); + + /** + * 获取指定位置元素 + * @param index 指定位置 + * @return 指定位置的元素 + */ + T get(int index); + + /** + * 获取当前列表的大小 + * @return 当前列表的大小 + */ + int size(); +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Queue.java b/group10/569420966/struct/src/main/java/com/myutil/Queue.java new file mode 100644 index 0000000000..9097caa7f4 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Queue.java @@ -0,0 +1,44 @@ +package com.myutil; + +/** + * 队列 + */ +public class Queue { + private LinkedList elementList = new LinkedList<>(); + + /** + * 进入队列 + * + * @param element 进入队列的元素 + */ + public void enQueue(T element) { + elementList.add(element); + } + + /** + * 出队列 + * + * @return 出队列的元素 + */ + public T deQueue() { + return elementList.removeFirst(); + } + + /** + * 队列是否为空 + * + * @return true-是 false-否 + */ + public boolean isEmpty() { + return elementList.size() == 0; + } + + /** + * 获取队列的大小 + * + * @return 队列的大小 + */ + public int size() { + return elementList.size(); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Stack.java b/group10/569420966/struct/src/main/java/com/myutil/Stack.java new file mode 100644 index 0000000000..d78ae2b39a --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Stack.java @@ -0,0 +1,61 @@ +package com.myutil; + +/** + * 栈 + */ +public class Stack { + private ArrayList elementList = new ArrayList<>(); + + /** + * 入栈 + * + * @param element 入栈的元素 + */ + public void push(T element) { + elementList.add(element); + } + + /** + * 出栈 + * + * @return 出栈的元素 + */ + public T pop() { + if (elementList.size() == 0) { + throw new ArrayIndexOutOfBoundsException("Stack is empty, don't to pop()."); + } + T element = elementList.get(elementList.size() - 1); + elementList.remove(elementList.size() - 1); + return element; + } + + /** + * 获取栈顶元素 + * + * @return 栈顶元素 + */ + public T peek() { + if (elementList.size() == 0) { + throw new ArrayIndexOutOfBoundsException("Stack is empty, don't to peek()."); + } + return elementList.get(elementList.size() - 1); + } + + /** + * 是否为空栈 + * + * @return true-是 false-否 + */ + public boolean isEmpty() { + return elementList.size() == 0; + } + + /** + * 获取当前栈大小 + * + * @return 当前栈大小 + */ + public int size() { + return elementList.size(); + } +} diff --git a/group10/584709796/worka/.classpath b/group10/584709796/worka/.classpath new file mode 100644 index 0000000000..ece376cba2 --- /dev/null +++ b/group10/584709796/worka/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/584709796/worka/.gitignore b/group10/584709796/worka/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group10/584709796/worka/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/584709796/worka/.project b/group10/584709796/worka/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group10/584709796/worka/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/584709796/worka/.settings/org.eclipse.jdt.core.prefs b/group10/584709796/worka/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group10/584709796/worka/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group10/584709796/worka/src/com/coding/basic/ArrayList.java b/group10/584709796/worka/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7d06acbe10 --- /dev/null +++ b/group10/584709796/worka/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +//刚开始学JAVA,现在学到JAVA基础类库耐力,迭代器还没学 + +package com.coding.basic; + +public class ArrayList implements List { + + private int size =0; + + private Object[] elementData = new Object[100]; + + public int getSize() {//得到数组大小 + return size; + } + public void setSize(int size) {//设置数组的长度 + this.size = size; + } + + public void ExtendArray(int size){ //插入元素时,数组长度加1,确保数组不越界 + this.size=size+1; + } + + public void add(Object o){//在末尾添加元素 + int length=getSize(); + elementData[length] = o; + ExtendArray(length); + } + + public void add(int index, Object o){ + int length=getSize(); + + for(int k=length-1;k>=index-1;k--){//元素后移 + elementData[k+1]=elementData[k]; + } + elementData[index-1]=o; + ExtendArray(length);//插一个元素,扩充一次 + } + + public Object get(int index){//获取元素 + int length=getSize(); + if(index+1>length||index<0){ + System.out.println("方法 get(int index)的index不在数组索引的范围内"); + } + return (Object)elementData[index]; + } + + public Object remove(int index){//移除元素 + int length=getSize(); + if(index+1>length||index<0){ + System.out.println("方法 remove(int index)的index不在数组索引的范围内"); + } + Object ss=(Object)elementData[index]; + + for(int k=index;k implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int initCapcity){ + if(initCapcity < 0){ + throw new IllegalArgumentException("initCapcity 必须大于0"); + } + elementData = new Object[initCapcity]; + } + + public ArrayList(){ + elementData = new Object[10]; + } + + @Override + public void add(Object obj) { + grow(size + 1); + elementData[size++] = obj; + } + + @Override + public void add(int index, Object obj) { + rangeCheckForAdd(index); + grow(size + 1); + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = obj; + size ++; + } + + @Override + public void remove(Object obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + fastRemove(i); + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + fastRemove(i); + } + } + } + } + + @Override + public E remove(int index) { + rangeCheck(index); + int movedNum = size - index - 1; + E oldElement = elementData(index); + System.arraycopy(elementData, index+1, elementData, index, movedNum); + elementData[--size] = null; + return oldElement; + } + + @Override + public E get(int index) { + rangeCheck(index); + return elementData(index); + } + + @Override + public E set(int index, E obj) { + rangeCheck(index); + E oldElement = elementData(index); + elementData[index] = obj; + return oldElement; + } + + @Override + public int indexOf(E obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + return i; + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + /** + * 数组扩容 + * @param minCapacity + */ + private void grow(int minCapacity) { + if(minCapacity <= elementData.length){ + return; + } + int oldCapacity = elementData.length; + int newCapacity = minCapacity + (oldCapacity >> 1); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + if(minCapacity > Integer.MAX_VALUE){ + newCapacity = Integer.MAX_VALUE; + } + Object[] newArray = new Object[newCapacity]; + System.arraycopy(elementData, 0, newArray, 0, newCapacity); + elementData = newArray; + } + + @SuppressWarnings("unchecked") + private E elementData(int index){ + return (E) elementData[index]; + } + + private void fastRemove(int i) { + int numMoved = size - i -1; + if(numMoved > 0){ + System.arraycopy(elementData, i+1, elementData, i, numMoved); + } + elementData[-- size] = null; + } + + private void rangeCheck(int index){ + if(index >= size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } + + private void rangeCheckForAdd(int index){ + if(index > size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } +} diff --git a/group10/595128841/src/org/le/LinkedList.java b/group10/595128841/src/org/le/LinkedList.java new file mode 100644 index 0000000000..fbe95017cb --- /dev/null +++ b/group10/595128841/src/org/le/LinkedList.java @@ -0,0 +1,299 @@ +/** + * + */ +package org.le; + +import java.util.NoSuchElementException; + +/** + * @author yue + * @time 2017年2月19日 + */ +public class LinkedList implements List { + + private int size = 0; + + private Node first; + + private Node last; + + private static class Node{ + E item; + Node prev; + Node next; + Node(Node prev,E item, Node next) { + super(); + this.item = item; + this.prev = prev; + this.next = next; + } + } + + public LinkedList(){ + + } + + /** + * 头部插入 + */ + private void linkFirst(E e){ + final Node f = first; + final Node newNode = new Node(null,e,f); + first = newNode; + if(f == null) + last = newNode; + else + f.prev = newNode; + size ++; + } + + /** + * 尾部插入 + */ + private void linkLast(E e){ + final Node l = last; + final Node newNode = new Node<>(l,e,null); + last = newNode; + if(last == null) + first = newNode; + else + l.next = newNode; + size ++; + } + + /** + * 某个不为null元素之前插入 + */ + private void linkBefore(E e,Node succ){ + final Node pred = succ.prev; + final Node newNode = new Node<>(pred,e,succ); + succ.prev = newNode; + if(pred == null) + first = newNode; + else + pred.next = newNode; + size ++; + } + + /** + * 删除头部元素 + */ + private E unlinkFirst(Node f){ + final E element = f.item; + final Node next = f.next; + f.item = null; + f.next = null; + first = next; + if(next == null) + last = null; + else + next.prev = null; + size -- ; + return element; + } + /** + * 删除尾部元素 + * @param l + * @return + */ + private E unlinkLast(Node l){ + final E element = l.item; + final Node prev = l.prev; + l.item = null; + l.prev = null; + last = prev; + if(prev == null) + first = null; + else + prev.next = null; + size -- ; + return element; + } + + /** + * 删除指定节点 + * @param e + * @return + */ + private E unlink(Node e){ + final Node prev = e.prev; + final E element = e.item; + final Node next = e.next; + + if(prev == null){ + first = next; + }else{ + prev.next = next; + e.prev = null; + } + + if(next == null){ + last = prev; + }else{ + next.prev = prev; + e.next = null; + } + e.item = null; + size -- ; + return element; + } + + /** + * 该方法默认在尾部添加 + */ + @Override + public void add(E e) { + linkLast(e); + } + + /** + * + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + if(index == size){ + linkLast(e); + }else{ + linkBefore(e, node(index)); + } + } + + private Node node(int index) { + //小于容量一半 + if(index < (size >> 1)){ + Node x = first; + for(int i = 0; i < index; i++){ + x = x.next; + } + return x; + }else{ + Node x = last; + for(int i = size - 1; i > index; i --){ + x = x.prev; + } + return x; + } + } + + private void checkPositionIndex(int index){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + private void checkElementIndex(int index){ + if(index <0 || index >= size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + /** + * + */ + @Override + public void remove(E obj) { + if(obj == null){ + for(Node x = first;x != null; x = x.next){ + if(x.item == null){ + unlink(x); + } + } + }else{ + for(Node x = first;x != null;x = x.next){ + if(obj.equals(x.item)){ + unlink(x); + } + } + } + } + + /** + * + */ + @Override + public E remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + /** + * + */ + @Override + public E get(int index) { + checkElementIndex(index); + return node(index).item; + } + + /** + * + */ + @Override + public E set(int index, E obj) { + checkElementIndex(index); + Node x = node(index); + E oldVal = x.item; + x.item = obj; + return oldVal; + } + + /** + * + */ + @Override + public int indexOf(E obj) { + int index = 0; + if(obj == null){ + for(Node x = first;x != null;x = x.next){ + if(x.item == null) + return index; + index ++; + } + }else{ + for(Node x = first; x != null; x = x.next){ + if(obj.equals(x.item)) + return index; + index ++; + } + } + return -1; + } + /** + * 弹出栈顶的元素,不删除元素 + * @param e + * @return + */ + public E peek(){ + final Node e = first; + return e == null ? null : e.item; + } + + /** + * 弹出栈顶元素,删除元素 + * @return + */ + public E poll(){ + final Node e = first; + return (e == null) ? null : unlinkFirst(e); + } + /** + * 入栈,栈顶 + * @param e + */ + public void push(E e){ + linkFirst(e); + } + + /** + * 出栈,删除并返回栈顶元素 + * @return + */ + public E pop(){ + final Node f = first; + if(f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + +} diff --git a/group10/595128841/src/org/le/List.java b/group10/595128841/src/org/le/List.java new file mode 100644 index 0000000000..5fa9d6799a --- /dev/null +++ b/group10/595128841/src/org/le/List.java @@ -0,0 +1,19 @@ +package org.le; + +public interface List { + + void add(E obj); + + void add(int index,E obj); + + void remove(E obj); + + E remove(int index); + + E get(int index); + + E set(int index,E obj); + + int indexOf(E obj); + +} diff --git a/group10/630505243/src/com/coding/basic/ArrayList.java b/group10/630505243/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..396dc96ae7 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/ArrayList.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(o!=null){ + elementData[size] = o; + if(size<100-1){ + size++; + }else{ + Object[] temp = new Object[(size+1)+100]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + size++; + } + } + + } + public void add(int index, Object o){ + if(index<=size){ + + if(index=index;i--){ + if(i==index){ + temps[index] = tmp; + }else{ + temps[i]=temps[i-1]; + } + } + elementData = temps; + + }else if(index==elementData.length-1){ + //Ԫλôұ߽ + Object[] temp = new Object[(size+1)+100]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + elementData[index] = o; + } + } + } + + public Object get(int index){ + if(index<=size){ + return elementData[index]; + }else{ + return null; + } + } + + public Object remove(int index){ + Object rtnObj = null; + if(index<=size){ + if(indexsize()){ + throw new IndexOutOfBoundsException(); + } + if (index == size) { + add(o); + } else { + Node node = head; + for (int i = 0; i < index-1; i++) { + node = node.next; + } + Node newNode = new Node(o, node); + node.next = newNode; + newNode.next = node; + size++; + } + } + public Object get(int index){ + if(index<0||index>size()){ + throw new IndexOutOfBoundsException(); + } + Node node=head; + for(int i=0;isize()){ + throw new IndexOutOfBoundsException(); + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o,null); + newNode.next=head.next; + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group10/706097141/learning/src/com/hmily/learning/List.java b/group10/706097141/learning/src/com/hmily/learning/List.java new file mode 100644 index 0000000000..02ef056bd3 --- /dev/null +++ b/group10/706097141/learning/src/com/hmily/learning/List.java @@ -0,0 +1,8 @@ +package com.hmily.learning; +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/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java b/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java new file mode 100644 index 0000000000..46e1c564af --- /dev/null +++ b/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java @@ -0,0 +1,91 @@ +package com.hmily.learning; + +public class MyArrayList implements List,Iterator{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * Ԫ + */ + public void add(Object o){ + if(size==elementData.length){ + Object[] newElementData = new Object[elementData.length+1]; + for(int i=0;iindex;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + /** + * ȡԪ + */ + public Object get(int index){ + if(index>=size()||index<0){ + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + /** + * ƳԪ + */ + public Object remove(int index){ + if(index<0||index>size()){ + throw new ArrayIndexOutOfBoundsException(); + } + Object o=elementData[index]; + for(int i=index;i + + + + + diff --git a/group10/875867419/.gitignore b/group10/875867419/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/875867419/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/875867419/.project b/group10/875867419/.project new file mode 100644 index 0000000000..b6d8ce6204 --- /dev/null +++ b/group10/875867419/.project @@ -0,0 +1,17 @@ + + + coding2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/875867419/src/com/work/week01/MyArrayList.java b/group10/875867419/src/com/work/week01/MyArrayList.java new file mode 100644 index 0000000000..d005800d39 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyArrayList.java @@ -0,0 +1,203 @@ +package com.work.week01; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * ʵListӿڣķʽʵԼArrayList + * @author denghuaijun + * + * @param + */ +public class MyArrayList implements MyList,Serializable { + + private static final long serialVersionUID = 4145346362382387995L; + + /** + * ĬϴС + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * ĬϿ + */ + private static final Object[] EMPTY_ELEMENTDATA = {}; + + transient Object[] elementData; + + /** + * С + */ + private int size; + + public MyArrayList(){ + this.elementData = EMPTY_ELEMENTDATA; + } + + public MyArrayList(int capacity){ + if(capacity > 0){ + this.elementData = new Object[capacity]; + }else if(capacity == 0){ + this.elementData = EMPTY_ELEMENTDATA; + }else{ + throw new IllegalArgumentException("Ƿ"); + } + } + + private void ensureCapacity(int minCapacity){ + if(this.elementData == EMPTY_ELEMENTDATA){ + minCapacity = Math.max(minCapacity, DEFAULT_CAPACITY); + } + if(minCapacity > elementData.length){//λô鳤 + grow(minCapacity); + } + } + + private void grow(int minCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public boolean add(E element) { + ensureCapacity(size + 1); + elementData[size++] = element; + return true; + } + + @Override + public void add(int index, E element) { + //ȷindexǷԽ + checkAddRange(index); + //ȷ鳤Ƿ㹻 + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + private void checkAddRange(int index){ + if(index < 0 || index > size){//index == size Ԫ + throw new IndexOutOfBoundsException("Խ"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + checkRange(index); + return (E) elementData[index]; + } + + private void checkRange(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("Խ"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E remove(int index) { + checkRange(index); + E element = (E) elementData[index]; + int numMoved = size - index - 1; + if(numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[size--] = null; + return element; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public int indexOf(Object o) { + if(o == null){ + for(int i=0;i=0;i--){ + if(elementData[i] == null){ + return i; + } + } + }else{ + for(int i=size-1;i>=0;i--){ + if(o.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = -1; + + public MyIter(){ + flag = size; //鳤 + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("ֵ鷶Χ"); + } + return (E) elementData[size-(flag--)]; + } + + } + public static void main(String[] args) { + MyArrayList array = new MyArrayList(); + array.add("1"); + array.add("2"); + array.add("3"); + array.add("4"); + array.remove(2); + array.add(2, "1"); + System.out.println("size="+array.size()); + System.out.println("indexOf(3)="+array.indexOf("3")); + System.out.println("lastIndexOf(1)="+array.lastIndexOf("1")); + MyIterator itr = array.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyBinaryTree.java b/group10/875867419/src/com/work/week01/MyBinaryTree.java new file mode 100644 index 0000000000..8c6f057648 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyBinaryTree.java @@ -0,0 +1,82 @@ +package com.work.week01; + +public class MyBinaryTree { + + private MyBinaryTreeNode parent; + + public MyBinaryTree(){ + this.parent = new MyBinaryTreeNode(null, null, null); + } + + public void insertNode(E element){ + MyBinaryTreeNode node = new MyBinaryTreeNode(element, null, null); + if(parent.element == null){ + parent = node; + return; + } + insertNode(parent, node); + } + + private void insertNode(MyBinaryTreeNode parentNode, MyBinaryTreeNode newNode){ + if(parentNode.compareTo(newNode) <= 0){// + if(parentNode.right == null){ + parentNode.right = newNode; + }else{ + insertNode(parentNode.right, newNode); + } + }else{ + if(parentNode.left == null){ + parentNode.left = newNode; + }else{ + insertNode(parentNode.left, newNode); + } + } + } + + private void printNode(MyBinaryTreeNode node, int count){ + if(node.left != null){ + printNode(node.left, count++); + } + if(node.right != null){ + printNode(node.right, count++); + } + for(int i=0;i implements Comparable> { + + private T element; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public MyBinaryTreeNode(T element, MyBinaryTreeNode left, MyBinaryTreeNode right){ + this.element = element; + this.left = left; + this.right = right; + } + + @Override + public int compareTo(MyBinaryTreeNode o) { + Integer src = (Integer) this.element; + Integer dest = (Integer) o.element; + return src.compareTo(dest); + } + } + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree(); + tree.insertNode(5); + tree.insertNode(7); + tree.insertNode(3); + tree.insertNode(9); + tree.insertNode(4); + tree.printTree(); + } +} diff --git a/group10/875867419/src/com/work/week01/MyIterator.java b/group10/875867419/src/com/work/week01/MyIterator.java new file mode 100644 index 0000000000..78abc20f23 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyIterator.java @@ -0,0 +1,6 @@ +package com.work.week01; + +public interface MyIterator { + boolean hasNext(); + E next(); +} diff --git a/group10/875867419/src/com/work/week01/MyLinkedList.java b/group10/875867419/src/com/work/week01/MyLinkedList.java new file mode 100644 index 0000000000..675323a249 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyLinkedList.java @@ -0,0 +1,169 @@ +package com.work.week01; + +import java.io.Serializable; + + +public class MyLinkedList implements MyList, Serializable{ + + private static final long serialVersionUID = 8700137302944494769L; + + transient int size = 0; + + transient MyNode head; + transient MyNode last; + + public MyLinkedList(){ + head = new MyNode(null, null); + last = new MyNode(null, null); + } + + @Override + public boolean add(E element) { + if(head.element == null){ + head = new MyNode(element, null); + last = head; + }else{ + MyNode node = new MyNode(element, null); + last.next = node; + last = node; + } + size++; + return true; + } + + @Override + public void add(int index, E element) { + if(index < 0 || index -size > 0){ + throw new IndexOutOfBoundsException(""); + } + if(index == 0){ + MyNode node = new MyNode(element, null); + node.next = head; + head = node; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = new MyNode(element, null); + node.next = leftNode.next; + leftNode.next = node; + } + size++; + } + + private MyNode getIndexNode(int index){ + MyNode node = head; + for(int i=0; i= 0){ + throw new IndexOutOfBoundsException(""); + } + MyNode node = getIndexNode(index); + return node.element; + } + + @Override + public E remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(""); + } + if(index == 0){//Ƴͷ + MyNode node = head; + head = head.next; + node.next = null; + size--; + return node.element; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = leftNode.next; //ƳĽڵ + leftNode.next = node.next; + node.next = null; + size--; + return node.element; + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E element){ + add(0, element); + } + + public void addLast(E element){ + add(size, element); + } + + public void removeFirst(){ + remove(0); + } + + public void removeLast(){ + remove(size-1); + } + + private static class MyNode{ + E element; + MyNode next; + + MyNode(E element, MyNode next) { + this.element = element; + this.next = next; + } + + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = 0; + + public MyIter(){ + flag = size; + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("ֵΧ"); + } + return get(size-(flag--)); + } + } + + public static void main(String[] args) { + MyLinkedList link = new MyLinkedList(); + link.add("1"); + link.add("2"); + link.add("3"); + link.add("4"); + link.add(3, "1"); + link.removeFirst(); + System.out.println("size="+link.size()); + MyIterator itr = link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + link.remove(4); + } +} diff --git a/group10/875867419/src/com/work/week01/MyList.java b/group10/875867419/src/com/work/week01/MyList.java new file mode 100644 index 0000000000..f7cc918888 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyList.java @@ -0,0 +1,11 @@ +package com.work.week01; + +public interface MyList{ + boolean add(E element); + void add(int index, E element); + E get(int index); + E remove(int index); + int size(); + boolean isEmpty(); + MyIterator iterator(); +} diff --git a/group10/875867419/src/com/work/week01/MyQueue.java b/group10/875867419/src/com/work/week01/MyQueue.java new file mode 100644 index 0000000000..97bca5399a --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyQueue.java @@ -0,0 +1,38 @@ +package com.work.week01; + +public class MyQueue { + private MyArrayList elementData; + + public MyQueue(){ + elementData = new MyArrayList(); + } + + public void enQueue(E element){// + elementData.add(element); + } + + public E deQuene(){// Ƚȳ + return elementData.remove(0); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + System.out.println("size="+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQuene()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyStack.java b/group10/875867419/src/com/work/week01/MyStack.java new file mode 100644 index 0000000000..f82bbe04c1 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyStack.java @@ -0,0 +1,43 @@ +package com.work.week01; + +public class MyStack { + private MyArrayList elementData; + + public MyStack(){ + elementData = new MyArrayList<>(); + } + + public void push(E element){ + elementData.add(element); + } + + public E pop(){ //ƳջԪ ȳ + return elementData.remove(elementData.size() - 1); + } + + public E peek(){ //ȡջԪ + return elementData.get(elementData.size() - 1); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + stack.push("4"); + stack.push("5"); + System.out.println("size="+stack.size()); + System.out.println("peekջԪ="+stack.peek()); + while(!stack.isEmpty()){ + System.out.println("popջԪ"+stack.pop()); + } + } +} diff --git "a/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..5c63425fba --- /dev/null +++ "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +ҵַhttps://my.oschina.net/u/3080511/blog/846172 \ No newline at end of file diff --git a/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java index a5a04de76d..4bfe28057f 100644 --- a/liuxin/src/com/coding/basic/Stack.java +++ b/liuxin/src/com/coding/basic/Stack.java @@ -1,22 +1,22 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +}