-
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 #33 from MrGPanPan/master
Group 11 第一周作业提交
- Loading branch information
Showing
88 changed files
with
4,452 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,10 @@ | ||
apply plugin: 'java' | ||
apply plugin: 'eclipse' | ||
|
||
jar { | ||
manifest { | ||
attributes 'Main-Class' : 'com.coding.Main' | ||
} | ||
} | ||
|
||
|
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 @@ | ||
实现基本的数据结构ArrayList,LinkList,Stack,Queue,Tree,Iterator |
58 changes: 58 additions & 0 deletions
58
group11/1178243325/DataStructure/src/main/java/com/coding/Main.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,58 @@ | ||
package com.coding; | ||
|
||
import com.coding.basic.*; | ||
public class Main { | ||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
|
||
list.add(0, "2xxx"); | ||
list.add(1, "we"); | ||
list.add(2, "sss"); | ||
list.add("xing"); | ||
list.remove(2); | ||
System.out.println(list.get(2)); | ||
Iterator iterator = list.iterator(); | ||
while(iterator.hasNext()) { | ||
System.out.println(iterator.next()); | ||
} | ||
System.out.println(list.size()); | ||
|
||
LinkedList llist = new LinkedList(); | ||
llist.add("hu"); | ||
llist.add("zhao"); | ||
llist.add(2,"xing"); | ||
llist.addFirst("身骑白马"); | ||
llist.addLast("德州小老虎"); | ||
llist.add(5, "sf"); | ||
llist.remove(5); | ||
llist.removeFirst(); | ||
llist.removeLast(); | ||
for (int i = 2; i >=0; i--) | ||
System.out.print(llist.get(i)); | ||
System.out.println(llist.size()); | ||
|
||
Iterator literator = llist.iterator(); | ||
while(literator.hasNext()) { | ||
System.out.println(literator.next()); | ||
} | ||
|
||
Stack stack = new Stack(); | ||
stack.push(1); | ||
stack.push(2); | ||
stack.push(3); | ||
stack.push(4); | ||
System.out.println(stack.peek()); | ||
while(!stack.isEmpty()) | ||
System.out.println(stack.pop()); | ||
|
||
Queue queue = new Queue(); | ||
queue.enQueue(1); | ||
queue.enQueue(2); | ||
queue.enQueue(3); | ||
System.out.println(queue.size()); | ||
while (!queue.isEmpty()) { | ||
System.out.println(queue.deQueue()); | ||
} | ||
|
||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
group11/1178243325/DataStructure/src/main/java/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,97 @@ | ||
package com.coding.basic; | ||
|
||
import com.coding.basic.exception.*; | ||
public class ArrayList implements List { | ||
|
||
private int size; | ||
private Object[] elementData; | ||
|
||
public ArrayList () { | ||
size = 0; | ||
elementData = new Object[100]; | ||
} | ||
|
||
public void add(Object o){ | ||
add(size(), o); | ||
} | ||
|
||
public void add(int index, Object o){ | ||
if (size() == elementData.length) | ||
ensureCapacity( size() * 2 + 1); | ||
if (index > size() || index < 0) { //index == size时相当于在尾后插入 | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
for (int i = size; i > index; i--) { | ||
elementData[i] = elementData[i-1]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
|
||
} | ||
|
||
private void ensureCapacity(int newCapacity) { | ||
if (newCapacity < size()) | ||
return; | ||
Object[] old = elementData; | ||
elementData = new Object[newCapacity]; | ||
for (int i = 0; i < size(); i++) { | ||
elementData[i] = old[i]; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= size() || index < 0) { //获取时,index==size()越界 | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "Index:" + index + ", Size:" + size; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index >= size() || index < 0) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
Object old = elementData[index]; | ||
for (int i = index; i < size(); i++) { | ||
elementData[i] = elementData[i+1]; | ||
} | ||
size--; | ||
return old; | ||
} | ||
|
||
/*获取表内容量*/ | ||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
public class ArrayListIterator implements Iterator { | ||
private final int ONLY_CAPACITY = size; | ||
private int index; | ||
public ArrayListIterator() { | ||
index = 0; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (ONLY_CAPACITY != size) | ||
throw new ConcurrentModificationException("此对象没有进行修改同步"); | ||
return index != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (ONLY_CAPACITY != size) | ||
throw new ConcurrentModificationException("此对象没有进行修改同步"); | ||
if (index >= ONLY_CAPACITY) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
return elementData[index++]; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group11/1178243325/DataStructure/src/main/java/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/1178243325/DataStructure/src/main/java/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(); | ||
} |
135 changes: 135 additions & 0 deletions
135
group11/1178243325/DataStructure/src/main/java/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,135 @@ | ||
package com.coding.basic; | ||
|
||
import com.coding.basic.exception.*; | ||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
public LinkedList() { | ||
head = new Node(null, null); | ||
size = 0; | ||
} | ||
|
||
public void add(Object o){ | ||
add(size, o); | ||
} | ||
|
||
public void add(int index , Object o){ | ||
if (index > size || index < 0) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
Node frontNode = getNode(index-1); | ||
Node newNode = new Node(o, frontNode.next); | ||
frontNode.next = newNode; | ||
size++; | ||
|
||
} | ||
|
||
private Node getNode(int index) { | ||
Node node = head; | ||
int i = 0; | ||
while(node.next != null && i <= index) { | ||
node = node.next; | ||
i++; | ||
} | ||
return node; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
Node node = getNode(index); | ||
return node.data; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
Node frontNode = getNode(index-1); | ||
Node oldNode = getNode(index); | ||
frontNode.next = oldNode.next; | ||
size--; | ||
return oldNode.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); | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(); | ||
} | ||
|
||
private class LinkedListIterator implements Iterator { | ||
int index; | ||
final int capacity = size; | ||
LinkedListIterator() { | ||
index = 0; | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
if (capacity != size) | ||
throw new ConcurrentModificationException("此对象没有修改同步"); | ||
return index < capacity; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (capacity != size) | ||
throw new ConcurrentModificationException("此对象没有修改同步"); | ||
if (index >= capacity) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
return get(index++); | ||
} | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "index:" + index + ", size:" + size; | ||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
|
||
Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
|
||
void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
Object getData() { | ||
return data; | ||
} | ||
|
||
void setNext(Node next) { | ||
this.next = next; | ||
} | ||
|
||
Object getNext() { | ||
return next; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group11/1178243325/DataStructure/src/main/java/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(); | ||
} |
30 changes: 30 additions & 0 deletions
30
group11/1178243325/DataStructure/src/main/java/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,30 @@ | ||
package com.coding.basic; | ||
import com.coding.basic.exception.*; | ||
public class Queue { | ||
|
||
private LinkedList elementData; | ||
|
||
public Queue() { | ||
elementData = new LinkedList(); | ||
} | ||
|
||
public void enQueue(Object o){ | ||
elementData.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
if (isEmpty()) { | ||
throw new EmptyQueueException("队空"); | ||
} | ||
Object result = elementData.removeFirst(); | ||
return result; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |
Oops, something went wrong.