1线性表
1.1线性表的基本操作
线性表是一种最常用的最简单的一种数据结构在计算机中可哟以使用顺序存储和链式存储结构。对于线性表虽然每个数据元素的值不相同但必须具有相同的数据类型,同时数据元素之间按存在一种一对一的逻辑关系及就是
(1) 第一个数据元素没有前驱这个数据元素称为开始节点
(2) 最后一个数据元素没有后继这个节点称为终端节点
(3) 除了第一个和最后一个节点其他节点有且仅有一个前驱和一个后继
线性表的基本操作如下:
(1) 线性表的置空clear()
(2) 线性表的判空isEmpty()
(3) 求线性表的长度length()
(4) 取元素操作get(i)
(5) 插入操作insert(I,x)在第i个数据元素之前插入一个值为x的数据元素
(6) 删除操作remove(i)
(7) 查找操作indexOf(x)
(8) 输出操作display();
线性表的抽象数据类型用JAVA接口实现 具体描述如下:
/*
* Kiss_My_Love
* 2012/8/26
* xaut.media082
* */
package www.xaut.com;
public interface Ilist {
public void clear();
public boolean isEmpty();
public int length();
public Object get(int i);
public void insert(int i,Object x);
public void remove(int i);
public int indexOf(Object x);
public void display();
}
1.2线性表的顺序存储和实现
顺序存储是用一组地址连续的存储单元依次存放线性表的各个数据元素,一般采用数组来实现顺序表类的描述下面是顺序表的JAVA语言描述:
/*
* Kiss_My_Love
* 2012/8/26
* xaut.media082
* */
package www.xaut.com;
public class SqList implements Ilist {
public Object[] listItem; //线性表的存储空间
private int curLen; //线性表的当前长度
public SqList(int maxSize){
this.curLen=0;
this.listItem=new Object[maxSize];
}
public SqList(){
this.curLen=0;
}
@Override
public void clear() {
this.curLen=0;
}
@Override
public boolean isEmpty() {
return this.curLen==0;
}
@Override
public int length() {
return this.curLen;
}
@Override
public Object get(int i) {
if(i<0||i>this.curLen){
try {
throw new Exception("第"+i+"个数据元素不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
return listItem[i];
}
@Override
public void insert(int i, Object x) {
if(this.curLen==this.listItem.length){
try {
throw new Exception("顺序表已满");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(i<0||i>this.curLen){
try {
throw new Exception("插入位置不合法");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
for(int j=this.curLen;j>i;j--){
this.listItem[j]=this.listItem[j-1];
}
this.listItem[i]=x;
this.curLen++;
}
}
@Override
public void remove(int i) {
if(i<0||i>this.curLen-1){
try {
throw new Exception("删除位置不合法");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
for(int j=i;j<this.curLen-1;j++){
this.listItem[j]=this.listItem[j+1];
}
}
this.curLen--;
}
@Override
public int indexOf(Object x) {
int i;
for(i=0;i<this.curLen&&! this.listItem[i].equals(x);i++){}
if(i<this.curLen)
return i;
else return -1;
}
@Override
public void display() {
System.out.print("[");
for(int i=0;i<this.curLen-1;i++){
System.out.print(this.listItem[i]+",");
}
System.out.println(this.listItem[this.curLen-1]+"]");
}
}
测试类
/*
* Kiss_My_Love
* 2012/8/26
* xaut.media082
* */
package www.xaut.com;
public class TestSqList {
public static void main(String[] args) {
SqList L=new SqList(10);
L.insert(0, 'q');
L.insert(1, 'w');
L.insert(2, 'd');
L.insert(3, 'f');
L.insert(4, 'k');
L.display();
System.out.println("线性表是空吗?"+L.isEmpty());
System.out.println("线性表的长度是"+ L.length());
System.out.println("获取第i个元素"+L.get(4));
L.remove(2);
L.display();
System.out.print("查找木个元素"+L.indexOf('f'));
}
}