-
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 #7 from Jiandan1357/master
Master
- Loading branch information
Showing
7 changed files
with
326 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,58 @@ | ||
package com.bjsxd.test; | ||
|
||
public class ArrayList implements List { | ||
private Object[] elementData = new Object[100]; | ||
private int size = 0; | ||
Object[] temp = null; | ||
|
||
public void add(Object o) { | ||
if (size < elementData.length) { | ||
size++; | ||
Object[] target = new Object[elementData.length + size]; | ||
System.arraycopy(elementData, 0, target, 0, elementData.length); | ||
elementData[size] = o; | ||
} | ||
} | ||
|
||
public void add(int index, Object o) { | ||
if (index < 0 || o == null) { | ||
throw new IllegalArgumentException("��Ӷ������"); | ||
} else if (index <= elementData.length) { | ||
add(o); | ||
} else if (index > elementData.length) { | ||
throw new IllegalArgumentException("��Ӷ���Խ��"); | ||
} | ||
|
||
if (size <= elementData.length) { | ||
this.size++; | ||
} | ||
Object[] target = new Object[this.size]; | ||
System.arraycopy(elementData, 0, target, 0, index); | ||
target[index] = o; | ||
System.arraycopy(elementData, index, target, index + 1, elementData.length - index); | ||
} | ||
|
||
public Object get(int index) { | ||
if (index < 0 || index >= elementData.length) { | ||
return false; | ||
} else { | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index >= elementData.length) { | ||
throw new IllegalArgumentException("ɾ��������"); | ||
} else { | ||
for (int i = index; i < elementData.length; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
} |
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,50 @@ | ||
package com.bjsxd.test; | ||
|
||
public class BinaryTreeNode { | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private BinaryTreeNode root; | ||
|
||
public BinaryTreeNode(BinaryTreeNode root){ | ||
this.root = root; | ||
|
||
} | ||
|
||
public BinaryTreeNode(BinaryTreeNode left,BinaryTreeNode right,Object data){ | ||
this.left = left; | ||
this.right = right; | ||
this.data = data; | ||
} | ||
|
||
public void buildTree(){ | ||
|
||
} | ||
|
||
public BinaryTreeNode(Object data){ | ||
this(null,null,data); | ||
} | ||
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; | ||
} | ||
|
||
} |
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(); | ||
|
||
} |
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,117 @@ | ||
package com.bjsxd.test; | ||
|
||
public class LinkedList implements List{ | ||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
private Node head; | ||
private Node last; | ||
public void add (Object o){ | ||
if (head == null){ | ||
head = new Node(); | ||
head.data = o; | ||
head.next = null; | ||
}else{ | ||
Node MyNode = new Node(); | ||
MyNode = head; | ||
while (MyNode.next != null){ | ||
MyNode = MyNode.next; | ||
} | ||
Node AddNode = new Node(); | ||
MyNode.next = AddNode; | ||
AddNode.data = o; | ||
} | ||
} | ||
public void add(int index,Object o){ | ||
if(index<0 || o ==null){ | ||
throw new IllegalArgumentException("添加对象位置出错且不能为空"); | ||
}else if (index == 0 && head == null){ | ||
head = new Node(); | ||
head.data = o; | ||
head.next = null; | ||
}else if (index > 0 && head == null){ | ||
throw new IllegalArgumentException("添加对象位置出错"); | ||
}else{ | ||
Node SrcNode = new Node(); | ||
Node AddNode = new Node(); | ||
Node SrcNode2 = new Node(); | ||
SrcNode = head; | ||
for(int i=0;i<=index;i++){ | ||
SrcNode = SrcNode.next; | ||
} | ||
AddNode.next = SrcNode; | ||
AddNode.data = o; | ||
for (int i=0;i<index;i++){ | ||
SrcNode2 = SrcNode2.next; | ||
} | ||
SrcNode2.next = AddNode; | ||
} | ||
} | ||
public Object get(int index){ | ||
Node SrcNode = new Node(); | ||
for (int i=0;i<index;i++){ | ||
SrcNode = SrcNode.next; | ||
} | ||
return SrcNode; | ||
} | ||
public Object remove(int index){ | ||
Node SrcNode = new Node(); | ||
Node TempNode = new Node(); | ||
Node MyNode = new Node(); | ||
MyNode = head; | ||
int size = 0; | ||
while (MyNode != null){ | ||
MyNode = MyNode.next; | ||
size++; | ||
} | ||
if (index < 0 || index > size){ | ||
throw new IllegalArgumentException("删除对象位置出错"); | ||
}else{ | ||
for (int i=0;i<index;i++){ | ||
SrcNode = SrcNode.next; | ||
} | ||
TempNode = SrcNode.next; | ||
SrcNode.next = SrcNode.next.next; | ||
return TempNode; | ||
} | ||
} | ||
public int size(){ | ||
Node MyNode = new Node(); | ||
MyNode = head; | ||
int size = 0; | ||
while (MyNode != null){ | ||
MyNode = MyNode.next; | ||
size++; | ||
} | ||
return size; | ||
} | ||
public void addFirst(Object o){ | ||
Node MyNode = new Node(); | ||
MyNode.data = o; | ||
MyNode.next = head; | ||
head.next = MyNode; | ||
} | ||
public Object removeFirst(){ | ||
Node MyNode = new Node(); | ||
MyNode.data = head.data; | ||
head.next = head.next.next; | ||
return MyNode; | ||
} | ||
public Object removeLast(){ | ||
Node TempNode = new Node(); | ||
Node MyNode = new Node(); | ||
MyNode = last; | ||
TempNode = head; | ||
int size = 0; | ||
while (TempNode != null){ | ||
TempNode = TempNode.next; | ||
size++; | ||
} | ||
for (int i = 0;i < size-1; i++){ | ||
TempNode = TempNode.next; | ||
} | ||
TempNode.next = null; | ||
return MyNode; | ||
} | ||
} |
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 @@ | ||
package com.bjsxd.test; | ||
|
||
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(); | ||
|
||
} |
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,45 @@ | ||
package com.bjsxd.test; | ||
|
||
public class Queue{ | ||
private Object[] Q; | ||
int front; | ||
int rear; | ||
public Queue(){ | ||
Q = new Object[100]; | ||
} | ||
public Queue(int size){ | ||
Q = new Object[size]; | ||
front = 0; | ||
rear = 0; | ||
} | ||
public void enQueue(Object o){ | ||
if((rear+1)%Q.length == front){ | ||
System.out.println("Õ»Âú£¡"); | ||
}else{ | ||
Q[rear] = o; | ||
rear = rear+1; | ||
} | ||
} | ||
|
||
public Object deQueue(){ | ||
if (rear == front) | ||
return null; | ||
else{ | ||
Object temp = Q[front]; | ||
front = front+1; | ||
return temp; | ||
} | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return rear == front; | ||
} | ||
|
||
public int size(){ | ||
if(rear > front){ | ||
return rear - front; | ||
}else | ||
return Q.length-1; | ||
} | ||
|
||
} |
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,39 @@ | ||
package com.bjsxd.test; | ||
|
||
public class Stack { | ||
private int top = -1; | ||
private Object[] elements; | ||
private int size = 0; | ||
public Stack(){ | ||
elements = new Object[100]; | ||
} | ||
public void push (Object o){ | ||
elements[this.size] = o; | ||
this.size++; | ||
} | ||
public Object pop(){ | ||
if (this.size != 0){ | ||
Object temp = elements[size-1]; | ||
elements[size-1]=0; | ||
size--; | ||
return temp; | ||
}else{ | ||
System.out.println("Õ»¿Õ"); | ||
return 0; | ||
} | ||
} | ||
public Object peek(){ | ||
if(!this.isEmpty()){ | ||
Object temp = elements[this.size-1]; | ||
elements[this.size-1] = 0; | ||
this.size--; | ||
return temp; | ||
}else{ | ||
System.out.println("Õ»¿Õ"); | ||
return 0; | ||
} | ||
} | ||
public boolean isEmpty(){ | ||
return this.size == 0; | ||
} | ||
} |