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.
Merge pull request #15 from weifei619/master
github
- Loading branch information
Showing
7 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
group20/1430208241/1430208241leaning/src/GithubWork/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,73 @@ | ||
package GithubWork; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
private int size = 0; | ||
private Object[] elementdata = new Object[100]; | ||
|
||
public void add(Object o) { | ||
if (elementdata.length <= size) { | ||
ensureCapacity(size + 1); | ||
} | ||
elementdata[size++] = o; | ||
} | ||
|
||
private void ensureCapacity(int minCapacity) { | ||
int oldCapacity = elementdata.length; | ||
if (oldCapacity < minCapacity) { | ||
|
||
int newCapacity = (int) (oldCapacity * 1.5); | ||
if (newCapacity < minCapacity) | ||
newCapacity = minCapacity; | ||
elementdata = Arrays.copyOf(elementdata, newCapacity); | ||
} | ||
} | ||
|
||
public void add(int index, Object o) { | ||
if (index > size || index < 0) { | ||
throw new IndexOutOfBoundsException(); | ||
|
||
} | ||
|
||
ensureCapacity(size + 1); | ||
System.arraycopy(elementdata, index, elementdata, index, size - index); | ||
elementdata[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
RangeCheck(index); | ||
|
||
return elementdata[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
RangeCheck(index); | ||
Object oldvalue = elementdata[index]; | ||
int numMoved = size - index - 1; | ||
if (numMoved > 0) | ||
System.arraycopy(elementdata, index + 1, elementdata, index, numMoved); | ||
elementdata[--size] = null; | ||
return oldvalue; | ||
} | ||
|
||
private void RangeCheck(int index) { | ||
if (index >= size) | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
public int size() { | ||
int i; | ||
for (i = 0; i < elementdata.length; i++) { | ||
size++; | ||
if (null == elementdata[i]) { | ||
break; | ||
} | ||
|
||
} | ||
return size; | ||
|
||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.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,47 @@ | ||
package GithubWork; | ||
|
||
import org.junit.Test; | ||
|
||
public class JunitTest { | ||
@Test | ||
public void ArrayList(){ | ||
ArrayList a=new ArrayList(); | ||
a.add(1); | ||
a.add(2); | ||
a.add(3); | ||
a.add(4); | ||
a.get(2); | ||
|
||
System.out.println(a); | ||
|
||
|
||
} | ||
@Test | ||
public void Queue(){ | ||
Queue q=new Queue(4); | ||
q.enQueue(1); | ||
q.enQueue(2); | ||
q.enQueue(3); | ||
q.enQueue(4); | ||
|
||
while(!q.isEmpty()){ | ||
int i=(int) q.deQueue(); | ||
System.out.println(i); | ||
} | ||
System.out.println(q.size()); | ||
} | ||
@Test | ||
public void LinkedList(){ | ||
LinkedList ls=new LinkedList(); | ||
ls.add(1); | ||
ls.add(7); | ||
ls.add(3); | ||
ls.add(4, 5); | ||
ls.get(2); | ||
ls.addFirst(0); | ||
|
||
ls.remove(3); | ||
System.out.println(ls); | ||
|
||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
group20/1430208241/1430208241leaning/src/GithubWork/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,136 @@ | ||
package GithubWork; | ||
|
||
import java.util.Iterator; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head = null;// 头节点 | ||
private int size = 0; | ||
private Node last = null; | ||
|
||
/* | ||
* 向链表中插入数据 (non-Javadoc) | ||
* | ||
* @see GithubWork.List#add(java.lang.Object) | ||
*/ | ||
public void add(Object o) { | ||
Node newNode = new Node(0);// 实例化一个节点 | ||
if (head == null) { | ||
head = newNode; | ||
return; | ||
} | ||
Node tmp = head; | ||
while (tmp.next != null) { | ||
tmp = tmp.next; | ||
} | ||
tmp.next = newNode; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
Node newNode = new Node(0); | ||
Node indexNode = head; | ||
int i = 0; | ||
while (i == index) { | ||
indexNode = indexNode.next; | ||
i++; | ||
} | ||
Node indexNextNode = indexNode.next; | ||
indexNode.next = newNode; | ||
newNode.pre = indexNode; | ||
newNode.next = indexNextNode; | ||
indexNextNode.pre = newNode; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
Node indexNode = head; | ||
int i = 0; | ||
while (i == index) { | ||
indexNode = indexNode.next; | ||
i++; | ||
} | ||
return indexNode; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 1 || index > size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
if (index == 1) { | ||
head = head.next; | ||
return head; | ||
} | ||
int i = 1; | ||
Node preNode = head; | ||
Node curNode = preNode.next; | ||
while (curNode != null) { | ||
if (i == index) { | ||
preNode.next = curNode.next; | ||
|
||
} | ||
preNode = curNode; | ||
curNode = curNode.next; | ||
i++; | ||
} | ||
return curNode; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node newNode = new Node(o); | ||
newNode.data = o; | ||
newNode.next = head; | ||
head.pre = newNode; | ||
head = newNode; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node newNode = new Node(o); | ||
newNode.data = o; | ||
|
||
newNode.pre = last; | ||
last.next = newNode; | ||
last = newNode; | ||
size++; | ||
|
||
} | ||
|
||
public Object removeFirst() { | ||
Node ref = head; | ||
head = head.next; | ||
head.pre = null; | ||
size--; | ||
return ref; | ||
} | ||
|
||
public Object removeLast() { | ||
Node rel = last; | ||
last = last.pre; | ||
last.next = null; | ||
size--; | ||
return rel; | ||
|
||
} | ||
|
||
public Iterator iterator() { | ||
|
||
return null; | ||
} | ||
|
||
private static class Node { | ||
Object data;// 节点内容 | ||
Node next = null;// 头节点 | ||
Node pre = null; | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
|
||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group20/1430208241/1430208241leaning/src/GithubWork/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,10 @@ | ||
package GithubWork; | ||
|
||
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(); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
group20/1430208241/1430208241leaning/src/GithubWork/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,43 @@ | ||
package GithubWork; | ||
|
||
public class Queue { | ||
private int maxSize; | ||
private Object[] array;//存放元素数组 | ||
private int front;//前一个元素索引 | ||
private int rear;//后一个元素索引 | ||
private int items=0;//元素个数 | ||
//构造对象并初始化 | ||
public Queue(int s){ | ||
maxSize=s; | ||
array=new Object[maxSize]; | ||
front=0; | ||
rear=-1; | ||
|
||
} | ||
public void enQueue(Object o){ | ||
if(rear==maxSize-1){ | ||
rear=-1; | ||
} | ||
array[++rear]=o; | ||
items++; | ||
|
||
} | ||
|
||
public Object deQueue(){ | ||
Object temp =array[front++]; | ||
if(front==maxSize){ | ||
front=0; | ||
} | ||
items--; | ||
return temp; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
|
||
return items==0; | ||
} | ||
|
||
public int size(){ | ||
return array.length; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
group20/1430208241/1430208241leaning/src/GithubWork/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,22 @@ | ||
package GithubWork; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
} | ||
|
||
public Object pop(){ | ||
return null; | ||
} | ||
|
||
public Object peek(){ | ||
return null; | ||
} | ||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
public int size(){ | ||
return -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 @@ | ||
http://blog.csdn.net/wifi619/article/details/57510982 |