因为最近在复习数据结构,看那本《大话数据结构》,这本书是用C语言写的,就想着自己用java实现,顺便也就记录自己的学习笔记。talk is cheap show me the code.
package com.chenli.List;
/**
* 实现基本的增删改查
* @author 陈力
*
*/
public interface IList {
//返回线性表的大小
public int getSize();
//判断线性表是否为空
public boolean isEmpty();
//是否包含某个元素
public boolean contains(Object obj);
//返回元素在线性表的序号
public int indexOf(Object obj);
//插入
public void insert(int i,Object obj) throws OutOfBoundaryException;
//删除指定序号的元素
public Object remove(int i)throws OutOfBoundaryException;
//返回线性表指定序号的元素
public Object get(int i)throws OutOfBoundaryException;
//添加元素
public void add(Object obj);
}
定义一个接口,模仿JDK的写法。
package com.chenli.List;
/**
* 比较对象的接口
* @author Administrator
*
*/
public interface IComparator {
//判断两个元素是否相等
public boolean equals(Object obj1,Object obj2);
/**
* 比较两个元素的大小
* 如果obj1小于obj2返回-1
* 等于返回0
* 大于返回1
*/
public int compare(Object obj1,Object obj2);
}
自定义一个比较器接口
package com.chenli.List;
public class DefaultComparator implements IComparator {
@Override
public boolean equals(Object obj1, Object obj2) {
// TODO Auto-generated method stub
return obj1.equals(obj2);
}
@Override
public int compare(Object obj1, Object obj2) {
// TODO Auto-generated method stub
return obj1.toString().compareTo(obj2.toString());
}
}
实现一个默认比较器
package com.chenli.List;
@SuppressWarnings("all")
public class MyList implements IList {
private int LENGTH=8;//数组最大容量
private IComparator icomparator;//比较器
private int size;//顺序表所包含到元素个数
private Object[] elements;//存储顺序表的数组
//构造方法
public MyList(){
this(new DefaultComparator());
}
public MyList(IComparator icomparator) {
// TODO Auto-generated constructor stub
this.icomparator = icomparator;
size = 0;
elements = new Object[LENGTH];
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return size==0;
}
@Override
public boolean contains(Object obj) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++){
if(this.icomparator.equals(obj, elements[i])){
return true;
}
}
return false;
}
@Override
public int indexOf(Object obj) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++){
if(icomparator.equals(obj, elements[i])){
return i;
}
}
return -1;
}
@Override
public void insert(int i, Object obj) throws OutOfBoundaryException {
// TODO Auto-generated method stub
//检查下标越界
if(i<0 || i>=size){
throw new OutOfBoundaryException("指定的插入序号越界");
}
//检查数组容量
if(size>=elements.length){
expandSpace();
}
for(int j=size;j>i;j--){
elements[j] = elements[j-1];
}
//插入数据
elements[i] = obj;
//长度+1
size++;
}
//数组扩容
private void expandSpace() {
// TODO Auto-generated method stub
Object[] a = new Object[elements.length*2];
for(int i=0;i<elements.length;i++){
a[i] = elements[i];
}
elements = a;
}
@Override
public Object remove(int i) throws OutOfBoundaryException {
// TODO Auto-generated method stub
//检查下标越界
if(i<0 || i>=size){
throw new OutOfBoundaryException("指定的删除序号越界");
}
//被删除的元素赋值给Object
Object obj = elements[i];
//删除元素之后数据都前移一位
for(int j=i;j<size;j++){
elements[j-1] = elements[j];
}
//数据元素-1
size--;
return obj;
}
@Override
public Object get(int i) throws OutOfBoundaryException {
// TODO Auto-generated method stub
//检查下标越界
if(i<0 || i>=size){
throw new OutOfBoundaryException("指定的序号越界");
}
return elements[i];
}
@Override
public void add(Object obj) {
// TODO Auto-generated method stub
elements[size] = obj;
size++;
}
}
定义一个实现类,实现接口的所有方法。
package com.chenli.List;
public class OutOfBoundaryException extends RuntimeException{
private static final long serialVersionUID = 1L;
public OutOfBoundaryException(String str){
super(str);
}
}
自己实现一个抛出异常的类。
顺序表的时间复杂度
存、读数据时,时间复杂度为O(1)
插入、删除数据时,时间复杂度为O(n)