forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4a25d6
commit 0b24942
Showing
7 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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,62 @@ | ||
package com.github.hwjcc969.coding2017.basic; | ||
|
||
import javax.lang.model.element.Element; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
if (o == null){ | ||
throw new NullPointerException("空对象"); | ||
} | ||
if (size >= 100){ | ||
throw new RuntimeException("越界"); | ||
} | ||
elementData[size] = o; | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
if (o == null){ | ||
throw new NullPointerException("空对象"); | ||
} | ||
if (index >= 99){ | ||
throw new RuntimeException("越界"); | ||
} | ||
for (int i = size; i >= index; i--) { | ||
elementData[i + 1] = elementData[i]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
if (index >= 100 || index < 0){ | ||
throw new RuntimeException("越界"); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index >= 100 || index < 0){ | ||
throw new RuntimeException("越界"); | ||
} | ||
Object o = elementData[index]; | ||
for (int i = index; i < size - 1; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
size--; | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...17/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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.github.hwjcc969.coding2017.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
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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.github.hwjcc969.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
...ng2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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,117 @@ | ||
package com.github.hwjcc969.coding2017.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
public void add(Object o){ | ||
Node newNode = new Node(o); | ||
if (head == null) { | ||
head = newNode; | ||
} | ||
else{ | ||
Node last = head.next; | ||
while (last != null){ | ||
last = last.next; | ||
} | ||
last = newNode; | ||
} | ||
size++; | ||
|
||
} | ||
public void add(int index , Object o){ | ||
Node dummy = new Node(); | ||
dummy.next = head; | ||
Node pre = dummy; | ||
while (index > 0) { | ||
pre = pre.next; | ||
} | ||
Node cur = new Node(o); | ||
cur.next = pre.next; | ||
pre.next = cur; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
Node cur = head; | ||
while (index > 0 && cur != null) { | ||
cur = cur.next; | ||
} | ||
return cur; | ||
} | ||
public Object remove(int index){ | ||
Node dummy = new Node(); | ||
dummy.next = head; | ||
Node pre = dummy; | ||
while (index > 0) { | ||
pre = pre.next; | ||
} | ||
Node cur = pre.next; | ||
Node next = pre.next.next; | ||
pre.next = next; | ||
size--; | ||
return cur; | ||
|
||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
Node firstNode = new Node(o); | ||
firstNode.next = head; | ||
head = firstNode; | ||
} | ||
public void addLast(Object o){ | ||
Node pre = new Node(); | ||
while (pre.next != null){ | ||
pre = pre.next; | ||
} | ||
Node lastNode = new Node(o); | ||
pre.next = lastNode; | ||
} | ||
public Object removeFirst(){ | ||
Node dummy = new Node(); | ||
dummy.next = head; | ||
dummy.next = head.next; | ||
return dummy.next; | ||
} | ||
public Object removeLast(){ | ||
Node pre = new Node(); | ||
while (pre.next.next != null) { | ||
pre = pre.next; | ||
} | ||
Node lastNode = pre.next.next; | ||
pre.next = null; | ||
return lastNode; | ||
|
||
} | ||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
public Node(Object o) { | ||
// TODO Auto-generated constructor stub | ||
} | ||
public Node() { | ||
// TODO Auto-generated constructor stub | ||
} | ||
Object data; | ||
Node next; | ||
|
||
} | ||
|
||
|
||
public boolean isEmpty() { | ||
// TODO Auto-generated method stub | ||
if (size() == 0){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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.github.hwjcc969.coding2017.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(); | ||
} |
27 changes: 27 additions & 0 deletions
27
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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,27 @@ | ||
package com.github.hwjcc969.coding2017.basic; | ||
|
||
public class Queue { | ||
private LinkedList list = new LinkedList(); | ||
public void enQueue(Object o){ | ||
list.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
if (!list.isEmpty()){ | ||
return list.removeFirst(); | ||
} | ||
return "空队列"; | ||
|
||
} | ||
|
||
public boolean isEmpty(){ | ||
if (list.size() == 0){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public int size(){ | ||
return list.size(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
coding2017/group02/447809161Learning/src/com/github/hwjcc969/coding2017/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,31 @@ | ||
package com.github.hwjcc969.coding2017.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
private int size = 0; | ||
|
||
public void push(Object o){ | ||
if (elementData.size() <= 99) | ||
elementData.add(o); | ||
size++; | ||
|
||
} | ||
|
||
public Object pop(){ | ||
size--; | ||
return elementData.remove(elementData.size() - 1); | ||
|
||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size() - 1); | ||
} | ||
public boolean isEmpty(){ | ||
if (size == 0) | ||
return false; | ||
return true; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
} |