链表和线性表的工具类

线性表
package com.lxr.listtable;

public interface List<T> {
	//判断链表是否为空
	public boolean isEmpty();
	//获取链表中的元素
	public T getData(int i) throws Exception;
	//设置某个元素的值
	public void update(int i, T obj) throws Exception;
	//根据index添加元素
	public void insert(int i, T obj) throws Exception;
	//添加元素
	public void insert(T date) throws Exception;
	//根据index移除元素
	public boolean delete(int i) throws Exception;
	//根据元素移除
	public boolean deleteData(T data) throws Exception;
	//清空链表
	public void remove();
	//根据元素值获取元素的下标
	public int indexOf(T data) throws Exception;
	//根据data值查询最后一个出现在顺序表的下标
	public int lastIndexOf(T data) throws Exception;
	//是否包含data元素
	public boolean hasData(T data)throws Exception;;
	//格式化输出
	public String toString();
	//判断两个顺序表是否相等
	
	public int size();
	
	
	
}
package com.lxr.listtable;
/**
 * 
 * @author lxr
 * @date 2017年8月3日下午4:42:48
 * @deprecated:线性表实现类
 * @param <T>
 */
public class ListImpl<T> implements List<T> {
	Object[] table;
	int length=0;
	
	public ListImpl() {		
		this(10);
	}
	public ListImpl(int size) {
		this.table=new Object[size];
		//length=table.length;
	}
	
	public ListImpl(T[] arry) {
		if(arry==null){
			throw new NullPointerException("数组不能为空");
		} 
		this.table=new Object[arry.length];
		table=arry;
		length=table.length;
	}
	@Override
	/**
	 * 判断线性表是否为空
	 */
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return length==0;
	}

	@Override
	/**
	 * 通过索引值获得线性表的值
	 */
	public T getData(int i) throws Exception {
		// TODO Auto-generated method stub
		return (T) table[i];
	}

	@Override
	/**
	 * 通过索引值修改线性表对应的值
	 */
	public void update(int i, T obj) throws Exception {
		
		if(i>=0&&i<length&&obj!=null){
			table[i]=obj;
		}
		
	}

	@Override
	/**
	 * 通过索引值将值插入到对应的位置
	 */
	public void insert(int i, T value) throws Exception {
		
		if(value==null){
			throw new Exception("传入的值不能为空!");
		}
		if(i>length&&length==0){//线性表没有元素
			i=0;
		}
		if(i<0){//传入的下标为负数,默认插在第一位
			i=0;
		}
		if(i>length){//插入的下标大于线性表的最大值,默认插入到最后
			i=length;
			
		}
		if(i==length||table.length==length){//插入在最后面,将线性表扩容+10
			Object[] temp = this.table; 
			for (int j = 0; j < temp.length; j++) {
				table=new Object[length+10];
			}
			for (int j = 0; j < temp.length; j++) {//拷贝原来线性表的值
				table[j]=temp[j];
			}
		}
		
		for (int j =length ; j >i; j--) {//移动元素,将要插入的元素位置空出来
		    table[j] = table[j - 1];
		}
		
		this.table[i]=value;//赋值
		length++;//线性表长度加1 
		
	}

	@Override
	/**
	 * 将对应元素插入到线性表末尾
	 */
	public void insert(T date) throws Exception {
		insert(length,date);
		
	}

	@Override
	/**
	 * 通过索引值删除元素
	 */
	public boolean delete(int index) throws Exception {
		
		if(index>=0&&index<length){
			//把原数组赋值给临时数组
			Object[] temp = this.table; //5
			//将原来数组进行容量-1操作,并把原数组赋值到新数组中.
			this.length--;
			this.table = new Object[this.length]; //10
			//[x<index]的目的是为了减少循环次数,提高效率
			for (int x = 0; x <this.length; x++) {
				  if(x<index){
					  table[x]=temp[x];
				  }
				  else{
					  table[x]=temp[x+1];
				  }
			}
			return true;
		}
		return false;
	}

	@Override
	/**
	 * 删除对应元素第一次出现的值
	 */
	public boolean deleteData(T data) throws Exception {
		if(data==null||this.length==0){
			return false;
		}
		
		return delete(indexOf(data));
		
	}

	@Override
	/**
	 * 清空线性表
	 */
	public void remove() {
		this.table=null;
		this.length=0;
		
	}

	@Override
	/**
	 * 获取对应元素第一次出现的下标
	 */
	public int indexOf(T data) throws Exception {
		for(int i=this.length-1;i>=0;i--){
			if(data.equals(table[i])){
				return i;
			}
		}
		return -1;
	}

	@Override
	/**
	 * 获取对应元素最后一次出现的下标
	 */
	public int lastIndexOf(T data) throws Exception {
		for(int i=this.length-1;i>=0;i--){
			if(data.equals(table[i])){
				return i;
			}
		}
		return -1;
	}

	@Override
	/**
	 * 判断该值在线性表中是否出现
	 */
	public boolean hasData(T data) throws Exception {
		if(data==null){
			return false;
		}
		return this.indexOf(data)>=0;
	}

	@Override
	/**
	 * 返回线性表的长度
	 */
	public int size() {
		// TODO Auto-generated method stub
		return length;
	}

}

链表

package com.lxr.listtable;

public class NoNode<T> {
	public T data;
	public NoNode<T> next;
	public NoNode() {
	}
	public NoNode(T data) {
		this.data = data;
	}
	public NoNode(T data, NoNode<T> next){
		this.data = data;
		this.next = next;
	}
	
}
package com.lxr.listtable;

public interface NoHeadNode<T> {
	public boolean isEmpty();
	public T getData(int i) throws Exception;
	public void update(int i, T obj) throws Exception;
	public void insert(int i, T obj) throws Exception;
	public void insert(T date) throws Exception;
	public boolean delete(int i) throws Exception;
	public boolean deleteData(T data) throws Exception;
	public void remove();
	public int indexOf(T data) throws Exception;
	public int lastIndexOf(T data) throws Exception;
	public boolean hasData(T data)throws Exception;;
	public String toString();
	public int size();
}
package com.lxr.listtable;
/**
 * 所有下标都由0开始
 * @author lxr
 * @date 2017年8月3日下午3:08:05
 * @deprecated:单向链表的实现类
 * @param <T>
 */
public class NoHeadNodeImpl<T> implements NoHeadNode<T> {
	
	NoNode<T> headNode;//链表的入口,头

	public NoHeadNodeImpl() {
	}
	
	public NoHeadNodeImpl(NoNode<T> headNode) {
		this.headNode = headNode;
	}
	
	public NoHeadNodeImpl(T[] arrys) {
		this();
		if(arrys!=null){
			this.headNode=new NoNode<T>(arrys[0]);
			//获取下一个结点的指针域
			NoNode<T> node=this.headNode;
			//确定数组元素转换为链表的某一个地址域
			for (int i = 1; i < arrys.length; i++) {
				node.next=new NoNode<>(arrys[i]);//改变结点next指向的结点
				//此时node结点的next改变,data未改变
				node=node.next;//将后面的一个结点赋值给当前结点
			}	
		}
	}
	public NoHeadNodeImpl(NoHeadNodeImpl<T>  nodeList){
		this();
		if(nodeList!=null && nodeList.headNode.next!=null){
			this.headNode=nodeList.headNode;
		}
	}
	
	/**
	 * 将一个元素插入到对应的结点
	 * @param index 要添加结点的位置
	 * @param data 要添加结点的元素值
	 * @throws Exception
	 */
	@Override
	public void insert(int index, T data) throws Exception {
		if(data!=null){
			if(index<=0){
				headNode=new NoNode<T>(data,headNode);
			}else{
				NoNode<T> now=headNode;
				
				for (int i = 0; i < index-1&&now.next!=null; i++) {
					now=now.next;
				}
				now.next=new NoNode<T>(data,now.next);
				
			}	
		}
		
	}
	/**
	 * 将一个元素插入到链表的末尾
	 * @param date 要插入的元素
	 * @throws Exception
	 */
	@Override
	public void insert(T date) throws Exception {
		if(date!=null){
			NoNode<T> now=headNode;
			while(now.next!=null){
				now=now.next;
			}
			now.next=new NoNode<T>(date);
		}
	}
	/**
	 * 
	 * @return链表是否为空
	 */
	@Override
	public boolean isEmpty() {
		return headNode==null;
	}
	/**
	 * 返回指定位置的值
	 * @param i 要获取元素在链表中的位置
	 * @return 返回获取到的元素值,如果没有查到返回空
	 * @throws Exception
	 */
	@Override
	public T getData(int i) throws Exception {
		T data=null;
		if(i>=0){
			NoNode<T> now=headNode;
			data=now.data;
			for (int j = 0; j <i; j++) {
				if(now.next!=null){
					data=now.next.data;
					now=now.next;
				}
				else{//超界
					return null;
				}
			}
			
		}
		else{
			return null;
		}
		return data;
	}
	/**
	 * 修改链表指定位置的元素的值,如果修改的位置不合法将不进行修改
	 * @param i 要修改的位置
	 * @param obj 要修改的值
	 * @throws Exception
	 */
	@Override
	public void update(int i, T obj) throws Exception {
		int length=size();
		System.out.println(length+"---");
		if(i==0){
			headNode.data=obj;
			
		}
		else if(i>0&&i<length){
			NoNode<T> node=headNode;
			for (int j = 0; j <i&&node.next!=null; j++) {
				node=node.next;
			}
			node.data=obj;
		}
		
		else{
			System.out.println("更新失败!");
		}
	}

	/**
	 * 删除指定位置的元素值,删除成功返回true,删除失败返回false
	 * @param i 需要删除的位置
	 * @return	返回删除的结果
	 * @throws Exception
	 */
	@Override
	public boolean delete(int i) throws Exception {
		if(i<0||i>=size()){
			//结点位置不满足要求,不进行删除
			return false;
		}if(i==0){//删除第一个结点
			headNode=headNode.next;
			return true;	
		}
		
		NoNode<T> now=headNode;
		//将指针移动到要删除结点的前一个结点,同时保证后面一个结点不能为空,如果为空则说明是末尾结点
		for (int j=0; j <i-1&&now.next.next!=null; j++) {
			now=now.next;
		}
		now.next=now.next.next;//正常情况删除(包括中间和末尾节点)
		return true;
	}
	/**
	 * 通过元素值 删除结点
	 * @param data 需要删除的元素值
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean deleteData(T data) throws Exception {
	
		return delete(indexOf(data));
	}
	/**
	 * 清空链表中的所有元素
	 */
	@Override
	public void remove() {
	headNode=null;
		
	}
	/**
	 * 获取对应元素在链表中的位置,查找成功返回大于等于零int类型的值,查找失败返回-1
	 * @param data 需要查找的元素
	 * @return 返回元素在链表中的位置
	 * @throws Exception
	 */
	@Override
	public int indexOf(T data) throws Exception {
		if(headNode==null){
			return -1;
		}
		if(data.equals(headNode.data)){
			return 0;
		}
		int index=0;
		NoNode<T> node=headNode;
		while(node!=null){
			
			//System.out.println(index+"--");
			if(node.data.equals(data)){
				return index;
				
			}
			index++;
			node=node.next;
		}
		return -1;//没有找到
	}
	/**
	 * 
	 * @param data
	 * @return	返回元素在链表中最后一次出现的位置,没有查到返回-1
	 * @throws Exception
	 */
	@Override
	public int lastIndexOf(T data) throws Exception {
		int index=0;//记录当前是在第几个结点
		int tis=-1;//记录最后一次出现的结点位置
		if(headNode==null){
			return -1;
		}
		if(data.equals(headNode.data)){
			tis=0;
		}
		NoNode<T> node=headNode;
		while(node!=null){
			
			System.out.println(index+"--");
			if(node.data.equals(data)){
				tis=index;	
			}
			index++;
			node=node.next;
		}
		return tis;//没有找到
		
	}
	/**
	 * 判断指定元素在链表中是否存在
	 * @param data 需要查找的元素值
	 * @return 有该元素返回true,否者返回false
	 * @throws Exception
	 */
	@Override
	public boolean hasData(T data) throws Exception {
		return indexOf(data)>=0;
	}
	/**
	 * 
	 * @return 返回该链表的长度
	 */
	@Override
	public int size() {
		if(headNode==null){
			return 0;
		}
		int length=0;
		NoNode<T> head=headNode;
		while(head!=null){
			length++;//获得链表的长度
			head=head.next;
			
		}
		return length;
	}
	/**
	 * 输出该链表中的所有元素
	 */
	public void print(){
		NoNode<T> pre=this.headNode;
		System.out.print("[ ");
		while(pre!=null){
			System.out.print(pre.data+" ");
			pre=pre.next;
		}
		System.out.println("]");
		
	}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值