线性表——顺序表

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

/**
 * 顺序表
 * @author VESON
 *
 * @param <E>
 */
public class SequeceList<E> {
	
	private static int DEFAULT_SIZE = 16;
	private Object[] table; //顺序表数组
	private int n; //顺序表中真实的元素个数
	private int table_capacity; //顺序表最大容量
	
	/**
	 * 指定容量创建顺序表
	 * @param capacity
	 */
	public SequeceList(int capacity) {
		table = new Object[capacity];
		n = 0;//table中还没有元素
		table_capacity = capacity;
	}
	
	/**
	 * 默认值创建顺序表
	 */
	public SequeceList() {
		this(DEFAULT_SIZE);
		n=0;
	}
	/**
	 * 判断顺序表是否为空
	 * @return boolean
	 */
	public boolean isEmpty() {
		return n == 0;
	}
	/**
	 * 判断顺序表容量是否已满
	 */
	public boolean isFull() {
		if(n>=table_capacity) {
			return true;
		}
		return false;
	}
	/**
	 * 为顺序表增加10容量
	 * @return boolean
	 */
	public boolean addCapacity() {
		table = new Object[table_capacity+10];
		table_capacity += 10;
		return true;
	}
	/**
	 * 返回顺序表长度
	 * @param args
	 */
	public int length() {
		return n;
	}
	/**
	 * 返回某个值的索引
	 * @param args
	 */
	public Object getIndex(E value) {
		int i;
		for(i=0; i<n; i++) {
			if(value == table[i]) break;
		}
		if(i == n) return null;
		else return i;
	}
	/**
	 * 返回某个下标所对应的顺序表的值
	 * @param args
	 */
	public E getValue(int index) {
		if(index>=0 && index<=n) {
			return (E)table[index];
		}
		return null;
	}
	/**
	 * 在指定下标处插入某个值,成功返回true,失败返回false
	 * @param args
	 */
	public boolean InsertValue(E value, int index) {
		if(index>=n || index<0) {
			return false;
		}
		//不能插入null
		if(value==null) {
			return false;
		}
		if(this.isFull()) {	
			System.out.println("顺序表容量已满!");
			return false;
		}
		//index后的元素向后移
		for(int i=n-1; i>index; i--) {
			table[i+1] = table[i]; 
		}
		table[index] = value;
		n++;
		return true;
	}
	/**
	 * 在顺序表最后加入元素
	 * @param args
	 */
	public boolean addElement(E element) {
		if(this.isFull()) {
			System.out.println("顺序表容量已满!");
			return false;
		}
		table[n] = element;
		n++;
		return true;
	}
	/**
	 * 移除index的元素
	 * @param args
	 */
	public boolean remove(int index) {
		int i;
		if(index>n || index<0) {
			return false;
		}
		int copylength = n-index-1;
		if(copylength>0)
			System.arraycopy(table, index+1, table, index, copylength);
		table[n]=null;
		this.n--;
		return true;
	}
	/**
	 * 清空顺序表
	 * @param args
	 */
	public boolean clear() {
		for(int i=0; i<n; i++) {
			table[i] = null;
		}
		n = 0;
		return true;
	}
	/**
	 * 返回所有元素的字符串,格式为a,b,c...
	 * @param args
	 */
	public String toString() {
		String str = "";
		for(int i=0; i<this.length(); i++) {
			str += table[i].toString();
			if(i+1<this.length()) //最后一个元素之后无“ ,”
				str += ",";
		}
		return str;
	}
	/**
	 * 打印顺序表的所有元素
	 */
	public void print() {
		
		for(int i=0; i<n; i++) {
			System.out.print(table[i] + " ");
		}
		System.out.println();
	}
	/**
	 * 返回第一个不为空的索引
	 * @return
	 */
	public int noNull() {
		for(int i=0; i<n; i++) {
			if(table[i] != null) return i;
		}
		return -1;
	}
	
//	public static void main(String[] args) {
//		SequeceList sl = new SequeceList();
//		sl.addElement(2);
//		sl.print();
//		System.out.println(sl.isEmpty() + " " + sl.noNull());
//	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值