diff --git a/group27/425044891/src/com/coding/basic/ArrayList.java b/group27/425044891/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..929a41661c --- /dev/null +++ b/group27/425044891/src/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +/** + * ArrayList + * + * @author Guang + * @date 2017年3月12日 下午1:55:47 + */ +public class ArrayList implements List { + + /** + * 数组中元素个数 + */ + private int size = 0; + + /** + * 数组 + */ + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size >= 100 || size < 0) { + throw new IndexOutOfBoundsException("越界."); + } + if (null == o) { + return; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkRangeForAdd(index); + if (null == o) { + return; + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + private void checkRangeForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("越界."); + } + } + + private void checkRangeForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("越界."); + } + } + + public Object get(int index) { + checkRangeForGet(index); + return elementData[index]; + } + + /** + * 移除第index位置的元素,后续元素前移 + */ + public Object remove(int index) { + checkRangeForGet(index); + int i = index; + Object o = elementData[index]; + for (; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[i] = null; + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + int index = 0; + @Override + public Object next() { + checkRangeForGet(index); + Object o = elementData[index]; + index++; + return o; + } + + @Override + public boolean hasNext() { + return index < size && index >= 0; + } + }; + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/BinaryTreeNode.java b/group27/425044891/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..c702b191e4 --- /dev/null +++ b/group27/425044891/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode> { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public T getData() { + return data; + } + + public void setData(T 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(T o) { + if (o.compareTo(data) <= 0) { + return getLeft().insert(o); + } else { + return getRight().insert(o); + } + } + +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Iterator.java b/group27/425044891/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..de308cef25 --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/LinkedList.java b/group27/425044891/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..0c8520c62e --- /dev/null +++ b/group27/425044891/src/com/coding/basic/LinkedList.java @@ -0,0 +1,274 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = null; + + private int size = 0; + + public LinkedList() { + this.head = new Node(); + this.head.next = null; + this.head.data = null; + this.size = 0; + } + + public void add(Object o) { + if (null == o) { + return; + } + Node next = new Node(); + next.data = o; + next.next = null; + + /** + * 寻找尾部节点 + */ + Node p = head; + while (p.next != null) { + p = p.next; + } + p.next = next; + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (null == o) { + return; + } + Node next = new Node(); + next.data = o; + next.next = null; + + Node p = head.next; + Node pre = head; + int pos = 0; + while (p != null && pos < index) { + pre = p; + p = p.next; + pos++; + } + next.next = p; + pre.next = next; + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + int pos = 0; + while (p != null && pos < index) { + p = p.next; + pos++; + } + return p == null ? null : p.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Node pre = p; + int pos = 0; + while (p != null && pos < index) { + pre = p; + p = p.next; + pos++; + } + Object o = p.data; + pre.next = p.next; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (null == o) { + return; + } + Node next = new Node(); + next.next = null; + next.data = o; + + next.next = head.next; + head.next = next; + size++; + } + + public void addLast(Object o) { + if (null == o) { + return; + } + Node p = head; + while (p.next != null) { + p = p.next; + } + + Node next = new Node(); + next.data = o; + next.next = null; + + p.next = next; + size++; + } + + public Object removeFirst() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Object o = p.data; + + head.next = p.next; + size--; + return o; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Node p = head.next; + Node pre = head; + while (p.next != null) { + pre = p; + p = p.next; + } + Object o = p.data; + pre.next = p.next; + size--; + return o; + } + + public Iterator iterator() { + return new Iterator() { + Node current = head; + + @Override + public Object next() { + Object o = current.next.data; + current = current.next; + return o; + } + + @Override + public boolean hasNext() { + return current.next != null; + } + }; + } + + private static class Node { + Object data; + Node next; + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node p = head.next.next; + Node pre = head.next; + pre.next = null; + while (p != null) { + Node pp = p.next; + p.next = pre; + pre = p; + p = pp; + } + head.next = pre; + // --- --- --- + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 从当前链表中取出那些listB所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + + public static void main(String[] args) { + + LinkedList list = new LinkedList(); + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.reverse(); + Iterator it = list.iterator(); + while (it.hasNext()) { + Object o = it.next(); + System.out.println(o); + } + + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/List.java b/group27/425044891/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group27/425044891/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(); +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Queue.java b/group27/425044891/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fcec6ac57c --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Queue.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Queue { + + private Object[] elementData = new Object[100]; + + int size = 0; + + public void enQueue(Object o) { + if (size < 0 || size >= 100) { + throw new IndexOutOfBoundsException(); + } + elementData[size++] = o; + } + + public Object deQueue() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } + Object o = elementData[0]; + int i = 0; + for (; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[i] = null; + size--; + return o; + } + + public boolean isEmpty() { + return elementData.length != 0; + } + + public int size() { + return elementData.length; + } +} \ No newline at end of file diff --git a/group27/425044891/src/com/coding/basic/Stack.java b/group27/425044891/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1217d25fbe --- /dev/null +++ b/group27/425044891/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + int size = elementData.size(); + elementData.add(size, o); + } + + public Object pop() { + int size = elementData.size(); + return elementData.remove(size - 1); + } + + public Object peek() { + int size = elementData.size(); + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return elementData.size() != 0; + } + + public int size() { + return elementData.size(); + } +} \ No newline at end of file