From 011550bae6acba09d226139f896b659c87554397 Mon Sep 17 00:00:00 2001 From: jacky <1271620150@qq.com> Date: Sun, 26 Feb 2017 22:23:36 +0800 Subject: [PATCH] Work01 commit --- group09/1271620150/Work01/.gitignore | 19 ++ .../src/com/coding/basic/ArrayList.java | 100 +++++++ .../Work01/src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 277 ++++++++++++++++++ .../Work01/src/com/coding/basic/List.java | 9 + .../Work01/src/com/coding/basic/Queue.java | 44 +++ .../Work01/src/com/coding/basic/Stack.java | 45 +++ 7 files changed, 501 insertions(+) create mode 100644 group09/1271620150/Work01/.gitignore create mode 100644 group09/1271620150/Work01/src/com/coding/basic/ArrayList.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Iterator.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/LinkedList.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/List.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Queue.java create mode 100644 group09/1271620150/Work01/src/com/coding/basic/Stack.java diff --git a/group09/1271620150/Work01/.gitignore b/group09/1271620150/Work01/.gitignore new file mode 100644 index 0000000000..1af1a6638d --- /dev/null +++ b/group09/1271620150/Work01/.gitignore @@ -0,0 +1,19 @@ +*.class +*.classpath +*.project + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ diff --git a/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4e6dc8c929 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private Object[] elements; + + private int size; + + public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + this.elements = new Object[initialCapacity]; + } + + public ArrayList() { + this(10); + } + + public void add(Object obj) { + ensureCapacity(size + 1); + elements[size++] = obj; + + } + + public void add(int index, Object obj) { + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = obj; + size++; + } + + public Object get(int index) { + rangeCheck(index); + return elements[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object toRemove = elements[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elements, index + 1, elements, index, numMoved); + elements[--size] = null; + return toRemove; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elements.length; + if (minCapacity > oldCapacity) { + int newCapacity = oldCapacity * 2; + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void rangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + this.size; + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos != size; + } + + public Object next() { + int i = pos; + if (i >= size) + throw new NoSuchElementException(); + Object[] elements = ArrayList.this.elements; + if (i >= elements.length) + throw new ConcurrentModificationException(); + pos = i + 1; + return (Object) elements[i]; + } + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/Iterator.java b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e60b443310 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..302d048d74 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,277 @@ +package com.coding.basic; + +import java.util.Collection; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node first; + private Node last; + private int size; + + public LinkedList() { + } + + public LinkedList(Collection c) { + this(); + addAll(size, c); + } + + private void addAll(int index, Collection c) { + checkPositionIndex(size); + + Object[] a = c.toArray(); + int numNew = a.length; + if (numNew == 0) + return; + + Node pred, succ; + if (index == size) { + succ = null; + pred = last; + } else { + succ = node(index); + pred = succ.prev; + } + + for (Object o : a) { + Node newNode = new Node(pred, o, null); + if (pred == null) + first = newNode; + else + pred.next = newNode; + pred = newNode; + } + + if (succ == null) { + last = pred; + } else { + pred.next = succ; + succ.prev = pred; + } + + size += numNew; + } + + public void add(Object o) { + linkLast(o); + } + + private void linkLast(Object o) { + Node l = last; + Node newNode = new Node(l, o, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linkLast(o); + } else { + Node l = node(index); + linkBefore(o, l); + } + + } + + public void linkBefore(Object o, Node succ) { + final Node pred = succ.prev; + final Node newNode = new Node(pred, o, succ); + succ.prev = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + + public Object get(int index) { + checkElementIndex(index); + return node(index).data; + } + + public Object remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + private Object unlink(Node node) { + final Object element = node.data; + final Node next = node.next; + final Node prev = node.prev; + + if (prev == null) { + first = next; + } else { + prev.next = next; + node.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + node.next = null; + } + + node.data = null; + size--; + return element; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + linkFirst(o); + } + + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(null, o, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object o) { + linkLast(o); + } + + public Object removeFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + + private Object unlinkFirst(Node f) { + final Object element = f.data; + final Node next = f.next; + f.data = null; + f.next = null; + first = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return element; + } + + public Object removeLast() { + final Node l = last; + if (l == null) + throw new NoSuchElementException(); + return unlinkLast(l); + } + + private Object unlinkLast(Node l) { + final Object element = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if (prev == null) + first = null; + else + prev.next = null; + size--; + return element; + } + + public Iterator iterator(int index) { + return new LinkListIterator(index); + } + + private static class Node { + Object data; + Node next; + Node prev; + + Node(Node prev, Object obj, Node next) { + this.data = obj; + this.next = next; + this.prev = prev; + } + + } + + Node node(int index) { + // assert isElementIndex(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 boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private class LinkListIterator implements Iterator { + private Node lastReturned = null; + private Node next; + private int nextIndex; + + LinkListIterator(int index) { + next = (index == size) ? null : node(index); + nextIndex = index; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/List.java b/group09/1271620150/Work01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..216d97e9ad --- /dev/null +++ b/group09/1271620150/Work01/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/group09/1271620150/Work01/src/com/coding/basic/Queue.java b/group09/1271620150/Work01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4484081ac6 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Queue.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +public class Queue { + private static final int CAPACITY = 10; + private int size; + private int front; + private int tail; + private Object[] array; + + public Queue(){ + this.size = CAPACITY; + array = new Object[size]; + front = tail = 0; + } + + + public void enQueue(Object o) throws Exception{ + if (size() == size -1) + throw new Exception("Queue is full"); + array[tail] = o; + tail = (tail +1) % size; + } + + public Object deQueue() throws Exception{ + Object o; + if (isEmpty()) + throw new Exception("Queue is empty"); + o = array[front]; + front = (front + 1) % size; + return o; + } + + public boolean isEmpty(){ + return (front==tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (size + tail - front) % size; + } + +} diff --git a/group09/1271620150/Work01/src/com/coding/basic/Stack.java b/group09/1271620150/Work01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..58322c0130 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Stack.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Stack { + private static final int CAPACITY = 10; + private int capacity; + private int top = -1; + Object[] array; + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws Exception{ + if(size()== CAPACITY){ + throw new Exception("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + +} \ No newline at end of file