线性表之顺序表

 线性表是一种最常见、最简单。也是最基本的数据结构,它是学习其他数据结构的基础。线性表在计算机中可以用顺序储存和链式存储结构来表示。其中用顺序存储结构表示的线性表成为顺序表,用链式存储结构表示的线性表称为链表。链表又分为单链表、双向链表和循环链表。
      线性表的基本特征: 
总存在唯一的第一个数据元素
总存在唯一的最后一个数据元素
除第一个数据元素外,集合中的每一个数据元素都只有一个前驱的数据元素
除最后一个数据元素外,集合中的每一个数据元素都只有一个后继的数据元素

线性表的抽象数据结构类型用java接口描述如下:
package com.slowly.xxb;

public interface IList {
       //将一个已经存在的线性表置成空表
    public void clear();
    //判断线性表是否为空
    public boolean isEmpty();
    //求线性表长度
    public int length();
    //取元素操作,返回线性表中第i个元素的值
    public Object get( int i ) throws Exception;
    //插入操作,在线性表的第i的数据元素之前插入一个值为x的元素
    public void insert( int i,Object x) throws Exception;
    //删除线性表中第i个元素
    public void remove( int i) throws Exception;
    //查找操作,返回线性表中首次出现的指定的数据元素的位序号,若线性表中不包含次数据元素,则返回-1.
    public int indexof(Object x );
    //输出操作,输出线性表中各个元素的值。
    public void display();

}

一、线性表的顺序存储
     1、顺序表的定义
          顺序表就是顺序存储的线性表。顺序存储是用一组地址连续的存储单元依次存放线性表中各个数据元素的存储结构。
     2、顺序表的特点
          在线性表中逻辑上相邻的数据元素,在物理上存储位置也是相邻的。
          存储密度高,但要预先分配足够应用的存储空间,这可能造成存储空间的浪费
          便于随机存取
          不便于插入和删除操作

     3、顺序表类的描述
package com.slowly.xxb;

public class SqList implements IList {
	
	private Object[] listElem;
    private int curLen;
    public SqList(int maxSize){
        curLen = 0;
        listElem = new Object[maxSize];
   }

	public void clear() {
		curLen = 0;
	}

	public boolean isEmpty() {
		return curLen==0;
	}

	public int length() {
		return curLen;
	}

	public Object get(int i)throws Exception{
        if(i<0||i>curLen-1)
             throw new Exception("第"+i+"个元素不存在");
        return listElem[i];
    }
//	顺序表的插入操作
	public void insert(int i, Object x)throws Exception {
		if(curLen == listElem.length) //顺序表说白了就是一个数组,所以可以直接调用length
			throw new Exception("顺序表已经满了");
		if(i<0||i>curLen)
			throw new Exception("插入位置不合法");
		for(int j = curLen;j>i;j--)//插入位置后边的所有元素后移一位
			listElem[j]=listElem[j-1];
			listElem[i] = x;
			curLen++;
	}

	public void remove(int i) throws Exception {
		if(i<0||i>curLen)
			throw new Exception("插入位置不合法");
		for(int j = i;j<curLen;j++)//删除位置后边的所有元素前移一位
			listElem[j]=listElem[j+1];
			curLen--;
	}


	public int indexof(Object x) {
		for(int i=0;i<curLen;i++){
			if(listElem[i].equals(x))
				return i;
		}
		return -1;
	}

	public void display() {
		for(int i=0;i<curLen;i++){
			System.out.println(listElem[i]+"");
		}

	}

}
4.线性表应用例子
package com.slowly.xxb;


public class example01 {
	public static void main(String[] args) {
		SqList L = new SqList(10);
		try {
			L.insert(0,'A');
			L.insert(1,'B');
			L.insert(2,'C');
			L.insert(3,'D');
			L.insert(4,'E');
		} catch (Exception e) {
			e.printStackTrace();
		}
	L.display();
	int order = L.indexof('B');
	System.out.println("B的索引为"+order);
	}
}
显示结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值