线性表的顺序存储结构之顺序表类的实现_Java

时间: 2011-07-10 / 分类: 瞎扯谈 / 浏览次数: / 0个评论 发表评论

在上一篇博文——线性表接口的实现_Java中,我们实现了线性表的接口,今天让我们来实现线性表的顺序存储结构——顺序表类。

首先让我们来看下顺序表的定义:

线性表的顺序存储是用一组连续的内存单元依次存放线性表的数据元素,元素在内存的物理存储次序与它们在线性表中的逻辑次序相同,即元素ai与其直接前驱ai-1及直接后继ai+1的存储位置相邻。顺序存储的线性表也成为顺序表(sequential list)。

顺序表类SeqList提供线性表基于顺序存储结构的一种实现,它有两个私有成员变量table和n,table是一个存放元素的对象数组;n为线性表长度,n≤table.length。SeqList声明如下,它实现了线性表的接口LList。

package dataStructure.linearList;
import dataStructure.linearList.LList;

public class SeqList<E> implements LList<E>					//顺序表类,实现线性表接口
{
	private Object[] table;									//对象数组,私有成员
	private int n;											//顺序表长度
	
	public SeqList(int capacity)							//构造方法,创建置顶容量的空表
	{
		this.table = new Object[Math.abs(capacity)];
		this.n = 0;
	}
	
	public SeqList()										//指定空表的默认容量
	{
		this(16);
	}
	
	public boolean isEmpty()								//判断顺序表是否为空,若空返回true
	{
		return this.n == 0;
	}
	
	public int length()										//返回顺序表长度
	{
		return this.n;
	}
	
	public E get(int index)									//返回index(初值为0)位置的对象,若序号无效,返回null
	{
		if(index>=0 && index < this.n)
		{
			return (E)this.table[index];
		}
		return null;
	}
	
	public E set(int index,E element)						//设置index位置的对象为element,若操作成功,放回原对象,否则返回null
	{
		if(index >= 0 && index < this.n && element != null)
		{
			E old =(E)this.table[index];
			this.table[index] = element;
			return old;
		}
		return null;
	}
	
	public boolean add(int index,E element)					//在index位置插入element对象,若操作成功返回true,不能插入null
	{
		if(element == null)									//不能插入null
		{
			return false;
		}
		if(this.n == table.length)							//若数组满,则需要扩充顺序表容量
		{
			Object[] temp = this.table;
			this.table = new Object[temp.length*2];			//重新申请一个容量更大的数组
			for(int i = 0;i < temp.length;i++)
			{
				this.table[i] = temp[i];
			}
		}
		
		if(index < 0)										//下标容错
		{
			index = 0;
		}
		if(index > this.n)
		{
			index =this.n;
		}
		for(int j = this.n-1;j >= index;j--)				//元素后移,平均移动n/2
		{
			this.table[j+1] = this.table[j];
		}
		this.table[index] = element;
		this.n++;
		return true;
	}
	
	public boolean add(E element)							//在顺序表最后插入element对象
	{
		return add(this.n,element);
	}
	
	public E remove(int index)								//移去index位置的对象,若操作成功,则返回被移去的对象,否者返回null
	{
		if(this.n != 0 && index >= 0 && index < this.n)
		{
			E old = (E)this.table[index];
			for(int j = index;j < this.n-1;j++)				//元素前移,平均移动n/2
			{
				this.table[j] = this.table[j + 1];
			}
			this.table[this.n - 1] = null;
			this.n--;
			return old;
		}
		return null;
	}
	
	public void clear()										//清空顺序表
	{
		if(this.n != 0)
		{
			for(int i = 0;i < this.n;i++)
			{
				this.table[i] = null;
			}
			this.n=0;
		}
	}
	public String toString()								//返回显示所有元素的字符串,形式为(,)
	{
		String str = "(";
		if(this.n != 0)
		{
			for(int i = 0;i < this.n - 1;i++)
			{
				str += this.table[i].toString()+",";
			}
			str += this.table[this.n - 1].toString();
		}
		return str + ")";
	}
}

顺序表是一种随即存取结构,存取任何一个元素的get()、set()方法的时间复杂度是O(1)。

对顺序表进行插入或删除操作是,算法所花费的时间主要用于移动元素。在等概率情况下,插入一个元素平均需要移动一半的元素,时间复杂度为O(n)。同里,删除一个元素的时间复杂度亦为O(n)。

综上所述,顺序表具有下列特点:

①:元素的物理存储顺序直接反映表中元素的逻辑顺序,可以随机存取元素。

②:插入和删除的操作效率很低。每插入或删除一个元素,可能需要移动大量元素,其平均移动次数是顺序表长度的一半。再者,数组容量不可更改,存在因容量小造成数据溢出,或因容量过大造成内存资源浪费的问题。解决数据溢出的方法是,申请另一个更大容量的数组,并进行数组元素复制,但插入操作效率很低。

下一章中我将和大家探讨用顺序表解决约瑟夫问题(约瑟夫环)

您阅读此文共耗时

暂无评论

  1. VLTIC
    2012/11/28 16:38:00

    你的add操作,是不是应该判空下。要是this.n==0,应该就会出现异常。

  2. AngelHacker
    2013/07/01 13:43:00

    [reply]VLTIC[/reply]
    首次插入数据的时候n就是0,不会异常的。

发表评论

你必须 登录后 才能留言!