forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
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 vvvandbb/master
add code 2-26
- Loading branch information
Showing
7 changed files
with
336 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,2 @@ | ||
文章地址 | ||
http://note.youdao.com/noteshare?id=a0345eff655b1cfc5877cc267452eed0&sub=FE5CD301B3C742269D04973B0DD2393C |
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,68 @@ | ||
package com.vvv.base; | ||
|
||
public class ArrayList implements IList { | ||
|
||
private static final int DEFAULT_SIZE = 100; | ||
|
||
private int size; | ||
private Object[] data = new Object[DEFAULT_SIZE]; | ||
|
||
@Override | ||
public void add(Object o) { | ||
if (size > data.length) { | ||
increase(); | ||
} | ||
data[size] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
check(index); | ||
|
||
if (index > data.length - 1) { | ||
increase(); | ||
} | ||
System.arraycopy(data, index, data, index + 1, size - index); | ||
data[index] = o; | ||
size++; | ||
} | ||
|
||
private void check(int index) { | ||
if (index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("index: " + index + ", size: " | ||
+ size); | ||
} | ||
|
||
private void increase() { | ||
Object[] newData = new Object[DEFAULT_SIZE * 2]; | ||
System.arraycopy(data, 0, newData, 0, size); | ||
data = newData; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
check(index); | ||
return data[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
if (index > 0 && index < size) { | ||
Object d = data[index]; | ||
int diff = size - index - 1; | ||
if (diff > 0) | ||
System.arraycopy(data, index + 1, data, index, size - index - 1); | ||
size--; | ||
data[size] = null; | ||
return d; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
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,37 @@ | ||
package com.vvv.base; | ||
|
||
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; | ||
} | ||
|
||
} |
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.vvv.base; | ||
|
||
public interface IList { | ||
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,117 @@ | ||
package com.vvv.base; | ||
|
||
public class LinkedList implements IList { | ||
private int size; | ||
private Node<Object> head; | ||
private Node<Object> tail; | ||
|
||
@Override | ||
public void add(Object o) { | ||
Node<Object> node = new Node<Object>(o); | ||
if (head == null) { | ||
head = node; | ||
tail = node; | ||
} else { | ||
tail.next = node; | ||
node.prev = tail; | ||
tail = node; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
check(index); | ||
Node<Object> node = new Node<Object>(o); | ||
Node<Object> temp = head; | ||
for (int i = 0; i < index; i++) { | ||
temp = temp.next; | ||
} | ||
if (temp.prev != null && temp.next != null) { | ||
Node<Object> prevNode = temp.prev; | ||
prevNode.next = node; | ||
node.prev = prevNode; | ||
node.next = temp; | ||
temp.prev = node; | ||
} else if (temp.prev == null) { | ||
head = node; | ||
temp.prev = node; | ||
node.next = temp; | ||
} else if (temp.next == null) { | ||
temp.next = node; | ||
node.prev = temp; | ||
tail = node; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
check(index); | ||
Node<Object> temp = head; | ||
for (int i = 0; i < index; i++) { | ||
temp = temp.next; | ||
} | ||
return temp.data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
check(index); | ||
Node<Object> temp = head; | ||
for (int i = 0; i < index; i++) { | ||
temp = temp.next; | ||
} | ||
Object oldValue = temp.data; | ||
if (temp.prev != null && temp.next != null) { | ||
temp.prev.next = temp.next; | ||
temp.next.prev = temp.prev; | ||
temp = null; | ||
} else if (temp.prev == null) { | ||
head = temp.next; | ||
temp.next.prev = null; | ||
temp = null; | ||
} else if (temp.next == null) { | ||
tail = temp.prev; | ||
temp.prev.next = null; | ||
temp = null; | ||
} | ||
size--; | ||
return oldValue; | ||
} | ||
|
||
public boolean remove(Object obj) { | ||
Node<Object> temp = head; | ||
for (int i = 0; i < size; i++) { | ||
if (obj.equals(temp.data)) { | ||
remove(i); | ||
return true; | ||
} | ||
temp = temp.next; | ||
} | ||
return false; | ||
} | ||
|
||
private void check(int index) { | ||
if (index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("index: " + index + ", size: " | ||
+ size); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@SuppressWarnings("hiding") | ||
private static class Node<Object> { | ||
Object data; | ||
Node<Object> next; | ||
Node<Object> prev; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
} |
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,73 @@ | ||
package com.vvv.base; | ||
|
||
public class Queue { | ||
|
||
QueueNode head; | ||
QueueNode tail; | ||
private int size; | ||
|
||
public Queue() { | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
|
||
public void enQueue(Object o) { | ||
if (head == null && tail == null) { | ||
tail = new QueueNode(o); | ||
head = tail; | ||
} else { | ||
QueueNode node = new QueueNode(o); | ||
tail.next = node; | ||
tail = tail.next; | ||
} | ||
size++; | ||
} | ||
|
||
public Object deQueue() { | ||
if (head == null) { | ||
return null; | ||
} | ||
|
||
if (head == tail && tail != null) { | ||
QueueNode node = head; | ||
tail = null; | ||
head = null; | ||
size--; | ||
return node.data; | ||
} | ||
|
||
Object obj = head.data; | ||
head = head.next; | ||
size--; | ||
return obj; | ||
} | ||
|
||
public boolean isEmpty() { | ||
if(head==null && tail == null) | ||
return true; | ||
return false; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
} | ||
|
||
class QueueNode { | ||
Object data; | ||
QueueNode next; | ||
|
||
public QueueNode() { | ||
this(null, null); | ||
} | ||
|
||
public QueueNode(Object data) { | ||
this(data, null); | ||
} | ||
|
||
public QueueNode(Object data, QueueNode next) { | ||
this.data = data; | ||
this.next = 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,30 @@ | ||
package com.vvv.base; | ||
|
||
public class Stack { | ||
private ArrayList data = new ArrayList(); | ||
|
||
public void push(Object o) { | ||
data.add(o); | ||
} | ||
|
||
public Object pop() { | ||
return data.remove(data.size() - 1); | ||
} | ||
|
||
public Object peek() { | ||
if (data.size() > 0) { | ||
return data.get(data.size() - 1); | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isEmpty() { | ||
if (data.size() > 0) | ||
return false; | ||
return true; | ||
} | ||
|
||
public int size() { | ||
return data.size(); | ||
} | ||
} |