forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #32 from xxxxxxttt817/master
The first week home work
- Loading branch information
Showing
12 changed files
with
566 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
group01/553624797/code2017/src/com/xxt/DataStructure/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.xxt.DataStructure; | ||
|
||
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(); | ||
} |
99 changes: 99 additions & 0 deletions
99
group01/553624797/code2017/src/com/xxt/DataStructure/MyArrayList.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,99 @@ | ||
package com.xxt.DataStructure; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by star on 2017/2/26. | ||
*/ | ||
public class MyArrayList implements List { | ||
|
||
|
||
|
||
private Object[] elementData; | ||
private int size = elementData.length; | ||
|
||
|
||
public MyArrayList(int initialCapacity) { | ||
if (initialCapacity > 0) { | ||
this.elementData = new Object[initialCapacity]; | ||
} else { | ||
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); | ||
} | ||
} | ||
|
||
public MyArrayList() { | ||
this(10); | ||
} | ||
|
||
|
||
@Override | ||
public void add(Object o) { | ||
ensureCapacity(size + 1); | ||
elementData[size + 1] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
//判断数组下标是否越界 | ||
if(index < 0 || index > elementData.length){ | ||
throw new IndexOutOfBoundsException("index : "+index+"size : "+size); | ||
} | ||
ensureCapacity(index); | ||
System.arraycopy(elementData, index,elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
//判断数组下标是否越界 | ||
if(index < 0 || index > elementData.length){ | ||
throw new IndexOutOfBoundsException("index : "+index+"size : "+size); | ||
} | ||
|
||
|
||
//取出删除的值 | ||
Object oldValue = elementData[index]; | ||
|
||
//做删除操作同样是复制数组 | ||
//计算要删除的数量 | ||
int numMoved = size - index - 1; | ||
if ( numMoved > 0){ | ||
System.arraycopy(elementData, index + 1, elementData, index, numMoved); | ||
} | ||
|
||
//将数组最后一个元素置为空,让GC回收 | ||
elementData[size - 1] = null; | ||
return oldValue; | ||
|
||
} | ||
|
||
@Override | ||
public int size() { | ||
return elementData.length; | ||
} | ||
|
||
|
||
//判断是否扩容 | ||
public void ensureCapacity(int minCapacity) { | ||
//取得当前数组容量 | ||
int currentCapacity = elementData.length; | ||
//如果最小需要的容量小于当前数组容量则需要扩容 | ||
if (minCapacity < currentCapacity) { | ||
int newCapacity = currentCapacity + (currentCapacity >> 1); | ||
//如果扩容后的长度还是小于最小需要的长度,则直接以最小需要的长度作为当前数组的长度 | ||
if (newCapacity < minCapacity) { | ||
newCapacity = minCapacity; | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
141 changes: 141 additions & 0 deletions
141
group01/553624797/code2017/src/com/xxt/DataStructure/MyLinkedList.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,141 @@ | ||
package com.xxt.DataStructure; | ||
|
||
import java.util.Iterator; | ||
|
||
import java.util.LinkedList; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Created by star on 2017/2/25. | ||
*/ | ||
public class MyLinkedList<E> implements List{ | ||
|
||
public static class Node<E>{ | ||
E elementData; | ||
Node<E> prerious; | ||
Node<E> next; | ||
|
||
public Node(Node<E> prerious , E elementData, Node<E> next) { | ||
this.prerious = prerious; | ||
this.elementData= elementData; | ||
this.next = next; | ||
} | ||
} | ||
|
||
|
||
|
||
private Node header; | ||
private Node last; | ||
|
||
private int size = 0; | ||
|
||
|
||
//往最后一个节点添加元素 | ||
@Override | ||
public void add(Object elementData) { | ||
addBefore((E) elementData, last); | ||
} | ||
|
||
@Override | ||
public void add(int index, Object elementData) { | ||
addBefore((E) elementData, (index == size ? last : node(index))); | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
return node(index).elementData; | ||
|
||
} | ||
|
||
//获取index位置的节点 | ||
private Node<E> node(int index){ | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("数组下标越界"+size); | ||
} | ||
|
||
if(index < (size >> 1)){ | ||
Node<E> e = header; | ||
for(int i = 0; i < index; i ++){ | ||
e = header.next; | ||
return e; | ||
} | ||
}else { | ||
Node<E> e = last; | ||
for(int i = size - 1; i > index; i--){ | ||
e = last.prerious; | ||
return e; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
@Override | ||
public Object remove(int index) { | ||
|
||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("inde :" + index); | ||
} | ||
|
||
return remove(node(index)); | ||
} | ||
|
||
|
||
|
||
//返回被删除的节点 | ||
private Object remove(Node<E> e){ | ||
E movedElementData = e.elementData; | ||
|
||
//被删除节点的上个节点指向该节点的下个节点 | ||
e.prerious.next = e.next; | ||
|
||
//被删除节点的下个节点指向该节点的上个节点 | ||
e.next.prerious = e.prerious; | ||
|
||
|
||
//将该节点置为空,让GC能够回收 | ||
e.next = e.prerious = null; | ||
e.elementData = null; | ||
//长度-1 | ||
size--; | ||
return movedElementData; | ||
} | ||
|
||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public Object removeFirst(){ | ||
return remove(header.next); | ||
|
||
|
||
} | ||
|
||
public Object removeLast(){ | ||
return remove(last.prerious); | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
|
||
//插入一个新的节点 | ||
private Node<E> addBefore(E e, Node<E> node){ | ||
|
||
Node<E> newNode = new Node<E>(node.prerious, e, node.next); | ||
|
||
//将上个节点的next指向自己 | ||
newNode.prerious.next = newNode; | ||
|
||
//将下个节点的previous指向自己 | ||
newNode.next.prerious = newNode; | ||
|
||
size++; | ||
return newNode; | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
group01/553624797/code2017/src/com/xxt/DataStructure/MyQueue.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,88 @@ | ||
package com.xxt.DataStructure; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* Created by star on 2017/2/26. | ||
*/ | ||
public class MyQueue{ | ||
|
||
|
||
public static class Node<E>{ | ||
Object elementData; | ||
Node<E> prerious; | ||
Node<E> next; | ||
|
||
public Node(Object elementData, Node<E> prerious, Node<E> next) { | ||
this.elementData = elementData; | ||
this.prerious = prerious; | ||
this.next = next; | ||
} | ||
|
||
} | ||
|
||
|
||
private MyQueue.Node header; | ||
private MyQueue.Node last; | ||
|
||
private int size = 0; | ||
|
||
|
||
//入队操作.在链表的头节点插入元素 | ||
public void enQueue(Object o){ | ||
addBefore(o, header); | ||
} | ||
|
||
|
||
//出队操作,返回链表的尾节点的元素 | ||
public Object deQueue(){ | ||
return node(size); | ||
} | ||
|
||
|
||
public boolean isEmpty(){ | ||
return header == last; | ||
} | ||
|
||
|
||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
private Node addBefore(Object o , Node node){ | ||
Node newNode = new Node(o, node.prerious, node.next); | ||
|
||
newNode.prerious.next = newNode; | ||
newNode.next.prerious = newNode; | ||
|
||
size++; | ||
return newNode; | ||
|
||
} | ||
|
||
|
||
private Node node(int index){ | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("数组下标越界"+size); | ||
} | ||
|
||
if(index < (size >> 1)){ | ||
Node e = header; | ||
for(int i = 0; i < index; i ++){ | ||
e = header.next; | ||
return e; | ||
} | ||
}else { | ||
Node e = last; | ||
for(int i = size - 1; i > index; i--){ | ||
e = last.prerious; | ||
return e; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
group01/553624797/code2017/src/com/xxt/DataStructure/MyStack.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,52 @@ | ||
package com.xxt.DataStructure; | ||
|
||
import java.util.EmptyStackException; | ||
|
||
/** | ||
* Created by star on 2017/2/26. | ||
*/ | ||
public class MyStack { | ||
|
||
|
||
//采用数组实现; | ||
private Object[] array; | ||
//栈顶指针 | ||
private int top; | ||
private final static int size = 100; | ||
|
||
public MyStack(Object[] array, int top) { | ||
this.array = array; | ||
//空栈 | ||
top = -1 ; | ||
} | ||
|
||
public void push(Object elementData){ | ||
//栈满 | ||
if(top == size - 1){ | ||
throw new StackOverflowError(); | ||
}else { | ||
array[++top] = elementData; | ||
} | ||
} | ||
|
||
//弹栈 | ||
public Object pop(){ | ||
if( top == -1){ | ||
throw new EmptyStackException(); | ||
}else { | ||
return array[top--]; | ||
} | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return top == -1; | ||
} | ||
|
||
public Object peek(){ | ||
if(top == -1){ | ||
throw new EmptyStackException(); | ||
}else { | ||
return array[top]; | ||
} | ||
} | ||
} |
Oops, something went wrong.