From a0ef984bc53b32faa28fc8da3103a2c475e73a3a Mon Sep 17 00:00:00 2001 From: ZhoufeifeiJAVA <793337535@qq.com> Date: Sun, 26 Feb 2017 19:42:24 +0800 Subject: [PATCH] MyArrayList,MyLinkedList,Queue,Stack --- .../coding2017/basic/Iterator.java | 7 + .../ZhoufeifeiJAVA/coding2017/basic/List.java | 9 ++ .../coding2017/basic/MyArrayList.java | 78 +++++++++++ .../coding2017/basic/MyArrayListTest.java | 34 +++++ .../coding2017/basic/MyLinkedList.java | 127 ++++++++++++++++++ .../coding2017/basic/MyLinkedListTest.java | 34 +++++ .../coding2017/basic/Queue.java | 26 ++++ .../coding2017/basic/QueueTest.java | 17 +++ .../coding2017/basic/Stack.java | 28 ++++ .../coding2017/basic/StackTest.java | 17 +++ 10 files changed, 377 insertions(+) create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayListTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedList.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java create mode 100644 group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..34d062b327 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java new file mode 100644 index 0000000000..7a13f14c83 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.ZhoufeifeiJAVA.coding2017.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(); +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..0666530694 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java @@ -0,0 +1,78 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o)throws RuntimeException{ + if(index > size) + throw new RuntimeException("index is bigger than the size of MyArrayList"); + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + if(index == size) + add(o); + else{ + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size ++; + } + private Object[] arrayGrow(Object[] src,int size){ + Object[] target = new Object[src.length+size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + public Object get(int index){ + if(index >= size) + return null; + else + return elementData[index]; + } + + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index>=size || index<0) + throw new IndexOutOfBoundsException("index is bigger than the size of MyArrayList"); + Object removeObject = elementData[index]; + for(int i=index;isize || index<0) + throw new IndexOutOfBoundsException("the index is bigger than the size of MyLinkedList"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0){ + newNode.next = head; + head = newNode; + } + else{ + int i = 0; + Node pointer = head; + while(isize-1) + return null; + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index --; + } + return pointer.data; + + } + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index<0 || index>size-1) + throw new IndexOutOfBoundsException("the index is not legal"); + Node pointer = head; + if(index == 0){ + head = head.next; + size --; + return pointer.data; + } + else{ + while(index>1){ + pointer = pointer.next; + index --; + } + Node temp = pointer.next; + pointer.next = pointer.next.next; + size --; + return temp.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + + private static class Node{ + Object data; + Node next; + } + private class MyLinkedListIterator implements Iterator{ + private Node pointer; + MyLinkedListIterator(){ + pointer = head; + } + public boolean hasNext() { + if(pointer.next != null) + return true; + else + return false; + } + public Object next() { + Node temp = pointer; + pointer = pointer.next; + return temp.data; + } + } + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + //public void showData(int index){ + // System.out.println("the data of "+index+" is "+get(index).data); + //} +} + + + + + + + diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..6d5ca14e70 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java @@ -0,0 +1,34 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class MyLinkedListTest{ + public static void main(String[] args){ + MyLinkedList al = new MyLinkedList(); + al.add("string0"); + al.add("string1"); + al.add("string2"); + al.add("string3"); + al.add("string4"); + al.add("string5"); + al.add("string6"); + al.add("string7"); + al.add("string8"); + al.add("string9"); + al.add("string10"); + al.add("string11"); + al.add("string12"); + al.add("string13"); + al.add("string14"); + al.add(11,"string10.5"); + sop(al.remove(4)); + sop(al.get(10)); + sop(al.get(11)); + sop(al.get(12)); + sop("the size of al is "+al.size()); + sop("the iterator method used,so the result is as follows:"); + for(Iterator it = al.iterator();it.hasNext();){ + sop(it.next()); + } + } + public static void sop(Object o){ + System.out.println(o); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java new file mode 100644 index 0000000000..0ce735f150 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java @@ -0,0 +1,26 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Queue { + private MyLinkedList llist = null; + Queue(){ + llist = new MyLinkedList(); + } + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + return llist.removeFirst(); + } + + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + + public int size(){ + return llist.size(); + } +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..95cc5117ff --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class QueueTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("String0"); + q.enQueue("String1"); + q.enQueue("String2"); + sop("the queue is not empty "+q.isEmpty()); + sop("the size of queue is "+q.size()); + sop("out queue "+q.deQueue()); + sop("out queue "+q.deQueue()); + sop("the size of queue is "+q.size()); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3992f89f86 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Stack { + private MyLinkedList llist = null; + Stack(){ + llist = new MyLinkedList(); + } + public void push(Object o){ + llist.add(o); + } + + public Object pop(){ + return llist.removeLast(); + } + + public Object peek(){ + return llist.get(llist.size()-1); + } + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + public int size(){ + return llist.size(); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..4bcb60e35d --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class StackTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Stack s = new Stack(); + s.push("String0"); + s.push("String1"); + s.push("String2"); + sop("the queue is not empty "+s.isEmpty()); + sop("the size of queue is "+s.size()); + sop("out queue "+s.pop()); + sop("just watch queue,not delete "+s.peek()); + sop("the size of queue is "+s.size()); + } +} \ No newline at end of file