数据结构的Java实现——顺序表
2014-05-11 13:09
344 查看
顺序表: 线性表的顺序表是,指的是用一组地址连续的存储单元一次存储线性表的数据元素。以元素在计算机内“物理位置相邻”来表示线性表中数据元素之间的逻辑关系。只要确定了存储线性表的起始位置,线性表中任何一数据元素都可以随机存取,所以线性表的存储结构是一种随机存取的存储结构。
由于高级程序设计语言中的数组类型也具有随机存取的特性,因此,通常都用数组来描述数据结构中的顺序存储结构。
优点——随机存储,读取数据的速度快
缺点——顺序存储,当需要增加、删除数据是慢
[java] view
plaincopy
package ds.linerlist;
/**
* 顺序表的实现
* @author Bao Yiming
* @param <E>
*/
public class ArrayList<E> {
private Object[] data = null; // data: 用来保存此线性表数据的数组
private int capacity; // capacity: 线性表的容量
private int current; // current: 当前数据的下标
/**
* 初始化为声明大小,则设置为10。
*/
ArrayList() {
this(10);
}
/**
* 初始化线性表,声明保存数据的数组大小。
* @param initialSize 顺序表的初始化大小
*/
ArrayList(int initialSize) {
if (initialSize >= 0) {
this.capacity = initialSize;
data = new Object[initialSize];
current = 0;
} else {
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
/**
* 在线性表的末尾添加元素,添加之前确认线性表是否已满
* @param e 待加入的元素
* @return
*/
public boolean AddElement(E e) {
ensureCapacity();
data[current] = e;
++current;
return true;
}
/**
* 检查存储数据的数组容量,如果数组已经满,则扩充容量;否则不操作。
*/
private void ensureCapacity() {
int index;
if (current == capacity) {
capacity *= 2;
Object[] newData = new Object[capacity];
for(index = 0; index < current; ++index) {
newData[index] = data[index];
}
data = newData;
}
}
/**
* 返回下标为index的元素
* @param index 欲取得元素的下标
* @return
*/
public E get(int index) {
validateIndex(index);
return (E) data[index];
}
/**
*
* @param index 待插入的位置
* @param e 待插入的元素
* @return
*/
public boolean set(int index, E e) {
validateIndex(index);
data[index] = e;
return true;
}
/**
* 验证下标值是否合法,非法时抛出异常
* @param index 待验证的下标值
*/
private void validateIndex(int index) {
if (index < 0 || index > current) {
throw new RuntimeException("无效的下标:" + index);
}
}
/**
* 返回当前顺序表的大小
* @return
*/
public int size() {
return current;
}
/**
* 在指定位置插入指定元素
* @param index 待插入的位置
* @param e 待插入的元素
* @return
*/
public boolean insert(int index, E e) {
validateIndex(index);
ensureCapacity();
for (int temp = current; temp > index; --temp) {
data[temp] = data[temp - 1];
}
data[index] = e;
return true;
}
/**
* 删除下标为index元素
* @param index 待删除元素的下标
* @return
*/
public boolean delete(int index) {
validateIndex(index);
for ( ; index < current - 1; ++index) {
data[index] = data[index + 1];
}
data[current - 1] = null;
--current;
return true;
}
@Override
public String toString() {
String str = "[ ";
for (Object o : data) {
if (o != null) {
str += o + " ";
}
}
str += "]";
return str;
}
}
由于高级程序设计语言中的数组类型也具有随机存取的特性,因此,通常都用数组来描述数据结构中的顺序存储结构。
优点——随机存储,读取数据的速度快
缺点——顺序存储,当需要增加、删除数据是慢
[java] view
plaincopy
package ds.linerlist;
/**
* 顺序表的实现
* @author Bao Yiming
* @param <E>
*/
public class ArrayList<E> {
private Object[] data = null; // data: 用来保存此线性表数据的数组
private int capacity; // capacity: 线性表的容量
private int current; // current: 当前数据的下标
/**
* 初始化为声明大小,则设置为10。
*/
ArrayList() {
this(10);
}
/**
* 初始化线性表,声明保存数据的数组大小。
* @param initialSize 顺序表的初始化大小
*/
ArrayList(int initialSize) {
if (initialSize >= 0) {
this.capacity = initialSize;
data = new Object[initialSize];
current = 0;
} else {
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
/**
* 在线性表的末尾添加元素,添加之前确认线性表是否已满
* @param e 待加入的元素
* @return
*/
public boolean AddElement(E e) {
ensureCapacity();
data[current] = e;
++current;
return true;
}
/**
* 检查存储数据的数组容量,如果数组已经满,则扩充容量;否则不操作。
*/
private void ensureCapacity() {
int index;
if (current == capacity) {
capacity *= 2;
Object[] newData = new Object[capacity];
for(index = 0; index < current; ++index) {
newData[index] = data[index];
}
data = newData;
}
}
/**
* 返回下标为index的元素
* @param index 欲取得元素的下标
* @return
*/
public E get(int index) {
validateIndex(index);
return (E) data[index];
}
/**
*
* @param index 待插入的位置
* @param e 待插入的元素
* @return
*/
public boolean set(int index, E e) {
validateIndex(index);
data[index] = e;
return true;
}
/**
* 验证下标值是否合法,非法时抛出异常
* @param index 待验证的下标值
*/
private void validateIndex(int index) {
if (index < 0 || index > current) {
throw new RuntimeException("无效的下标:" + index);
}
}
/**
* 返回当前顺序表的大小
* @return
*/
public int size() {
return current;
}
/**
* 在指定位置插入指定元素
* @param index 待插入的位置
* @param e 待插入的元素
* @return
*/
public boolean insert(int index, E e) {
validateIndex(index);
ensureCapacity();
for (int temp = current; temp > index; --temp) {
data[temp] = data[temp - 1];
}
data[index] = e;
return true;
}
/**
* 删除下标为index元素
* @param index 待删除元素的下标
* @return
*/
public boolean delete(int index) {
validateIndex(index);
for ( ; index < current - 1; ++index) {
data[index] = data[index + 1];
}
data[current - 1] = null;
--current;
return true;
}
@Override
public String toString() {
String str = "[ ";
for (Object o : data) {
if (o != null) {
str += o + " ";
}
}
str += "]";
return str;
}
}
相关文章推荐
- 数据结构 --- 单链表
- Linux环境编程之文件I/O(四):文件I/O的数据结构
- 数据结构之图的深度优先搜索
- 数据结构
- 数据结构 DFS
- 邻接矩阵 数据结构
- UVA 297 Quadtrees( 数据结构,树)
- UVA 548 Tree(数据结构,二叉树,遍历)
- UVA 112 & POJ 1145 Tree Summing(数据结构,二叉树,栈)
- 数据结构之(图最短路径之)Floyd(弗洛伊德)算法
- 数据结构之(图最短路径之)Dijkstra(迪杰斯特拉)算法
- 数据结构之二叉树(遍历、建立、深度)
- 数据结构-深度遍历和广度遍历
- 二叉树的遍历问题
- 数据结构与算法-链表的基本操作---ShinPans
- 数据结构C语言二叉树的遍历
- 数据结构-单链表 读书笔记
- 数据结构cp1
- 线段树单点更新-----高级数据结构
- 数据结构《19》----String容器的三种实现