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 #2 from C-BoBo/master
HomeWork0226
- Loading branch information
Showing
12 changed files
with
1,435 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1,056 changes: 1,056 additions & 0 deletions
1,056
group05/183127807/HomeWork0226/.idea/workspace.xml
Large diffs are not rendered by default.
Oops, something went wrong.
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
85 changes: 85 additions & 0 deletions
85
group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.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,85 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private static final int DEFAULT_CAPACITY = 100; | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[DEFAULT_CAPACITY]; | ||
|
||
public void add(Object o){ | ||
ensureCapacity(size+1); | ||
elementData[size] = o; | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
if (index > size || index < 0) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
ensureCapacity(size+1); | ||
for (int i = size;i>index;i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index > size || index < 0) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
|
||
public Object remove(int index){ | ||
Object[] removeData = (Object[]) elementData[index]; | ||
for (int i =index;i<size()-1;i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[size] = null; | ||
size--; | ||
|
||
return removeData; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
|
||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
private int current = 0; | ||
@Override | ||
public boolean hasNext() { | ||
return current<size(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return elementData[current++]; | ||
} | ||
} | ||
|
||
|
||
public void ensureCapacity(int minCapacity) { | ||
if (minCapacity < elementData.length) { | ||
return; | ||
} else if (minCapacity > elementData.length) { | ||
|
||
elementData = Arrays.copyOf(elementData, minCapacity + DEFAULT_CAPACITY); | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group05/183127807/HomeWork0226/src/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,32 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return 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){ | ||
return null; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
142 changes: 142 additions & 0 deletions
142
group05/183127807/HomeWork0226/src/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,142 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
private int size ; | ||
|
||
private Node current = head; | ||
public LinkedList() { | ||
head = null; | ||
size = 0; | ||
} | ||
|
||
public void add(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
if (current.next == null) | ||
{ | ||
current.next = newNode; | ||
} | ||
|
||
while (current.next != null){ | ||
current = current.next; | ||
} | ||
current.next = newNode; | ||
size++; | ||
|
||
} | ||
public void add(int index , Object o){ | ||
|
||
Node newNode = new Node(); | ||
newNode.data = o; | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
for (int i = 0; i < index - 2; i++) { | ||
|
||
current = current.next; | ||
} | ||
|
||
newNode.next = current.next; | ||
current.next = newNode; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
|
||
|
||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} else if (index == 0) { | ||
return head; | ||
} | ||
for (int i = 0;i<index - 1; i++) { | ||
current = current.next; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
public Object remove(int index){ | ||
for (int i = 1; i <= index; i++) { | ||
|
||
if (i == index - 1) { | ||
current.next = current.next.next; | ||
size--; | ||
} else { | ||
current = current.next; | ||
} | ||
|
||
} | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node newHead = new Node(); | ||
newHead.data = o; | ||
newHead.next = head; | ||
head = newHead; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
while (current.next != null){ | ||
current = current.next; | ||
} | ||
current.next = newNode; | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
Node removeHead = head; | ||
head.next = head.next.next; | ||
size--; | ||
return removeHead; | ||
} | ||
public Object removeLast(){ | ||
Node theNext = current.next; | ||
Node oldLast; | ||
while(theNext.next != null) { | ||
current = theNext; | ||
theNext = theNext.next; | ||
} | ||
oldLast=current.next; | ||
current.next = theNext.next; | ||
|
||
size--; | ||
return oldLast; | ||
} | ||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
public Object head() { | ||
return head; | ||
} | ||
private class LinkedListIterator implements Iterator { | ||
@Override | ||
public boolean hasNext() { | ||
return current.next!=null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Node data = (Node) current.data; | ||
current.next = current.next.next; | ||
return data; | ||
} | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
|
||
Node next; | ||
|
||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group05/183127807/HomeWork0226/src/com/coding/basic/List.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,9 @@ | ||
package com.coding.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(); | ||
} |
22 changes: 22 additions & 0 deletions
22
group05/183127807/HomeWork0226/src/com/coding/basic/Queue.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,22 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
private LinkedList linkedList = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
linkedList.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
Object removeQueue = linkedList.removeFirst(); | ||
return removeQueue; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return linkedList.head()==null; | ||
} | ||
|
||
public int size(){ | ||
return linkedList.size(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
group05/183127807/HomeWork0226/src/com/coding/basic/Stack.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,35 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.EmptyStackException; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
int size = elementData.size(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
size++; | ||
} | ||
|
||
public Object pop(){ | ||
if(this.isEmpty()==true) | ||
{ | ||
throw new EmptyStackException(); | ||
} | ||
ArrayList popData = (ArrayList) elementData.remove(elementData.size()-1); | ||
size--; | ||
|
||
return popData; | ||
|
||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size()-1); | ||
} | ||
public boolean isEmpty(){ | ||
return elementData.size()==0; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
} |