Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.ZhoufeifeiJAVA.coding2017.basic;

public interface Iterator {
public boolean hasNext();
public Object next();

}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;i<size-1;i++)
elementData[i] = elementData[i+1];
size --;
return removeObject;
}

public int size(){
return size;
}

private class MyArrayListIterator implements Iterator{
private int pointer;
MyArrayListIterator(){
pointer = 0;
}
public boolean hasNext() {
if(pointer < size)
return true;
else
return false;
}
public Object next() {
pointer ++;
return elementData[pointer-1];
}

}
public Iterator iterator(){
return new MyArrayListIterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.ZhoufeifeiJAVA.coding2017.basic;
public class MyArrayListTest{
public static void main(String[] args){
MyArrayList al = new MyArrayList();
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).toString());
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.github.ZhoufeifeiJAVA.coding2017.basic;

public class MyLinkedList implements List {

private Node head,tail;
private int size = 0;

public void add(Object o){
if(size == 0){
head = new Node();
tail = head;
head.data = o;
}
else{
Node newNode = new Node();
newNode.data = o;
newNode.next = null;
tail.next = newNode;
tail = newNode;
}
size ++;
}
public void add(int index , Object o)throws IndexOutOfBoundsException{
if(index>size || 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(i<index-1){
i++;
pointer = pointer.next;
}
newNode.next = pointer.next;
pointer.next = newNode;
}
size++;
}
public Object get(int index){
if(index<0 || index>size-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);
//}
}







Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}