-
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 #9 from piaoxiangyijian/master
The first homework
- Loading branch information
Showing
8 changed files
with
471 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,9 @@ | ||
/bin/ | ||
*.class | ||
*.settings | ||
*.project | ||
*.classpath | ||
*/.settings | ||
*.iml | ||
/.idea | ||
/**/target/**/* |
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,126 @@ | ||
/*范例名称: | ||
* 原文件名称: | ||
* 要点: | ||
* 1. 实现基本的数据结构类:ArrayList | ||
*/ | ||
public class ArrayList_self<T> implements KIList<T> { | ||
/***初始化容量大小***/ | ||
private final static int INIT_CAPACITY=12; | ||
private Object[] mList=null; | ||
|
||
/***当前容量***/ | ||
private int mCurrentCapacity=0; | ||
/***容器中元素个数***/ | ||
private int mSize=0; | ||
|
||
public ArrayList_self(){ | ||
mList=new Object[INIT_CAPACITY]; | ||
mCurrentCapacity=INIT_CAPACITY; | ||
} | ||
|
||
/** | ||
* 插入一个元素到ArrayList尾部 | ||
* @param item | ||
* */ | ||
@Override | ||
public void add(T item) { | ||
// TODO Auto-generated method stub | ||
if(mSize==mCurrentCapacity){ | ||
expansion();//扩容 | ||
} | ||
mList[mSize]=item; | ||
mSize++; | ||
} | ||
|
||
/** | ||
* 插入一个元素到指定位置,从插入位置及其后面的元素往后移动一个位置 | ||
* @param index 要插入的位置 | ||
* @param item | ||
* */ | ||
@Override | ||
public void add(int index, T item) { | ||
// TODO Auto-generated method stub | ||
if (index<0 || index>=mSize) {//不允许index小于0,或者index >= 数组当前大小 | ||
throw new IndexOutOfBoundsException();//抛出越界异常 | ||
} | ||
if(mSize==mCurrentCapacity){ | ||
expansion();//扩容 | ||
} | ||
Object[] newList=new Object[mCurrentCapacity]; | ||
System.arraycopy(mList, 0, newList, 0, index); | ||
System.arraycopy(mList, index, newList, index+1, mSize-index); | ||
newList[index]=item; | ||
mList=newList; | ||
mSize++; | ||
} | ||
/** | ||
* 移除指定位置的元素,后面的元素向前移动一位 | ||
* @param index | ||
* */ | ||
@Override | ||
public T remove(int index) { | ||
// TODO Auto-generated method stub | ||
if(index<0 || index>=mSize){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object[] newList=new Object[mCurrentCapacity]; | ||
System.arraycopy(mList, 0, newList, 0, index); | ||
System.arraycopy(mList, index+1, newList, index, mSize - index); | ||
|
||
T tempT=(T) mList[index]; | ||
mList=newList; | ||
mSize--; | ||
|
||
return tempT; | ||
} | ||
/** | ||
* 更新指定位置的元素 | ||
* @param index | ||
* @param item | ||
* */ | ||
@Override | ||
public void set(int index, T item) { | ||
// TODO Auto-generated method stub | ||
if(index<0 || index>=mSize){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
mList[index]=item; | ||
} | ||
/** | ||
* 获取指定位置的元素 | ||
* @param index | ||
* @return | ||
* */ | ||
@Override | ||
public T get(int index) { | ||
// TODO Auto-generated method stub | ||
if(index<0 || index>=mSize){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
return (T)mList[index]; | ||
} | ||
/** | ||
* 获取当前链表的长度 | ||
* @return int | ||
* */ | ||
@Override | ||
public int size() { | ||
// TODO Auto-generated method stub | ||
return mSize; | ||
} | ||
|
||
/** | ||
* 扩容,当 mSize == mCurrentCapacity 时调用;当满的时候每次增加当前容量的50% | ||
* */ | ||
private void expansion() { | ||
// TODO Auto-generated method stub | ||
Object[] oldList=mList; | ||
Object[] newList=new Object[mCurrentCapacity + (mCurrentCapacity >> 1)]; | ||
System.arraycopy(oldList, 0, newList, 0, oldList.length); | ||
mList=newList; | ||
mCurrentCapacity=mCurrentCapacity + (mCurrentCapacity >> 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,41 @@ | ||
/*范例名称: | ||
* 原文件名称: | ||
* 要点: | ||
* 1. 实现基本的数据结构类:ArrayList、LinkedList、Queue、Stack、Tree | ||
*/ | ||
public class CollectionTest { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
//测试ArrayList | ||
ArrayList_self<Name> arrayList1=new ArrayList_self<Name>(); | ||
for(int i=0;i<15;i++){ | ||
arrayList1.add(new Name("An"+i, "Array")); | ||
if(i>6){ | ||
arrayList1.set(i, new Name("Bo"+i, "Array")); | ||
} | ||
System.out.println(arrayList1.get(i)); | ||
} | ||
|
||
//测试LinkedList | ||
LinkedList_self<Name> linkedList1=new LinkedList_self<Name>(); | ||
for(int i=0;i<8;i++){ | ||
linkedList1.add(new Name("An"+i, "Linked")); | ||
if(i>3){ | ||
linkedList1.set(i, new Name("Bo"+i, "Linked")); | ||
} | ||
System.out.println(linkedList1.get(i)); | ||
} | ||
|
||
//测试Stack | ||
Stack_self<Name> stack1=new Stack_self<Name>(); | ||
for(int i=0;i<6;i++){ | ||
stack1.push(new Name("An"+i, "Stack")); | ||
if(i>3){ | ||
System.out.println(stack1.peek()); | ||
} | ||
} | ||
} | ||
|
||
} |
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,18 @@ | ||
/*范例名称: | ||
* 原文件名称: | ||
* 要点: | ||
* 1. 顺序表接口:基本的增、删、改、查功能 | ||
*/ | ||
public interface KIList<T> { | ||
public void add(T item); | ||
public void add(int index, T item); | ||
|
||
public T remove(int index);//返回值是删除的元素 | ||
|
||
public void set(int index, T item); | ||
|
||
public T get(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,199 @@ | ||
/*范例名称: | ||
* 原文件名称: | ||
* 要点: | ||
* 1. 实现基本的数据结构类:LinkedList | ||
*/ | ||
public class LinkedList_self<T> implements KIList<T> { | ||
// 定义一个内部类Node Node 实例代表链表的节点 | ||
public class Node { | ||
public T data;// 链表节点的数据 | ||
public Node next;// 指向下一个节点的引用 | ||
|
||
// 构建无参构造器 | ||
public Node() { | ||
}; | ||
|
||
// 初始化全部属性构造器 | ||
public Node(T data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
} | ||
|
||
// 链表头节点 | ||
public Node header; | ||
// 链表尾节点 | ||
public Node tail; | ||
// 链表的节点数 | ||
public int size = 0; | ||
|
||
// 创建空链表 | ||
public LinkedList_self() { | ||
header = null; | ||
tail = null; | ||
} | ||
|
||
// 创建包含一个指定元素的链表 | ||
public LinkedList_self(T element) { | ||
header = new Node(element, tail); | ||
tail = header;// 只有一个节点,header tail 都指向该节点 | ||
size++; | ||
} | ||
|
||
/** | ||
* 插入一个元素到ArrayList尾部 | ||
* @param item | ||
*/ | ||
@Override | ||
public void add(T item) { | ||
// TODO Auto-generated method stub | ||
// 空链表 | ||
if (header == null) { | ||
header = new Node(item, tail); | ||
tail = header; | ||
} else { | ||
// 创建新节点 | ||
Node newNode = new Node(item, null); | ||
// 尾节点指向新节点 | ||
tail.next = newNode; | ||
tail = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
/** | ||
* 插入一个元素到指定位置,从插入位置及其后面的元素往后移动一个位置 | ||
* | ||
* @param index | ||
* 要插入的位置 | ||
* @param item | ||
*/ | ||
@Override | ||
public void add(int index, T item) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
// 如果是空链表 | ||
if (header == null) { | ||
add(item); | ||
} else { | ||
// 当index为0时在表头插入 | ||
if (index == 0) { | ||
addAtHeader(item); | ||
} | ||
else{ | ||
//获取前置节点 | ||
Node prev=getNodeByIndex(index-1); | ||
//prev指向新节点,新节点的next指向prev的next | ||
prev.next=new Node(item,prev.next); | ||
size++; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 移除指定位置的元素,后面的元素向前移动一位 | ||
* | ||
* @param index | ||
*/ | ||
@Override | ||
public T remove(int index) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
Node del = null; | ||
// 如果删除的是头结点 | ||
if (index == 0) { | ||
del = header; | ||
header = header.next; | ||
} else { | ||
// 获取删除节点的前置节点 | ||
Node prev = getNodeByIndex(index - 1); | ||
del = prev.next; | ||
prev.next = del.next; | ||
del.next = null; | ||
} | ||
size--; | ||
return del.data; | ||
} | ||
|
||
/** | ||
* 更新指定位置的元素 | ||
* | ||
* @param index | ||
* @param item | ||
*/ | ||
@Override | ||
public void set(int index, T item) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
// 从header节点开始 | ||
Node current = header; | ||
for (int i = 0; i < size && current != null; i++) { | ||
if (i == index) { | ||
current.data = item; | ||
} | ||
current = current.next; | ||
} | ||
} | ||
|
||
/** | ||
* 获取指定位置的元素 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
@Override | ||
public T get(int index) { | ||
// TODO Auto-generated method stub | ||
return getNodeByIndex(index).data; | ||
} | ||
|
||
/** | ||
* 返回链表的长度 | ||
* | ||
* @param item | ||
*/ | ||
@Override | ||
public int size() { | ||
// TODO Auto-generated method stub | ||
return size; | ||
} | ||
|
||
// 根据索引index获取指定位置的节点 | ||
private Node getNodeByIndex(int index) { | ||
// TODO Auto-generated method stub | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
// 从header节点开始 | ||
Node current = header; | ||
for (int i = 0; i < size && current != null; i++) { | ||
if (i == index) { | ||
return current; | ||
} | ||
current = current.next; | ||
} | ||
return null; | ||
} | ||
|
||
// 采用头插法为链表添加新节点 | ||
private void addAtHeader(T item) { | ||
// TODO Auto-generated method stub | ||
//创建新节点,并让新节点的next 指向header | ||
//以新节点为header | ||
Node newNode=new Node(item,header); | ||
header=newNode; | ||
//如果插入前是空链表 | ||
if(tail==null){ | ||
tail=header; | ||
} | ||
size++; | ||
} | ||
} |
Oops, something went wrong.