From eee5017c54c8b4d7010110760321a2f0233c7920 Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Wed, 8 Mar 2017 17:13:38 +0800 Subject: [PATCH 1/2] 17457741 --- group22/17457741/src/ArrayList.java | 110 +++++++++++++++++ group22/17457741/src/BinaryTreeNode.java | 53 ++++++++ group22/17457741/src/LinkedList.java | 149 +++++++++++++++++++++++ group22/17457741/src/Queue.java | 29 +++++ group22/17457741/src/Stack.java | 33 +++++ 5 files changed, 374 insertions(+) create mode 100644 group22/17457741/src/ArrayList.java create mode 100644 group22/17457741/src/BinaryTreeNode.java create mode 100644 group22/17457741/src/LinkedList.java create mode 100644 group22/17457741/src/Queue.java create mode 100644 group22/17457741/src/Stack.java diff --git a/group22/17457741/src/ArrayList.java b/group22/17457741/src/ArrayList.java new file mode 100644 index 0000000000..345538755c --- /dev/null +++ b/group22/17457741/src/ArrayList.java @@ -0,0 +1,110 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayList implements Iterable{ + private static final int CAPACITY = 5; + + private int size; + + private T [] items; + + public ArrayList(){doClear();} + + public void clear(){ + doClear(); + } + + private void doClear(){ + this.size=0;ensureCapacity(CAPACITY); + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size==0; + } + + public void trimToSize(){ + ensureCapacity(size()); + } + + public T get( int a){ + return items[a]; + } + + public T set(int a,T b){ + T old = items[a]; + items[a]=b; + return old; + } + + public void ensureCapacity(int newCapacity){ + if(newCapacitya;i--){ + items[i]=items[i-1]; + } + items[a]=b; + size++; + } + + public T remove(int a){ + T removedItem=items[a]; + for(int i=a;i iterator() { + // TODO Auto-generated method stub + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int current =0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return current implements Iterable{ + private Nodebegin; + private Nodeend; + private int size; + private int modCount=0; + + public LinkedList(){ + doClear(); + } + public void clear(){ + doClear(); + } + private void doClear(){ + begin=new Node(null,null,null); + end=new Node(null,begin,null); + begin.next=end; + } + + public int size(){ + return size; + } + public boolean isEmpty(){ + return size==0; + } + + public boolean add( T x){ + add(size(),x); + return true; + } + public void add(int a,T x){ + addBefore(getNode(a,0,size()),x); + } + + public T get(int a){ + return getNode(a).data; + } + public T set(int a,T newVal){ + Nodep=getNode(a); + T old=p.data; + p.data=newVal; + return old; + } + + public T remove(int a){ + return remove(getNode(a)); + } + + private void addBefore(Nodep,T x){ + Node newNode=new Node<>(x,p.prev,p); + newNode.prev.next=newNode; + p.prev=newNode; + size++; + modCount--; + } + + private T remove(Nodep){ + p.next.prev=p.prev; + p.prev.next=p.next; + size--; + modCount++; + + return p.data; + } + + private NodegetNode(int a){ + return getNode(a,0,size()-1); + } + + private NodegetNode(int a,int lower,int upper){ + Nodep; + + if(aupper) + throw new IndexOutOfBoundsException(); + if(aa;i--) + p=p.prev; + } + return p; + } + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return new LinkedListIterator(); + } + private class LinkedListIterator implements Iterator{ + private Node current=begin.next; + private int expectedModCount=modCount; + private boolean toRemove=false; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return current!=end; + } + @Override + public T next() { + // TODO Auto-generated method stub + if(modCount!=expectedModCount) + throw new java.util.ConcurrentModificationException(); + if(!toRemove) + throw new IllegalStateException(); + + T nextItem=current.data; + current=current.next; + toRemove=true; + return nextItem; + } + + public void remove(){ + if(modCount!=expectedModCount) + throw new java.util.ConcurrentModificationException(); + if(!toRemove) + throw new IllegalStateException(); + + LinkedList.this.remove(current.prev); + expectedModCount++; + toRemove=false; + } + } + + private static class Node{ + + public T data; + public Node prev; + public Node next; + + public Node(T d, Node p,Node n) { + // TODO Auto-generated constructor stub + data=d;prev=p;next=n; + } + + } + +} + diff --git a/group22/17457741/src/Queue.java b/group22/17457741/src/Queue.java new file mode 100644 index 0000000000..b98d8e2043 --- /dev/null +++ b/group22/17457741/src/Queue.java @@ -0,0 +1,29 @@ + + +public class Queue { + + private ArrayList list = new ArrayList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + final int size = list.size(); + if (0 == size) + return null; + Object o = list.remove(size); + return o; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + +} + + diff --git a/group22/17457741/src/Stack.java b/group22/17457741/src/Stack.java new file mode 100644 index 0000000000..12f870566f --- /dev/null +++ b/group22/17457741/src/Stack.java @@ -0,0 +1,33 @@ + + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object peek(){ + return elementData.get(0); + } + + public Object pop(){ + Object a = null; + if(elementData.size() > 0) { + + a = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + } + return a; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + +} From 1cd0c34be6d711ad95386830796611e94d2915ee Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Wed, 8 Mar 2017 17:14:54 +0800 Subject: [PATCH 2/2] 17457741 --- group22/17457741/src/ArrayList.java | 2 +- group22/17457741/src/LinkedList.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/group22/17457741/src/ArrayList.java b/group22/17457741/src/ArrayList.java index 345538755c..45b583d13c 100644 --- a/group22/17457741/src/ArrayList.java +++ b/group22/17457741/src/ArrayList.java @@ -1,4 +1,4 @@ -import java.util.Iterator; +import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayList implements Iterable{ diff --git a/group22/17457741/src/LinkedList.java b/group22/17457741/src/LinkedList.java index d7c4fe3415..c098978572 100644 --- a/group22/17457741/src/LinkedList.java +++ b/group22/17457741/src/LinkedList.java @@ -1,7 +1,6 @@ import java.util.Iterator; -import LinkedList.LinkedListIterator; -import LinkedList.Node; +