forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from zjt-github/master
JaxWilliams - Homework of BasicDatastruct
- Loading branch information
Showing
8 changed files
with
457 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by zhangjiatao on 2017/2/25. | ||
*/ | ||
public class ArrayList implements List { | ||
private int size = 0; | ||
private Object[] elementData = new Object[100]; | ||
|
||
@Override | ||
public void add(Object o) { | ||
if(isFull()) { | ||
elementData = expandArray(elementData); | ||
} | ||
elementData[size++] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
ifOutOfBounds(index); | ||
if(isFull()) { | ||
elementData = expandArray(elementData); | ||
} | ||
System.arraycopy(elementData,index,elementData,index+1,size++); | ||
elementData[index] = o; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
ifOutOfBounds(index); | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
ifOutOfBounds(index); | ||
Object delData = elementData[index]; | ||
System.arraycopy(elementData,index+1,elementData,index,size-index); | ||
size--; | ||
return index; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String str = ""; | ||
for(int i=0; i<=size-1; i++){ | ||
str += elementData[i] + " "; | ||
} | ||
return str; | ||
} | ||
|
||
private boolean isFull() { | ||
if(size >= elementData.length-1){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean isEmpty() { | ||
if(size == 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void ifOutOfBounds(int index) { | ||
if(index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private Object[] expandArray(Object[] arr) { | ||
return Arrays.copyOf(arr, arr.length+10); | ||
} | ||
|
||
public Iterator iterator = new ArrayListIterator(this); | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
private ArrayList arrList; | ||
int position = 0,length; | ||
|
||
private ArrayListIterator(ArrayList arrList){ | ||
this.arrList = arrList; | ||
this.length = arrList.size(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return position < length; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return arrList.get(position++); | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList arr = new ArrayList(); | ||
arr.add(1); | ||
arr.add(2); | ||
arr.add(3); | ||
arr.add(1,4); | ||
arr.remove(2); | ||
System.out.println(arr.toString()); | ||
System.out.println(arr.size()); | ||
System.out.println(arr.iterator.next()); | ||
System.out.println(arr.iterator.next()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
group08/108621969/2-26/com/coding/basic/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* Created by zhangjiatao on 2017/2/25. | ||
*/ | ||
public class BinaryTreeNode { | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Object o) { | ||
this.data = o; | ||
this.left = null; | ||
this.right = null; | ||
} | ||
|
||
public int getData() { | ||
return (int) 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) { | ||
BinaryTreeNode newNode = new BinaryTreeNode(o); | ||
insertInto(this, newNode); | ||
return this; | ||
} | ||
|
||
private void insertInto(BinaryTreeNode tree, BinaryTreeNode o) { | ||
if (o.getData() <= tree.getData()) { | ||
if (tree.getLeft() != null) insertInto(tree.getLeft(), o); | ||
else tree.setLeft(o); | ||
} else { | ||
if (tree.getRight() != null) insertInto(tree.getRight(), o); | ||
else tree.setRight(o); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* Created by zhangjiatao on 2017/2/25. | ||
*/ | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
174 changes: 174 additions & 0 deletions
174
group08/108621969/2-26/com/coding/basic/LinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Created by zhangjiatao on 2017/2/25. | ||
*/ | ||
public class LinkedList implements List { | ||
private Node head = null,tail = null; | ||
private int size=0; | ||
private LinkedListIterator iterator = null; | ||
|
||
@Override | ||
public void add(Object o) { | ||
Node newNode = new Node(o); | ||
if(tail == null) { | ||
head = tail = newNode; | ||
}else { | ||
tail.next = newNode; | ||
tail = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
ifOutOfBounds(index); | ||
Node temp = head,previousNode = head; | ||
Node newNode = new Node(o); | ||
if(index == 0) { | ||
newNode.next = head; | ||
head = newNode; | ||
return ; | ||
} | ||
while(index-- > 0) { | ||
previousNode = temp; | ||
temp = temp.next; | ||
} | ||
previousNode.next = newNode; | ||
newNode.next = temp; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
ifOutOfBounds(index); | ||
Node temp = head; | ||
while(index-- > 0) { | ||
temp = temp.next; | ||
} | ||
return temp; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
ifOutOfBounds(index); | ||
Node temp = head,previousNode = head; | ||
if(head == tail) { | ||
head = tail = null; | ||
return temp; | ||
} | ||
while(index-- > 0) { | ||
previousNode = temp; | ||
temp = temp.next; | ||
} | ||
if(tail == temp) { | ||
tail = previousNode; | ||
} | ||
previousNode.next = temp.next; | ||
size--; | ||
return temp; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
add(0,o); | ||
} | ||
|
||
public void addLast(Object o) { | ||
add(o); | ||
} | ||
|
||
public Object removeFirst() { | ||
return remove(0); | ||
} | ||
|
||
public Object removeLast() { | ||
return remove(size()-1); | ||
} | ||
|
||
public LinkedListIterator Iterator() { | ||
if(iterator ==null) { | ||
iterator = new LinkedListIterator(this); | ||
} | ||
return iterator; | ||
} | ||
|
||
private void ifOutOfBounds(int index) { | ||
if(index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
private boolean isEmpty() { | ||
return size==0; | ||
} | ||
|
||
public String toString() { | ||
String str = ""; | ||
Node temp = head; | ||
while(temp!=tail) { | ||
str = str + temp.data + " "; | ||
temp = temp.next; | ||
} | ||
return str + tail.data; | ||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
public Node(Object o) { | ||
this.data = o; | ||
this.next = null; | ||
} | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
private LinkedList linkedList = null; | ||
private Node currentNode = null; | ||
|
||
public LinkedListIterator(LinkedList linkedList) { | ||
this.linkedList = linkedList; | ||
this.currentNode = this.linkedList.head; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if(linkedList.isEmpty()) return false; | ||
return currentNode.next != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object data = currentNode.data; | ||
if(hasNext()) { | ||
currentNode = currentNode.next; | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList arr = new LinkedList(); | ||
arr.add(1); | ||
arr.add(2); | ||
arr.add(3); | ||
arr.add(5); | ||
arr.add(1,4); | ||
arr.remove(2); | ||
arr.removeLast(); | ||
arr.addFirst(5); | ||
System.out.println(arr.toString()); | ||
System.out.println(arr.size()); | ||
System.out.println(arr.Iterator().hasNext()); | ||
System.out.println(arr.Iterator().next()); | ||
System.out.println(arr.Iterator().next()); | ||
System.out.println(arr.Iterator().next()); | ||
System.out.println(arr.Iterator().next()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* Created by zhangjiatao on 2017/2/25. | ||
*/ | ||
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(); | ||
} |
Oops, something went wrong.