forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #8 from XeCtvi/master
DataStructure
- Loading branch information
Showing
10 changed files
with
380 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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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 @@ | ||
/bin/ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>dataStructure</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
74 changes: 74 additions & 0 deletions
74
group11/635189253/dataStructure/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,74 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public boolean add(Object o){ | ||
add(size(), o); | ||
size++; | ||
return true; | ||
} | ||
public boolean add(int index, Object o){ | ||
if (index >= 0 && index < elementData.length) { | ||
for (int i = size(); i > index; i--) { | ||
elementData[i] = elementData[i-1]; | ||
} | ||
elementData[index] = o; | ||
} | ||
size++; | ||
return true; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= 0 && index < size()) { | ||
return elementData[index]; | ||
} | ||
// return null; | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
|
||
public Object remove(int index){ | ||
Object removedItem = elementData[index]; | ||
if (index > size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
for (int i = index; i < size() - 1; i++) { | ||
elementData[i] = elementData[i+1]; | ||
} | ||
size--; | ||
return removedItem; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator { | ||
|
||
private int current = 0; | ||
|
||
public boolean hasNext() { | ||
return current < size(); | ||
} | ||
|
||
public Object next() { | ||
/* if (elementData[current+1] != null) { | ||
return elementData[current+1]; | ||
} else { | ||
return null; | ||
}*/ | ||
if (!hasNext()) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
return elementData[current++]; | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group11/635189253/dataStructure/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; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
group11/635189253/dataStructure/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,6 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
191 changes: 191 additions & 0 deletions
191
group11/635189253/dataStructure/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,191 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private int size; | ||
private int modCount = 0; | ||
private Node beginMarker; | ||
private Node endMarker; | ||
|
||
private static class Node{ | ||
public Node( Object d, Node p, Node n) { | ||
data = d; | ||
prev = p; | ||
next = n; | ||
} | ||
public Object data; | ||
public Node prev; | ||
public Node next; | ||
} | ||
|
||
public LinkedList () { | ||
doClear(); | ||
} | ||
|
||
public void clear() { | ||
doClear(); | ||
} | ||
|
||
private void doClear() { | ||
beginMarker = new Node(null, null, null); | ||
endMarker = new Node(null, beginMarker, null); | ||
beginMarker.next = endMarker; | ||
|
||
size = 0; | ||
modCount++; | ||
} | ||
|
||
public boolean add(Object o){ | ||
add(size(), o); | ||
return true; | ||
} | ||
public boolean add(int index , Object o){ | ||
/*if (index < 0 && index > size()) { | ||
throw new IndexOutOfBoundsException(); | ||
}*/ | ||
addBefore( getNode( index, 0, size()), o); | ||
return true; | ||
} | ||
|
||
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()); | ||
} | ||
|
||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
public Object remove(int index){ | ||
return remove(getNode(index)); | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size() == 0; | ||
} | ||
|
||
private void addBefore( Node p, Object o ) { | ||
Node newNode = new Node(o, p.prev, p ); | ||
newNode.prev.next = newNode; | ||
p.prev = newNode; | ||
// p.prev = p.prev.next = new Node(o, p.prev, p); | ||
size++; | ||
modCount++; | ||
} | ||
|
||
private Node getNode(int idx) { | ||
return getNode(idx, 0, size() - 1); | ||
} | ||
|
||
private Node getNode(int idx, int lower, int upper) { | ||
Node p; | ||
if (idx < lower || idx > upper) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
if (idx < size() / 2) { | ||
p = beginMarker.next; | ||
for (int i = 0; i < idx; i++) { | ||
p = p.next; | ||
} | ||
} else { | ||
p = endMarker.prev; | ||
for (int i = size(); i > idx; i--) { | ||
p = p.prev; | ||
} | ||
} | ||
return p; | ||
} | ||
|
||
private Object remove( Node p ) { | ||
p.prev.next = p.next; | ||
p.next.prev = p.prev; | ||
modCount++; | ||
size--; | ||
|
||
return p.data; | ||
} | ||
|
||
|
||
public java.util.Iterator iterator() { | ||
return new LinkListIterator(); | ||
} | ||
|
||
private class LinkListIterator implements java.util.Iterator { | ||
|
||
private Node current = beginMarker.next; | ||
private int expectedModCount = modCount; | ||
private boolean okToRemove = false; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return current != endMarker; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if ( modCount != expectedModCount ) { | ||
throw new java.util.ConcurrentModificationException(); | ||
} | ||
if (!hasNext()) { | ||
throw new java.util.NoSuchElementException(); | ||
} | ||
Object nextItem = current.next; | ||
current = current.next; | ||
okToRemove = true; | ||
return nextItem; | ||
} | ||
|
||
public void remove() { | ||
if ( modCount != expectedModCount ) { | ||
throw new java.util.ConcurrentModificationException(); | ||
} | ||
if ( !okToRemove ) { | ||
throw new IllegalStateException(); | ||
} | ||
LinkedList.this.remove(current.prev); | ||
expectedModCount++; | ||
okToRemove = false; | ||
|
||
} | ||
} | ||
} | ||
|
||
class TestLinkLedList { | ||
|
||
public static void main(String[] args) { | ||
LinkedList lst = new LinkedList(); | ||
for (int i = 0; i < 10; i++) { | ||
lst.add(i); | ||
} | ||
for (int i = 20; i < 30; i++) { | ||
lst.add(0, i); | ||
} | ||
|
||
lst.remove(0); | ||
lst.remove(lst.size()-1); | ||
|
||
System.out.println(lst); | ||
|
||
java.util.Iterator itr = lst.iterator(); | ||
while (itr.hasNext()) { | ||
itr.next(); | ||
itr.remove(); | ||
System.out.println(lst); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group11/635189253/dataStructure/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 boolean add(Object o); | ||
public boolean 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
group11/635189253/dataStructure/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 elementData; | ||
|
||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return null; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return -1; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
group11/635189253/dataStructure/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,22 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
} | ||
|
||
public Object pop(){ | ||
return null; | ||
} | ||
|
||
public Object peek(){ | ||
return null; | ||
} | ||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
public int size(){ | ||
return -1; | ||
} | ||
} |