From ffc07a4ae94ca5232e51a91821bd76ce0a06fa58 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 10:52:45 +0800 Subject: [PATCH 1/9] template --- group22/2622819383/Task1/ArrayList.java | 30 +++++ group22/2622819383/Task1/BinaryTreeNode.java | 30 +++++ group22/2622819383/Task1/Iterator.java | 5 + group22/2622819383/Task1/LinkedList.java | 120 +++++++++++++++++++ group22/2622819383/Task1/List.java | 7 ++ group22/2622819383/Task1/Queue.java | 17 +++ group22/2622819383/Task1/Stack.java | 20 ++++ 7 files changed, 229 insertions(+) create mode 100644 group22/2622819383/Task1/ArrayList.java create mode 100644 group22/2622819383/Task1/BinaryTreeNode.java create mode 100644 group22/2622819383/Task1/Iterator.java create mode 100644 group22/2622819383/Task1/LinkedList.java create mode 100644 group22/2622819383/Task1/List.java create mode 100644 group22/2622819383/Task1/Queue.java create mode 100644 group22/2622819383/Task1/Stack.java diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java new file mode 100644 index 0000000000..ca9faa3291 --- /dev/null +++ b/group22/2622819383/Task1/ArrayList.java @@ -0,0 +1,30 @@ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group22/2622819383/Task1/BinaryTreeNode.java b/group22/2622819383/Task1/BinaryTreeNode.java new file mode 100644 index 0000000000..1f07869939 --- /dev/null +++ b/group22/2622819383/Task1/BinaryTreeNode.java @@ -0,0 +1,30 @@ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group22/2622819383/Task1/Iterator.java b/group22/2622819383/Task1/Iterator.java new file mode 100644 index 0000000000..96a43dbe0a --- /dev/null +++ b/group22/2622819383/Task1/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java new file mode 100644 index 0000000000..d431e25fdb --- /dev/null +++ b/group22/2622819383/Task1/LinkedList.java @@ -0,0 +1,120 @@ +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group22/2622819383/Task1/List.java b/group22/2622819383/Task1/List.java new file mode 100644 index 0000000000..4f7bcc71a8 --- /dev/null +++ b/group22/2622819383/Task1/List.java @@ -0,0 +1,7 @@ +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/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java new file mode 100644 index 0000000000..08f1e8ba29 --- /dev/null +++ b/group22/2622819383/Task1/Queue.java @@ -0,0 +1,17 @@ +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java new file mode 100644 index 0000000000..6c8c49bb10 --- /dev/null +++ b/group22/2622819383/Task1/Stack.java @@ -0,0 +1,20 @@ +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; + } +} From aa9a515649cce4260ed37acdb85cf1fe330104e7 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 12:26:19 +0800 Subject: [PATCH 2/9] done with ArrayList.java --- group22/2622819383/Task1/ArrayList.java | 136 ++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 11 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index ca9faa3291..1c060ffe3a 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,30 +1,144 @@ public class ArrayList implements List { - private int size = 0; + private int size; + + private int capacity; + + private static final int DEFAULT_CAPACITY = 10; - private Object[] elementData = new Object[100]; + private Object[] elementData; + + //add()ʱ�������� + private void expand() { + if (size < capacity) return;//��δ��Ա���������� + + if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//��������С���� + + Object[] oldElem = elementData; + elementData = new Object[capacity <<= 1]; + for (int i = 0; i < size; i++) + elementData[i] = oldElem[i]; + } + + //remove()ʱ�������� + private void shrink() { + if (capacity < DEFAULT_CAPACITY << 1) return;//����������DEFAULT_CAPACITY���� + + if (capacity >> 2 < size) return; //��25%Ϊ�� + + Object[] oldElem = elementData; elementData = new Object[capacity >>= 1]; + for (int i = 0; i < size; i++) + elementData[i] = oldElem[i]; + + } + + public ArrayList() { + clear(); + } + + public ArrayList(Object ...args) { + this(); + for (Object o : args) + add(o); + } + + public void clear() { + size = 0; + elementData = new Object[capacity = DEFAULT_CAPACITY]; + } + public int size() { return size; } + + public int capacity() { return capacity; }//���ڲ���shrink()&expand() + + public boolean isEmpty() { return size == 0; } + public void add(Object o){ - + add(size(), o); } public void add(int index, Object o){ - + if (index < 0 || size < index) throw new IndexOutOfBoundsException(); + expand(); + + for (int i = size; i > index; i--) + elementData[i] = elementData[i - 1]; + elementData[index] = o; + + size++; } + public void add(Object ...args) { + for (Object o : args) + add(o); + } public Object get(int index){ - return null; + if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + return elementData[index]; } public Object remove(int index){ - return null; - } - - public int size(){ - return -1; + if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + + Object removed = elementData[index]; + + for (int i = index; i < size - 1; i++) + elementData[i] = elementData[i + 1]; + size--; + shrink(); + return removed; } + + public void removeElems(int ...args) { + for (int i : args) + remove(i); + } public Iterator iterator(){ - return null; + return new ArrayListIterator(); } + private class ArrayListIterator implements Iterator { + private int current; + public boolean hasNext() { return current != size; } + public Object next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + + return elementData[current++]; + } + } + public static void showElements(ArrayList list) { + System.out.print("��ǰlist��Ԫ�أ�"); + Iterator iter = list.iterator(); + while (iter.hasNext()) + System.out.print(iter.next() + " "); + System.out.println(); + } + + public static void test(ArrayList list) { + System.out.println("--------������������---------"); + System.out.println("��ǰlist.isEmpty(): " + list.isEmpty()); + System.out.println("��ǰlist.size(): " + list.size()); + System.out.println("��ǰlist.capacity(): " + list.capacity()); + showElements(list); + + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(1, 2, 3, 4, 5); + test(list); + list.add(6, 7, 8, 9, 10); + test(list); + list.add(3, 11); + list.get(3); + test(list); + list.remove(3); + test(list); + list.add(11,12,13,14,15,16,17,18,19,20,21,22,23,24); + test(list); + + list.removeElems(1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); + test(list); + + + } } From f1691151953a645e9e602ed2111434a611b981df Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 12:32:42 +0800 Subject: [PATCH 3/9] add ignore file --- group22/2622819383/Task1/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 group22/2622819383/Task1/.gitignore diff --git a/group22/2622819383/Task1/.gitignore b/group22/2622819383/Task1/.gitignore new file mode 100644 index 0000000000..5241a7220a --- /dev/null +++ b/group22/2622819383/Task1/.gitignore @@ -0,0 +1 @@ +*.class \ No newline at end of file From 23f62d1c7341d127f0039e6a18da53cca798d48e Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:01:42 +0800 Subject: [PATCH 4/9] done with LinkedList&Queue&Stack no Test --- group22/2622819383/Task1/LinkedList.java | 127 +++++++++++++++++------ group22/2622819383/Task1/Queue.java | 10 +- group22/2622819383/Task1/Stack.java | 11 +- 3 files changed, 105 insertions(+), 43 deletions(-) diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index d431e25fdb..70ddd5d7a0 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,59 +1,118 @@ public class LinkedList implements List { - private Node head; + private Node header; + + private Node trailer; + + private int theSize; + + public LinkedList() { + header = new Node(null, null, null); + trailer = new Node(null, header, null); + header.succ = trailer; + theSize = 0; + } public void add(Object o){ - + add(size(), o); } public void add(int index , Object o){ - + if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; + while (0 < index--) p = p.succ(); + p.insertAsSucc(o); + theSize++; } public Object get(int index){ - return null; + if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); + while (0 < index--) p = p.succ(); + + return p.data(); } public Object remove(int index){ - return null; + if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); + while (0 < index--) p = p.succ(); + Object removed = p.data(); + p.pred().succ = p.succ(); + p.succ().pred = p.pred(); + theSize--; + return removed; } public int size(){ - return -1; + return theSize; } public void addFirst(Object o){ - + header.insertAsSucc(o); } public void addLast(Object o){ - + trailer.insertAsPred(o); } public Object removeFirst(){ - return null; + return remove(0); } public Object removeLast(){ - return null; + return remove(theSize - 1); } public Iterator iterator(){ - return null; - } - + return new LinkedListIterator(); + } + private class LinkedListIterator implements Iterator { + private Node current = header.succ(); + public boolean hasNext() { + return current != trailer; + } + public Object next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + Object item = current.data(); + current = current.succ(); + return item; + } + } - private static class Node{ - Object data; - Node next; + private static class Node{ + private Object data; + private Node pred; + private Node succ; + public Node(Object d, Node p, Node s) { + data = d; + pred = p; + succ = s; + } + public Object data() { return data; } + public Node succ() { return succ; } + public Node pred() { return pred; } + //����ǰ���ڵ㣬�����½ڵ� + public Node insertAsPred(Object data) { + Node p = new Node(data, pred, this); + pred = pred.succ = p; + return p; + } + public Node insertAsSucc(Object data) { + Node p = new Node(data, this, succ); + succ = succ.pred = p; + return p; + } + + } /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + * �Ѹ��������� + * ��������Ϊ 3->7->10 , ���ú��Ϊ 10->7->3 */ public void reverse(){ } /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + * ɾ��һ����������ǰ�벿�� + * ���磺list = 2->5->7->8 , ɾ���Ժ��ֵΪ 7->8 + * ���list = 2->5->7->8->10 ,ɾ���Ժ��ֵΪ7,8,10 */ public void removeFirstHalf(){ @@ -61,7 +120,7 @@ public void removeFirstHalf(){ } /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * �ӵ�i��Ԫ�ؿ�ʼ�� ɾ��length ��Ԫ�� �� ע��i��0��ʼ * @param i * @param length */ @@ -69,11 +128,11 @@ public void remove(int i, int length){ } /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 + * �ٶ���ǰ������list���������������е����� + * �ӵ�ǰ������ȡ����Щlist��ָ����Ԫ�� + * ���統ǰ���� = 11->101->201->301->401->501->601->701 * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] + * ���صĽ��Ӧ����[101,301,401,601] * @param list */ public static int[] getElements(LinkedList list){ @@ -81,8 +140,8 @@ public static int[] getElements(LinkedList list){ } /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 + * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * �ӵ�ǰ��������ɾ����list�г��ֵ�Ԫ�� * @param list */ @@ -92,16 +151,16 @@ public void subtract(LinkedList list){ } /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + * ��֪��ǰ�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * ɾ����������ֵ��ͬ�Ķ���Ԫ�أ�ʹ�ò���������Ա�������Ԫ�ص�ֵ������ͬ�� */ public void removeDuplicateValues(){ } /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * ��дһ��Ч���㷨��ɾ����������ֵ����min��С��max��Ԫ�أ������д���������Ԫ�أ� * @param min * @param max */ @@ -110,8 +169,8 @@ public void removeRange(int min, int max){ } /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * ���赱ǰ�����Ͳ���listָ������������Ԫ����ֵ�����������У�ͬһ���е�Ԫ��ֵ������ͬ�� + * ��Ҫ������������C����Ԫ��Ϊ��ǰ������list��Ԫ�صĽ������ұ�C�е�Ԫ������ֵ������������ * @param list */ public LinkedList intersection( LinkedList list){ diff --git a/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java index 08f1e8ba29..654a1e3e38 100644 --- a/group22/2622819383/Task1/Queue.java +++ b/group22/2622819383/Task1/Queue.java @@ -1,17 +1,19 @@ public class Queue { + private LinkedList elementData = new LinkedList(); - public void enQueue(Object o){ + public void enQueue(Object o){ + elementData.addLast(o); } public Object deQueue(){ - return null; + return elementData.removeFirst(); } public boolean isEmpty(){ - return false; + return size() == 0; } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java index 6c8c49bb10..17a4d0bd0f 100644 --- a/group22/2622819383/Task1/Stack.java +++ b/group22/2622819383/Task1/Stack.java @@ -1,20 +1,21 @@ public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o){ + public void push(Object o){ + elementData.add(o); } public Object pop(){ - return null; + return elementData.remove(size() - 1); } public Object peek(){ - return null; + return elementData.get(size() - 1); } public boolean isEmpty(){ - return false; + return size() == 0; } public int size(){ - return -1; + return elementData.size(); } } From ee94cd7ce5ff84826a35d360a631f4cb09e8110e Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:25:05 +0800 Subject: [PATCH 5/9] change from GB2312 to UTF-8 --- group22/2622819383/Task1/ArrayList.java | 103 ++++++++++++++---------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 1c060ffe3a..19b0b6ca53 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -8,11 +8,10 @@ public class ArrayList implements List { private Object[] elementData; - //add()ʱ�������� + //add()时用于在必要时刻扩充底层数组容量 private void expand() { - if (size < capacity) return;//��δ��Ա���������� - - if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//��������С���� + if (size < capacity) return;//尚未满员,不必扩容 + if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//不低于最小容量 Object[] oldElem = elementData; elementData = new Object[capacity <<= 1]; @@ -20,67 +19,65 @@ private void expand() { elementData[i] = oldElem[i]; } - //remove()ʱ�������� + //remove()时用于在必要时刻缩小底层数组容量 private void shrink() { - if (capacity < DEFAULT_CAPACITY << 1) return;//����������DEFAULT_CAPACITY���� - - if (capacity >> 2 < size) return; //��25%Ϊ�� + if (capacity < DEFAULT_CAPACITY << 1) return;//不致收缩至DEFAULT_CAPACITY以下 + if (capacity >> 2 < size) return; //以25%为界 Object[] oldElem = elementData; elementData = new Object[capacity >>= 1]; for (int i = 0; i < size; i++) - elementData[i] = oldElem[i]; - + elementData[i] = oldElem[i]; } public ArrayList() { clear(); } - public ArrayList(Object ...args) { - this(); - for (Object o : args) - add(o); - } public void clear() { size = 0; elementData = new Object[capacity = DEFAULT_CAPACITY]; } - public int size() { return size; } + public int size() { + return size; + } - public int capacity() { return capacity; }//���ڲ���shrink()&expand() + public int capacity() { //用于测试shrink()&expand() + return capacity; + } - public boolean isEmpty() { return size == 0; } + public boolean isEmpty() { + return size == 0; + } public void add(Object o){ add(size(), o); } + public void add(int index, Object o){ - if (index < 0 || size < index) throw new IndexOutOfBoundsException(); - expand(); + if (index < 0 || size < index) + throw new IndexOutOfBoundsException(); + expand(); for (int i = size; i > index; i--) elementData[i] = elementData[i - 1]; elementData[index] = o; - size++; } - public void add(Object ...args) { - for (Object o : args) - add(o); - } - + public Object get(int index){ - if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); - return elementData[index]; + if (index < 0 || size <= index) + throw new IndexOutOfBoundsException(); + + return elementData[index]; } public Object remove(int index){ - if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + if (index < 0 || size <= index) + throw new IndexOutOfBoundsException(); Object removed = elementData[index]; - for (int i = index; i < size - 1; i++) elementData[i] = elementData[i + 1]; size--; @@ -88,10 +85,7 @@ public Object remove(int index){ return removed; } - public void removeElems(int ...args) { - for (int i : args) - remove(i); - } + public Iterator iterator(){ return new ArrayListIterator(); @@ -99,15 +93,39 @@ public Iterator iterator(){ private class ArrayListIterator implements Iterator { private int current; - public boolean hasNext() { return current != size; } + + public boolean hasNext() { + return current != size; + } + public Object next() { - if (!hasNext()) throw new java.util.NoSuchElementException(); + if (!hasNext()) + throw new java.util.NoSuchElementException(); return elementData[current++]; } } + + + //以下方法便于测试 + + public ArrayList(Object ...args) { + this(); + for (Object o : args) + add(o); + } + + public void add(Object ...args) { + for (Object o : args) + add(o); + } + + public void removeElems(int ...args) { + for (int i : args) + remove(i); + } public static void showElements(ArrayList list) { - System.out.print("��ǰlist��Ԫ�أ�"); + System.out.print("当前list中元素:"); Iterator iter = list.iterator(); while (iter.hasNext()) System.out.print(iter.next() + " "); @@ -115,12 +133,11 @@ public static void showElements(ArrayList list) { } public static void test(ArrayList list) { - System.out.println("--------������������---------"); - System.out.println("��ǰlist.isEmpty(): " + list.isEmpty()); - System.out.println("��ǰlist.size(): " + list.size()); - System.out.println("��ǰlist.capacity(): " + list.capacity()); - showElements(list); - + System.out.println("--------基本方法测试---------"); + System.out.println("当前list.isEmpty(): " + list.isEmpty()); + System.out.println("当前list.size(): " + list.size()); + System.out.println("当前list.capacity(): " + list.capacity()); + showElements(list); } public static void main(String[] args) { From 4b527d28acde98b013b06bbe658bdcd9e749b792 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:33:45 +0800 Subject: [PATCH 6/9] change Tab --- group22/2622819383/Task1/ArrayList.java | 62 ++++++++++++------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 19b0b6ca53..55c8c616d0 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,12 +1,12 @@ public class ArrayList implements List { - private int size; + private int size; private int capacity; private static final int DEFAULT_CAPACITY = 10; - private Object[] elementData; + private Object[] elementData; //add()时用于在必要时刻扩充底层数组容量 private void expand() { @@ -51,29 +51,29 @@ public boolean isEmpty() { return size == 0; } - public void add(Object o){ - add(size(), o); - } - - public void add(int index, Object o){ + public void add(Object o){ + add(size(), o); + } + + public void add(int index, Object o){ if (index < 0 || size < index) throw new IndexOutOfBoundsException(); - expand(); + expand(); for (int i = size; i > index; i--) elementData[i] = elementData[i - 1]; elementData[index] = o; size++; - } - - public Object get(int index){ + } + + public Object get(int index){ if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); - + return elementData[index]; - } - - public Object remove(int index){ + } + + public Object remove(int index){ if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); @@ -82,15 +82,15 @@ public Object remove(int index){ elementData[i] = elementData[i + 1]; size--; shrink(); - return removed; - } - - - - public Iterator iterator(){ - return new ArrayListIterator(); - } - + return removed; + } + + + + public Iterator iterator(){ + return new ArrayListIterator(); + } + private class ArrayListIterator implements Iterator { private int current; @@ -105,21 +105,21 @@ public Object next() { return elementData[current++]; } } - - + + //以下方法便于测试 - + public ArrayList(Object ...args) { this(); for (Object o : args) add(o); } - + public void add(Object ...args) { for (Object o : args) add(o); } - + public void removeElems(int ...args) { for (int i : args) remove(i); @@ -131,7 +131,7 @@ public static void showElements(ArrayList list) { System.out.print(iter.next() + " "); System.out.println(); } - + public static void test(ArrayList list) { System.out.println("--------基本方法测试---------"); System.out.println("当前list.isEmpty(): " + list.isEmpty()); @@ -139,7 +139,7 @@ public static void test(ArrayList list) { System.out.println("当前list.capacity(): " + list.capacity()); showElements(list); } - + public static void main(String[] args) { ArrayList list = new ArrayList(1, 2, 3, 4, 5); test(list); From 05f0a9dcc608600e67b50bae9f679ba0b2a799a6 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:48:05 +0800 Subject: [PATCH 7/9] change Tab --- group22/2622819383/Task1/Iterator.java | 4 +-- group22/2622819383/Task1/LinkedList.java | 43 ++++++++++++++++++------ group22/2622819383/Task1/List.java | 10 +++--- group22/2622819383/Task1/Queue.java | 30 ++++++++--------- group22/2622819383/Task1/Stack.java | 38 +++++++++++---------- 5 files changed, 74 insertions(+), 51 deletions(-) diff --git a/group22/2622819383/Task1/Iterator.java b/group22/2622819383/Task1/Iterator.java index 96a43dbe0a..f390e63f3a 100644 --- a/group22/2622819383/Task1/Iterator.java +++ b/group22/2622819383/Task1/Iterator.java @@ -1,5 +1,5 @@ public interface Iterator { - public boolean hasNext(); - public Object next(); + public boolean hasNext(); + public Object next(); } diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 70ddd5d7a0..d1af15765f 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,6 +1,6 @@ public class LinkedList implements List { - private Node header; + private Node header; private Node trailer; @@ -12,26 +12,30 @@ public LinkedList() { header.succ = trailer; theSize = 0; } - public void add(Object o){ add(size(), o); } + public void add(int index , Object o){ if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; while (0 < index--) p = p.succ(); p.insertAsSucc(o); theSize++; } + public Object get(int index){ if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); - return p.data(); } + public Object remove(int index){ if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); Object removed = p.data(); @@ -48,23 +52,30 @@ public int size(){ public void addFirst(Object o){ header.insertAsSucc(o); } + public void addLast(Object o){ trailer.insertAsPred(o); } + public Object removeFirst(){ return remove(0); } + public Object removeLast(){ return remove(theSize - 1); } + public Iterator iterator(){ return new LinkedListIterator(); } + private class LinkedListIterator implements Iterator { private Node current = header.succ(); + public boolean hasNext() { return current != trailer; } + public Object next() { if (!hasNext()) throw new java.util.NoSuchElementException(); Object item = current.data(); @@ -77,28 +88,38 @@ private static class Node{ private Object data; private Node pred; private Node succ; + public Node(Object d, Node p, Node s) { data = d; pred = p; succ = s; } - public Object data() { return data; } - public Node succ() { return succ; } - public Node pred() { return pred; } - //����ǰ���ڵ㣬�����½ڵ� + + public Object data() { + return data; + } + + public Node succ() { + return succ; + } + + public Node pred() { + return pred; + } + + //����ǰ���ڵ㣬���ز�����½ڵ� public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); pred = pred.succ = p; return p; } + + //�����̽ڵ㣬���ز�����½ڵ� public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); succ = succ.pred = p; return p; - } - - - + } } /** diff --git a/group22/2622819383/Task1/List.java b/group22/2622819383/Task1/List.java index 4f7bcc71a8..c8f6da95a8 100644 --- a/group22/2622819383/Task1/List.java +++ b/group22/2622819383/Task1/List.java @@ -1,7 +1,7 @@ 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(); + 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/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java index 654a1e3e38..fa916cb089 100644 --- a/group22/2622819383/Task1/Queue.java +++ b/group22/2622819383/Task1/Queue.java @@ -1,19 +1,19 @@ public class Queue { private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ + + public void enQueue(Object o){ elementData.addLast(o); - } - - public Object deQueue(){ - return elementData.removeFirst(); - } - - public boolean isEmpty(){ - return size() == 0; - } - - public int size(){ - return elementData.size(); - } + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } } diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java index 17a4d0bd0f..3072c65370 100644 --- a/group22/2622819383/Task1/Stack.java +++ b/group22/2622819383/Task1/Stack.java @@ -1,21 +1,23 @@ public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ elementData.add(o); - } - - public Object pop(){ - return elementData.remove(size() - 1); - } - - public Object peek(){ - return elementData.get(size() - 1); - } - public boolean isEmpty(){ - return size() == 0; - } - public int size(){ - return elementData.size(); - } + } + + public Object pop(){ + return elementData.remove(size() - 1); + } + + public Object peek(){ + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } } From 6fc5ae0ccfe9d2f3d4862dd808cea4eec35b25ce Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 20:07:43 +0800 Subject: [PATCH 8/9] change Tab --- group22/2622819383/Task1/LinkedList.java | 263 ++++++++++++----------- 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index d1af15765f..0d87d2d72a 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -12,30 +12,31 @@ public LinkedList() { header.succ = trailer; theSize = 0; } - public void add(Object o){ - add(size(), o); - } - public void add(int index , Object o){ - if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); - + public void add(Object o) { + add(size(), o); + } + + public void add(int index , Object o) { + if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; while (0 < index--) p = p.succ(); p.insertAsSucc(o); theSize++; - } - - public Object get(int index){ + } + + public Object get(int index) { if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); - - Node p = header.succ(); + + Node p = header.succ(); while (0 < index--) p = p.succ(); return p.data(); - } - - public Object remove(int index){ - if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); - + } + + public Object remove(int index) { + if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); Object removed = p.data(); @@ -43,39 +44,39 @@ public Object remove(int index){ p.succ().pred = p.pred(); theSize--; return removed; - } - - public int size(){ - return theSize; - } - - public void addFirst(Object o){ - header.insertAsSucc(o); - } - - public void addLast(Object o){ - trailer.insertAsPred(o); - } - - public Object removeFirst(){ - return remove(0); - } - - public Object removeLast(){ - return remove(theSize - 1); - } - - public Iterator iterator(){ - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { + } + + public int size() { + return theSize; + } + + public void addFirst(Object o) { + header.insertAsSucc(o); + } + + public void addLast(Object o) { + trailer.insertAsPred(o); + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(theSize - 1); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { private Node current = header.succ(); - + public boolean hasNext() { return current != trailer; } - + public Object next() { if (!hasNext()) throw new java.util.NoSuchElementException(); Object item = current.data(); @@ -83,118 +84,118 @@ public Object next() { return item; } } - - private static class Node{ - private Object data; - private Node pred; + + private static class Node { + private Object data; + private Node pred; private Node succ; - + public Node(Object d, Node p, Node s) { data = d; pred = p; succ = s; } - + public Object data() { return data; } - + public Node succ() { return succ; } - + public Node pred() { return pred; } - + //����ǰ���ڵ㣬���ز�����½ڵ� public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); pred = pred.succ = p; return p; } - + //�����̽ڵ㣬���ز�����½ڵ� public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); succ = succ.pred = p; return p; } - } - - /** - * �Ѹ��������� - * ��������Ϊ 3->7->10 , ���ú��Ϊ 10->7->3 - */ - public void reverse(){ - - } - - /** - * ɾ��һ����������ǰ�벿�� - * ���磺list = 2->5->7->8 , ɾ���Ժ��ֵΪ 7->8 - * ���list = 2->5->7->8->10 ,ɾ���Ժ��ֵΪ7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * �ӵ�i��Ԫ�ؿ�ʼ�� ɾ��length ��Ԫ�� �� ע��i��0��ʼ - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * �ٶ���ǰ������list���������������е����� - * �ӵ�ǰ������ȡ����Щlist��ָ����Ԫ�� - * ���統ǰ���� = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * ���صĽ��Ӧ����[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� - * �ӵ�ǰ��������ɾ����list�г��ֵ�Ԫ�� + } - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * ��֪��ǰ�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� - * ɾ����������ֵ��ͬ�Ķ���Ԫ�أ�ʹ�ò���������Ա�������Ԫ�ص�ֵ������ͬ�� - */ - public void removeDuplicateValues(){ - - } - - /** - * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� - * ��дһ��Ч���㷨��ɾ����������ֵ����min��С��max��Ԫ�أ������д���������Ԫ�أ� - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * ���赱ǰ�����Ͳ���listָ������������Ԫ����ֵ�����������У�ͬһ���е�Ԫ��ֵ������ͬ�� - * ��Ҫ������������C����Ԫ��Ϊ��ǰ������list��Ԫ�صĽ������ұ�C�е�Ԫ������ֵ������������ - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } + /** + * �Ѹ��������� + * ��������Ϊ 3->7->10 , ���ú��Ϊ 10->7->3 + */ + public void reverse(){ + + } + + /** + * ɾ��һ����������ǰ�벿�� + * ���磺list = 2->5->7->8 , ɾ���Ժ��ֵΪ 7->8 + * ���list = 2->5->7->8->10 ,ɾ���Ժ��ֵΪ7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * �ӵ�i��Ԫ�ؿ�ʼ�� ɾ��length ��Ԫ�� �� ע��i��0��ʼ + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * �ٶ���ǰ������list���������������е����� + * �ӵ�ǰ������ȡ����Щlist��ָ����Ԫ�� + * ���統ǰ���� = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * ���صĽ��Ӧ����[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * �ӵ�ǰ��������ɾ����list�г��ֵ�Ԫ�� + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * ��֪��ǰ�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * ɾ����������ֵ��ͬ�Ķ���Ԫ�أ�ʹ�ò���������Ա�������Ԫ�ص�ֵ������ͬ�� + */ + public void removeDuplicateValues(){ + + } + + /** + * ��֪�����е�Ԫ����ֵ�����������У����Ե��������洢�ṹ�� + * ��дһ��Ч���㷨��ɾ����������ֵ����min��С��max��Ԫ�أ������д���������Ԫ�أ� + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * ���赱ǰ�����Ͳ���listָ������������Ԫ����ֵ�����������У�ͬһ���е�Ԫ��ֵ������ͬ�� + * ��Ҫ������������C����Ԫ��Ϊ��ǰ������list��Ԫ�صĽ������ұ�C�е�Ԫ������ֵ������������ + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } } From 9af937c194a2d0dfc73dfb7578fc81b31140124b Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 22:26:27 +0800 Subject: [PATCH 9/9] no test --- group22/2622819383/Task1/ArrayList.java | 1 + group22/2622819383/Task1/BinaryTreeNode.java | 78 +++++++++++++------- group22/2622819383/Task1/LinkedList.java | 6 +- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 55c8c616d0..d438fba17d 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,3 +1,4 @@ +//代码参考自《数据结构与算法分析》 public class ArrayList implements List { private int size; diff --git a/group22/2622819383/Task1/BinaryTreeNode.java b/group22/2622819383/Task1/BinaryTreeNode.java index 1f07869939..8c2b4492d2 100644 --- a/group22/2622819383/Task1/BinaryTreeNode.java +++ b/group22/2622819383/Task1/BinaryTreeNode.java @@ -1,30 +1,54 @@ +//����ο��ԡ����ݽṹ���㷨������ public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - + 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; + } + + private BinaryTreeNode parrent; + private BinaryTreeNode hot; //��ʾsearch(Object o)�������ص����нڵ�ĸ��� + + public BinaryTreeNode(Object o, BinaryTreeNode p) { + data = o; + parrent = p; + } + //����vΪ���Ķ������в��ҹؼ���o���������еĽڵ㣨��ʵ���ڵû���������ڵģ� + public static BinaryTreeNode search(BinaryTreeNode v, Object o, BinaryTreeNode hot) { + int vData = (int)v.getData(); + int searched = (int)o; + if (v == null || vData == searched) return v; + + hot = v; + return search(searched < vData ? v.getLeft() : v.getRight(), o, hot); + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = search(this, o, this.parrent); + if (node != null) return node; + + node = new BinaryTreeNode(o, hot); + if ((int)o < (int)hot.getData()) hot.setLeft(node); + else hot.setRight(node); + return node; + } + } diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 0d87d2d72a..81c1db409c 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,3 +1,4 @@ +//����ο��ԡ����ݽṹ���㷨������ public class LinkedList implements List { private Node header; @@ -86,6 +87,7 @@ public Object next() { } private static class Node { + //pred��succ�������ԣ�pred()��succ()����Node�ڵ� private Object data; private Node pred; private Node succ; @@ -111,14 +113,14 @@ public Node pred() { //����ǰ���ڵ㣬���ز�����½ڵ� public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); - pred = pred.succ = p; + pred = pred().succ = p; return p; } //�����̽ڵ㣬���ز�����½ڵ� public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); - succ = succ.pred = p; + succ = succ().pred = p; return p; } }