-
Notifications
You must be signed in to change notification settings - Fork 641
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 #27 from skycobo/master
[菠萝C]:补交第一次作业!
- Loading branch information
Showing
16 changed files
with
750 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,5 @@ | ||
package main; | ||
|
||
public class Test { | ||
|
||
} |
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,5 @@ | ||
package main; | ||
|
||
public class Test { | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
group23/1323434545/ordinary_work/01_DataStructure/src/main/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,82 @@ | ||
package main; | ||
|
||
import java.util.Arrays; | ||
|
||
import utils.ListUtils; | ||
|
||
public class ArrayList implements List { | ||
private Object[] elementData = new Object[10]; | ||
private int size; | ||
|
||
@Override | ||
public void add(Object o) { | ||
add(size, o); | ||
|
||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
//检查是否越界 | ||
ListUtils.CheckIndexInRange(0, size, index); | ||
if (size == elementData.length) { | ||
elementData = Arrays.copyOf(elementData, elementData.length + 10); | ||
} | ||
if (index < size) { | ||
for (int i = size; i > index; i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
ListUtils.CheckIndexInRange(0, size - 1, index); | ||
Object tag = elementData[index]; | ||
for (int i = index; i < size - 1; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[size - 1] = null; | ||
size--; | ||
return tag; | ||
|
||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
ListUtils.CheckIndexInRange(0, size - 1, index); | ||
return elementData[index]; | ||
|
||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Iterator() { | ||
private int cursor; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (size > 0) { | ||
return cursor < size; | ||
} else { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object tag = get(cursor); | ||
cursor++; | ||
return tag; | ||
} | ||
}; | ||
|
||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
group23/1323434545/ordinary_work/01_DataStructure/src/main/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,65 @@ | ||
package main; | ||
|
||
public class BinaryTreeNode { | ||
@SuppressWarnings("rawtypes") | ||
private Comparable data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) { | ||
this.data = data; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public BinaryTreeNode insert(Comparable o) { | ||
int result = o.compareTo(data); | ||
if (0 == result) { | ||
return this; | ||
} else if (0 < result) { | ||
if (null == this.right) { | ||
BinaryTreeNode node = new BinaryTreeNode(o, null, null); | ||
this.right = node; | ||
return node; | ||
} | ||
return this.right.insert(o); | ||
} else { | ||
if (null == this.left) { | ||
BinaryTreeNode node = new BinaryTreeNode(o, null, null); | ||
this.left = node; | ||
return node; | ||
} | ||
return this.left.insert(o); | ||
} | ||
|
||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public Comparable getData() { | ||
return data; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public void setData(Comparable 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; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
group23/1323434545/ordinary_work/01_DataStructure/src/main/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 main; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
114 changes: 114 additions & 0 deletions
114
group23/1323434545/ordinary_work/01_DataStructure/src/main/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,114 @@ | ||
package main; | ||
|
||
import utils.ListUtils; | ||
|
||
public class LinkedList implements List { | ||
private Node head; | ||
private int size; | ||
|
||
private static class Node { | ||
private Object data; | ||
private Node next; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
add(size, o); | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
ListUtils.CheckIndexInRange(0, size, index); | ||
if (0==size) { | ||
head = new Node(); | ||
head.data = o; | ||
size++; | ||
return; | ||
} | ||
if (0 == index) { | ||
Node node = new Node(); | ||
node.data = o; | ||
node.next = head; | ||
head = node; | ||
size++; | ||
return; | ||
} | ||
if (index == size) { | ||
Node node = head; | ||
while (null != node.next) { | ||
node = node.next; | ||
} | ||
Node addNode = new Node(); | ||
addNode.data = o; | ||
node.next = addNode; | ||
size++; | ||
return; | ||
} | ||
Node node = head; | ||
for (int i = 0; i < index - 1; i++) { | ||
node = node.next; | ||
} | ||
Node addNode = new Node(); | ||
addNode.data = o; | ||
addNode.next = node.next; | ||
node.next = addNode; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
ListUtils.CheckIndexInRange(0, size - 1, index); | ||
Node node = head; | ||
if (0 == index) { | ||
head = head.next; | ||
size--; | ||
return node.data; | ||
} | ||
for (int i = 0; i < index - 1; i++) { | ||
node = node.next; | ||
} | ||
Node removeNode = node.next; | ||
node.next = removeNode.next; | ||
size--; | ||
return removeNode.data; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
ListUtils.CheckIndexInRange(0, size - 1, index); | ||
Node node = head; | ||
for (int i = 0; i < index; i++) { | ||
node = node.next; | ||
} | ||
return node.data; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Iterator() { | ||
private int cursor; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (size > 0) { | ||
return cursor < size; | ||
} else { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object tag = get(cursor); | ||
cursor++; | ||
return tag; | ||
} | ||
}; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
group23/1323434545/ordinary_work/01_DataStructure/src/main/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 main; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
public void add(int index,Object o); | ||
public Object remove(int index); | ||
public Object get(int index); | ||
public int size(); | ||
} |
22 changes: 22 additions & 0 deletions
22
group23/1323434545/ordinary_work/01_DataStructure/src/main/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 main; | ||
|
||
public class Queue { | ||
private LinkedList list = new LinkedList(); | ||
|
||
public void enQueue(Object o) { | ||
list.add(o); | ||
} | ||
|
||
public Object deQueue() { | ||
return list.remove(0); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size() == 0; | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
group23/1323434545/ordinary_work/01_DataStructure/src/main/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,25 @@ | ||
package main; | ||
|
||
public class Stack { | ||
private ArrayList list = new ArrayList(); | ||
|
||
public void push(Object o) { | ||
list.add(o); | ||
} | ||
|
||
public Object pop() { | ||
return list.remove(list.size() - 1); | ||
} | ||
|
||
public Object peek() { | ||
return list.get(list.size() - 1); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size() == 0; | ||
} | ||
|
||
public int size() { | ||
return list.size(); | ||
} | ||
} |
Oops, something went wrong.