From 2783ab6fa31acc1d3908e8dcd73289535eff9db0 Mon Sep 17 00:00:00 2001 From: kilien Date: Sun, 9 Apr 2017 10:36:33 +0800 Subject: [PATCH] week05 --- .../jvm/loader/ClassFileLoader.java | 51 +++ .../jvm/test/ClassFileloaderTest.java | 92 ++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++ .../src/com/coding/basic/BinaryTree.java | 40 +++ .../src/com/coding/basic/BinaryTreeNode.java | 33 ++ .../src/com/coding/basic/Iterator.java | 1 - .../790466157/src/com/coding/basic/Queue.java | 71 +--- .../src/com/coding/basic/array/ArrayList.java | 98 ++++++ .../src/com/coding/basic/array/ArrayUtil.java | 262 +++++++++++++++ .../com/coding/basic/array/ArrayUtilTest.java | 149 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 161 +++++++++ .../basic/linklist/LRUPageFrameTest.java | 34 ++ .../com/coding/basic/linklist/LinkedList.java | 305 ++++++++++++++++++ .../src/com/coding/basic/stack/Stack.java | 37 +++ .../src/com/coding/basic/stack/StackUtil.java | 101 ++++++ 15 files changed, 1408 insertions(+), 55 deletions(-) create mode 100644 group09/790466157/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java create mode 100644 group09/790466157/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 group09/790466157/mini-jvm/src/com/coderising/jvm/test/EmployeeV1.java create mode 100644 group09/790466157/src/com/coding/basic/BinaryTree.java create mode 100644 group09/790466157/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group09/790466157/src/com/coding/basic/array/ArrayList.java create mode 100644 group09/790466157/src/com/coding/basic/array/ArrayUtil.java create mode 100644 group09/790466157/src/com/coding/basic/array/ArrayUtilTest.java create mode 100644 group09/790466157/src/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 group09/790466157/src/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 group09/790466157/src/com/coding/basic/linklist/LinkedList.java create mode 100644 group09/790466157/src/com/coding/basic/stack/Stack.java create mode 100644 group09/790466157/src/com/coding/basic/stack/StackUtil.java diff --git a/group09/790466157/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/group09/790466157/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..51c9ba7c34 --- /dev/null +++ b/group09/790466157/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,51 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws Exception { + URL base = this.getClass().getResource("/"); + String baseToString = ""+base; + String filePath = baseToString.replaceAll("file:/", "")+className.replace(".", "\\")+".class"; + //String filePath = clzPaths.get(0)+"\\"+className.replace(".", "\\")+".class"; //符合Junit测试调用addClassPath方法 + File file = new File(filePath); + + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte[] buffer = new byte[1024]; + int len = 0; + while((len = bis.read(buffer)) != -1){ + baos.write(buffer,0,len); + } + return baos.toByteArray(); + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuffer strBuffer = new StringBuffer(); + for(int i=0;i ((Integer)currentNode.getData())){ + currentNode = currentNode.getRight(); + if(currentNode == null){ + parentNode.setRight(node); + return node; + } + }else{ + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parentNode.setLeft(node); + return node; + } + } + } + } + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/BinaryTreeNode.java b/group09/790466157/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..bb02141c8a --- /dev/null +++ b/group09/790466157/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.left = null; + this.right = null; + this.data = data; + } + + public Object getData() { + return data; + } + public void setData(int 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; + } +} diff --git a/group09/790466157/src/com/coding/basic/Iterator.java b/group09/790466157/src/com/coding/basic/Iterator.java index 7c02cc6e51..acfb2d36d2 100644 --- a/group09/790466157/src/com/coding/basic/Iterator.java +++ b/group09/790466157/src/com/coding/basic/Iterator.java @@ -3,5 +3,4 @@ public interface Iterator { public boolean hasNext(); public Object next(); - } \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/Queue.java b/group09/790466157/src/com/coding/basic/Queue.java index 80d0dc9835..e1c2eda131 100644 --- a/group09/790466157/src/com/coding/basic/Queue.java +++ b/group09/790466157/src/com/coding/basic/Queue.java @@ -1,68 +1,31 @@ package com.coding.basic; -import java.util.Arrays; + +import com.coding.basic.linklist.LinkedList; public class Queue { - private static final int CAPACITY = 10; - private static int capacity; - private static int front; - private static int tail; - private static Object[] array; - - public Queue(){ - this.capacity = CAPACITY; - array = new Object[capacity]; - front = tail = 0; - } + private LinkedList linkedlist = new LinkedList(); - public void enQueue(Object o) throws ExceptionQueueFull { - if (size() == capacity -1) - throw new ExceptionQueueFull("Queue is full"); - array[tail] = o; - tail = (tail +1) % capacity; + public void enQueue(Object o){ + linkedlist.add(o); } - public Object deQueue() throws ExceptionQueueEmpty{ - Object o; - if (isEmpty()) - throw new ExceptionQueueEmpty("Queue is empty"); - o = array[front]; - front = (front + 1) % capacity; - return o; + public Object deQueue(){ + if(!isEmpty()){ + return linkedlist.get(0); + }else{ + return null; + } } public boolean isEmpty(){ - return (front == tail); + if(size() > 0){ + return false; + }else{ + return true; + } } public int size(){ - if (isEmpty()) - return 0; - else - return (capacity + tail - front) % capacity; - } - - public class ExceptionQueueEmpty extends Exception { - // Constructor - public ExceptionQueueEmpty() { - - } - - // Constructor with parameters - public ExceptionQueueEmpty(String mag) { - System.out.println(mag); - } - } - - public class ExceptionQueueFull extends Exception { - - // Constructor - public ExceptionQueueFull() { - - } - - // Constructor with parameters - public ExceptionQueueFull(String mag) { - System.out.println(mag); - } + return linkedlist.size(); } } \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/array/ArrayList.java b/group09/790466157/src/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..2b25623dba --- /dev/null +++ b/group09/790466157/src/com/coding/basic/array/ArrayList.java @@ -0,0 +1,98 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if( size <= elementData.length){ + elementData[size + 1] = o; + size++; + }else{ + elementData = grow(elementData, 1); + elementData[size+1] = o; + size++; + } + } + public void add(int index, Object o){ + Object[] temp = new Object[elementData.length]; + for(int i = 0; i= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + public static Object[] grow(Object[]src, int size){ + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/array/ArrayUtil.java b/group09/790466157/src/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..471f939e50 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,262 @@ +package com.coding.basic.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + //注意边界条件 + if(origin == null || origin.length == 0){ + return; + } + + for(int i=0, j = origin.length-1; i array2[j]){ + newArray[count++] = array2[j++]; + }else if(array1[i] == array2[j]){ + newArray[count++] = array2[j++]; + i++; + } + } + + while(i==array1.length && j= max){ + break; + }else{ + count++; + } + } + + return Arrays.copyOf(a,count); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 3){ + return new int[0]; + } + + boolean[] isPrime = isPrime(max); + + int[] array = new int[max]; + int count = 1; + array[0] = 2; + for(int i = 3; i < max; i++){ + if(isPrime[i]){ + array[count++] = i; + } + } + return Arrays.copyOf(array, count); + } + + private boolean[] isPrime(int max) { + boolean[] isPrime = new boolean[max]; + for(int i = 3; i < max; i++){ + if(i % 2 != 0 ){ + isPrime[i] = true; + }else{ + isPrime[i] = false; + } + } + + for(int i = 3; i < Math.sqrt(max); i++){ + if(isPrime[i]){ + for(int j = 2*i ; j < max; j += i){ + isPrime[j] = false; + } + } + } + return isPrime; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max < 0){ + return new int[0]; + } + + int[] Array = new int[max]; + + int count = 0; + for(int n = 1; n < max; n++) + { + int sum = 0; + for(int i=1; i< n; i++) + { + if(n%i == 0) + sum += i; + } + if(sum == n) + Array[count++] = n; + } + + return Arrays.copyOf(Array, count); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) + { + if(array == null || array.length == 0){ + return ""; + } + + StringBuilder buffer = new StringBuilder(); + + for(int i = 0; i < array.length; i++){ + buffer.append(array[i]); + if(i < array.length - 1){ + buffer.append(seperator); + } + } + + return buffer.toString(); + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/array/ArrayUtilTest.java b/group09/790466157/src/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..adac75e2a4 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,149 @@ +package com.coding.basic.array; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ArrayUtilTest { + + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception{ + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() + { + int[] a = {3, 5, 7, 8, 9}; + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + myArray.grow(a, -3);//注意这里 + } + + @Test + public void testFibonacci() + { + //max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2的时候没有素数,数组为空数组 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //传递空数组时,返回空字符串 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } +} diff --git a/group09/790466157/src/com/coding/basic/linklist/LRUPageFrame.java b/group09/790466157/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..cd45806a4e --- /dev/null +++ b/group09/790466157/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,161 @@ +package com.coding.basic.linklist; + +import java.util.Hashtable; + +/** + * 用双向链表实现LRU算法 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + Object value; + Object key; + + Node() { + } + } + + private int capacity; + private int currentSize; + private Hashtable nodes; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int i) { + currentSize = 0; + capacity = i; + nodes = new Hashtable(capacity); + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + * @return + */ + public Object get(Object key) { + Node node = (Node) nodes.get(key); + if (node != null) { + moveToHead(node); + return node.value; + } + else { + return null; + } + } + /** + * 添加缓存 + * + * @param key + * @param value + */ + public void put(Object key, Object value) { + Node node = (Node) nodes.get(key); + + if (node == null) { + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + if (last != null)// 将最少使用的删除 + nodes.remove(last.key); + removeLast(); + } else { + currentSize++; + } + + node = new Node(); + } + node.value = value; + node.key = key; + // 将最新使用的节点放到链表头,表示最新使用的. + moveToHead(node); + nodes.put(key, node); + } + + /** + * 将缓存删除 + * + * @param key + * @return + */ + public Object remove(Object key) { + Node node = (Node) nodes.get(key); + if (node != null) { + if (node.prev != null) { + node.prev.next = node.next; + } + if (node.next != null) { + node.next.prev = node.prev; + } + if (last == node) + last = node.prev; + if (first == node) + first = node.next; + } + return node; + } + + public void clear() { + first = null; + last = null; + } + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + // 链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象) + if (last != null) { + if (last.prev != null) + last.prev.next = null; + else + first = null; + last = last.prev; + } + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveToHead(Node node) { + if (node == first) + return; + if (node.prev != null) + node.prev.next = node.next; + if (node.next != null) + node.next.prev = node.prev; + if (last == node) + last = node.prev; + if (first != null) { + node.next = first; + first.prev = node; + } + first = node; + node.prev = null; + if (last == null) + last = first; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.key); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group09/790466157/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..ef89b34fb2 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.put(1,0); + frame.put(0,1); + frame.put(7,2); + frame.get(7); + frame.get(0); + frame.get(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.put(2,0); + Assert.assertEquals("2,1,0", frame.toString()); + frame.put(0,1); + Assert.assertEquals("0,2,1", frame.toString()); + frame.put(0,1); + Assert.assertEquals("0,2,1", frame.toString()); + frame.put(3,1); + Assert.assertEquals("3,0,2", frame.toString()); + frame.put(0,1); + Assert.assertEquals("0,3,2", frame.toString()); + frame.put(4,1); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/linklist/LinkedList.java b/group09/790466157/src/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..cea86b62a7 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,305 @@ +package com.coding.basic.linklist; + +import java.util.NoSuchElementException; + +public class LinkedList { + + private int size = 0; + + private Node head; + + public Node getHead() { + return head; + } + + public LinkedList() { + this.head = new Node(); + } + + @Override + public String toString() { + return "[" + head + "]"; + } + + private void outOfBoundsForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private void outOfBoundsForOther(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + public void add(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + public void add(int index, Object o) { + outOfBoundsForAdd(index); + if (size == index) + add(o); + else { + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node nextNode = prevNode.next; + Node node = new Node(o); + prevNode.next = node; + node.next = nextNode; + size++; + } + } + + public Object get(int index) { + outOfBoundsForOther(index); + Node node = head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + outOfBoundsForOther(index); + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node node = prevNode.next; + prevNode.next = node.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + Node node = head.next; + head.next = newNode; + newNode.next = node; + size++; + } + + public void addLast(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + private void noSuchEle() { + if (head.next == null) + throw new NoSuchElementException("没有这个元素"); + } + + public Object removeFirst() { + noSuchEle(); + Node node = head.next; + head.next = node.next; + size--; + return node.data; + } + + public Object removeLast() { + noSuchEle(); + Node node = head; + for (int i = 0; i < size - 1; i++) { + node = node.next; + } + Node reNode = node.next; + node.next = null; + size--; + return reNode.data; + } + + public static class Node { + Object data; + Node next; + + @Override + public String toString() { + return data + ", " + next; + } + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + // ------------------------------------------------------------------------------------------ + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + Node node = head.next; + Object[] arr = new Object[size]; + int i = size - 1; + while (i >= 0) { + arr[i--] = node.data; + node = node.next; + } + node = head.next; + for (int j = 0; j < size; j++) { + node.data = arr[j]; + node = node.next; + } + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + int n = size / 2; + Node node = head.next; + for (int i = 0; i < n; i++) { + node = node.next; + } + head.next = node; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + Node node = head; + for (int j = 0; j < i; j++) { + node = node.next; + } + Node newNode = node; + for (int j = 0; j < length; j++) { + newNode = newNode.next; + } + node.next = newNode.next; + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + int length = list.size(); + int[] arr = new int[length]; + int index = -1; + for (int i = 0; i < length; i++) { + index = (int) list.get(i); + arr[i] = (int) get(index); + } + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + public void subtract(LinkedList list) { + Node preNode = head; + Node node = head.next; + int data = 0; + for (int i = 0; i < list.size(); i++) { + data = (int) list.get(i); + while ((int) node.data < data) { + preNode = node; + node = node.next; + } + if ((int) node.data == data) { + preNode.next = node.next; + node = node.next; + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node preNode = head.next; + Node node = preNode.next; + while (true) { + if (preNode.data == node.data) + preNode.next = node.next; + else + preNode = node; + if (node.next == null) + break; + node = node.next; + } + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + Node preNode = head; + Node node = preNode.next; + for (int i = 0; i < size; i++) { + if ((int) node.data <= min) { + preNode = node; + node = node.next; + } else if ((int) node.data < max) { + node = node.next; + } else { + break; + } + } + preNode.next = node; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + int size1 = size; + int size2 = list.size(); + int i = 0, j = 0; + LinkedList newList = new LinkedList(); + while (i < size1 && j < size2) { + if ((int) get(i) < (int) list.get(j)) + newList.add(get(i++)); + else + newList.add(list.get(j++)); + } + if (i == size1) { + for (; j < size2; j++) { + newList.add(list.get(j)); + } + } else { + for (; i < size1; i++) { + newList.add(get(i)); + } + } + return newList; + } +} diff --git a/group09/790466157/src/com/coding/basic/stack/Stack.java b/group09/790466157/src/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..a0b40e64d7 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/stack/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(!isEmpty()){ + return elementData.remove(size()-1); + }else{ + return null; + } + } + + public Object peek(){ + if(!isEmpty()){ + return elementData.get(size()-1); + }else{ + return null; + } + } + public boolean isEmpty(){ + if(size() > 0){ + return false; + }else{ + return true; + } + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/stack/StackUtil.java b/group09/790466157/src/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..8af0a1c13b --- /dev/null +++ b/group09/790466157/src/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,101 @@ +package com.coding.basic.stack; + +public class StackUtil { + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + if(!s.isEmpty()){ + Object top = s.pop(); + reverse(s); + toTop(s,top); + } + } + + private static void toTop(Stack s,Object t){ + if(s.isEmpty()){ + s.push(t); + return; + } + Object top = s.pop(); + toTop(s,t); + s.push(top); + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + if(s.isEmpty()){ + s.push(o); + return; + } + Object top = s.peek(); + remove(s,o); + s.push(top); + } + + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Stack getTop(Stack s,int len) { + Stack top = new Stack(); + // 若当前为空栈,则返回null + if (len == 0) { + return null; + } + while (!s.isEmpty()){ + for(int i = 0;i < len;i++){ + s.pop(); + top.push(s); + } + } + return top; + } + + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + Stack myStack = new Stack(); + char[] arr = s.toCharArray(); + for (char c : arr) { + Character temp = (Character) myStack.pop(); + // 栈为空时只将c入栈 + if (temp == null) { + myStack.push(c); + } + // 配对时c不入栈 + else if (temp == '[' && c == ']') { + } + // 配对时c不入栈 + else if (temp == '(' && c == ')') { + } + // 不配对时c入栈 + else { + myStack.push(temp); + myStack.push(c); + } + } + return myStack.isEmpty(); + } + + +} \ No newline at end of file