From 46fb00afe02ddb4b9ff503dd190b4f5e1cba1970 Mon Sep 17 00:00:00 2001 From: Yanbo Date: Sun, 12 Mar 2017 19:03:50 +0800 Subject: [PATCH] =?UTF-8?q?'=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E6=A5=AD'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group23/609041842/src/code/ArrayList.java | 49 ++++++++++++ group23/609041842/src/code/LinkedList.java | 87 ++++++++++++++++++++++ group23/609041842/src/code/Queue.java | 17 +++++ group23/609041842/src/code/Stack.java | 30 ++++++++ 4 files changed, 183 insertions(+) create mode 100644 group23/609041842/src/code/ArrayList.java create mode 100644 group23/609041842/src/code/LinkedList.java create mode 100644 group23/609041842/src/code/Queue.java create mode 100644 group23/609041842/src/code/Stack.java diff --git a/group23/609041842/src/code/ArrayList.java b/group23/609041842/src/code/ArrayList.java new file mode 100644 index 0000000000..e24f37e35a --- /dev/null +++ b/group23/609041842/src/code/ArrayList.java @@ -0,0 +1,49 @@ +package code; + +import java.util.Arrays; + +public class ArrayList { + + private Object[] obj = new Object[0]; + + public void add(Object o) { + Object[] tar = new Object[obj.length + 1]; + System.arraycopy(obj, 0, tar, 0, obj.length); + tar[tar.length - 1] = o; + obj = tar; + System.out.println(Arrays.toString(obj)); + } + + public void add(int index, Object o) { + Object[] tar = new Object[obj.length + 1]; + System.arraycopy(obj, 0, tar, 0, index); + tar[index] = o; + System.arraycopy(obj, index, tar, index + 1, obj.length - index); + obj = tar; + } + + public Object get(int index) { + return obj[index]; + } + + public int size(){ + return obj.length; + } + + public Object remove(int index){ + Object[] tar = new Object[obj.length-1]; + System.arraycopy(obj, 0, tar, 0, index); + System.arraycopy(obj, index+1, tar, index, obj.length-index-1); + Object o = obj[index]; + obj = tar; + return o;//���ر�ɾԪ�� + } + public static void main(String[] args) { + ArrayList al = new ArrayList(); + al.add("hello"); + al.add("java"); + al.add(2, "addm"); + System.out.println(al.remove(1)); + } + +} diff --git a/group23/609041842/src/code/LinkedList.java b/group23/609041842/src/code/LinkedList.java new file mode 100644 index 0000000000..6f2ac5158f --- /dev/null +++ b/group23/609041842/src/code/LinkedList.java @@ -0,0 +1,87 @@ +package code; + +public class LinkedList { + private static Node head; + private Node last; + public int size; + + public void add(Object o) { + Node l = last; + Node newNode = new Node(l, o, null); + last = newNode; + if (head == null) { + head = newNode; + size = 1; + } else { + l.next = newNode; + size++; + } + } + + public void add(int index, Object o) { + Node n = node(index); + System.out.println(n.data); + Node pred = n.prev; + Node newNode = new Node(pred, o, n); + if (pred == null) { + head = newNode; + } else { + pred.next = newNode; + } + size++; + } + + + public Node get(int index){ + return node(index); + } + public Node node(int index) { + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Node remove(int index){ + Node del = node(index); + Node after = del.next; + Node before = del.prev; + before.next = after; + after.prev = before; + size--; + return del; + } + private static class Node { + Node next; + Object data; + Node prev; + + private Node(Node prev, Object data, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } + + public static void main(String[] arg) { + LinkedList ll = new LinkedList(); + ll.add("hello"); + ll.add("java"); + ll.add("jvm"); + ll.add("jvmd"); + // System.out.println(ll.get(2)); +// ll.add(1, "ds"); + System.out.println(ll.get(0).data); + System.out.println(ll.get(1).data); + System.out.println(ll.get(2).data); + System.out.println(ll.get(3).data); + System.out.println(ll.size); + System.out.println(ll.remove(1).data); + System.out.println(ll.get(0).data); + System.out.println(ll.get(1).data); + System.out.println(ll.get(2).data); + System.out.println(ll.size); + } + +} diff --git a/group23/609041842/src/code/Queue.java b/group23/609041842/src/code/Queue.java new file mode 100644 index 0000000000..4f18db0129 --- /dev/null +++ b/group23/609041842/src/code/Queue.java @@ -0,0 +1,17 @@ +package code; + +public class Queue { + + private LinkedList lk = new LinkedList(); + public void enQueue(Object o){ + lk.add(o); + } + public void deQueue(){ + lk.remove(lk.size-1); + } + public boolean isEmpty(){ + if(lk.size == 0) + return true; + return false; + } +} diff --git a/group23/609041842/src/code/Stack.java b/group23/609041842/src/code/Stack.java new file mode 100644 index 0000000000..315b07f5a8 --- /dev/null +++ b/group23/609041842/src/code/Stack.java @@ -0,0 +1,30 @@ +package code; + +public class Stack { + + private ArrayList array = new ArrayList(); + + public void push(Object o){ + array.add(o); + } + public Object pop(){ + return array.remove(array.size()-1); + } + + public boolean isEmpty(){ + if(array.size()<=0) + return true; + return false; + } + + public int size(){ + return array.size(); + } + public static void main(String[] args) { + Stack sc = new Stack(); + sc.push("hello world"); + sc.push("java"); + sc.push("jvm"); + } + +}