数据结构中JAVA实现双向链表

       JAVA是一门面向对象语言,当你一切都以对象为目的编程时,会让人觉得敲出来的代码好似自出餐里面的食物,只要你已经进入餐厅,任何食物都可以任你挑选,省时省力。

      用JAVA实现双向链表中首先定义了一个接口来规定每一个类的作用,如代码所示:

package 双向链表;

public interface Link {
    //添加结点
	void add(Object obj);
	
	//删除指定位置结点
	boolean remove(int index);
	
	//修改指定位置数据
	boolean set(int index,Object obj);
	
	//取得指定位置数据
	Object get(int index);
	
	//取得链表所有数据集合
	void printLink();
	
	//将链表转为数组
	Object [] toArray();
	
	//取得链表长度
	int getsize();
	
	//用于清空链表数据
	void clear();
	
	//判断指定内容在链表中是否存在
	int contanins (Object obj);
}

接着定义一个类覆写该接口中的方法:

 

package 双向链表;

public  class LinkImpl implements Link{
		
	private Node first;
	private Node last;
	int size;
	class Node {
		private Node prev;
		private Object data;
		private Node next;
		public Node(Node prev ,Object data,Node next){
			this.prev=prev;
			this.data=data;
			this.next=next;
		}
	}
	public void add(Object obj) {
			Node tmp=this.last;
			Node newnode=new Node(tmp, obj, null);
			this.last=newnode;
			if(this.first==null){
				this.first=newnode;
			}
			else{
				tmp.next=newnode;
			}
			this.size++;
	}

	public boolean remove(int index) {
		if(!isLinkElement(index)){
			return false;
		}
		Node node=node(index);
		if(node==this.first){
			if(node==this.last){
				node=null;
				this.size--;
				return true;
			}else{
				Node temp=this.first;
				this.first=node.next;
				temp.next=null;
				this.first.prev=null;
				this.size--;
				return true;
			}
		}else if(node==this.last){
			Node temp=this.last;
			this.last=node.prev;
			temp.prev=null;
			this.last.next=null;
			this.size--;
			return true;
		}
		node.next.prev=node.prev;
		node.prev.next=node.next;
		node.next=node.prev=null;
		this.size--;
		return true;
	}

	public boolean set(int index, Object obj) {
		if(index>this.size){
		return false;
		}
		Node node=node(index);
		node.data=obj;
		return true;
	}

	public Object get(int index) {
		if(index>(this.size)){
		return null;
		}
		return node(index).data;
		
	}

	public void printLink() {
		Object[]obj=this.toArray();
		for (int i = 0; i < obj.length; i++) {
			System.out.println(obj[i]);
		}
	}

	public Object[] toArray() {
		Object[]result=new Object[size];
		int i=0;
		for(Node tmp=first;tmp!=null;tmp=tmp.next){
			result[i]=tmp.data;
			i++;
		}
		return result;
	}

	public int getsize() {
				return this.size;
	}

	public void clear() {
		Node tmp=this.first;
		while(tmp != null){
			Node next=tmp.next;
			tmp.next=tmp.prev=null;
			tmp=next;
		}
		this.first=this.last=null;
		this.size=0;
	}

	@SuppressWarnings("unused")
	public int contanins(Object obj) {
		if(obj==null){
			Node tmp=this.first;
			int index=0;
			while(tmp!=null){
				if(tmp==null)
					return index;
			}
			index++;
		
		return -1;
	}
		else {
			Node tmp=this.first;
			int index=0;
		while(tmp!=null){
			if(tmp.data.equals(obj)){
				return index;
			}
			else{
				tmp=tmp.next;
			}
			index++;
		}
		return -1;
		}
	}
	//返回指定位置
	private Node node (int index) {		
		 if(index<(this.size>>1)){
			 Node result=this.first;
			 for(int i=0;i<index;i++){
				 result=result.next;
			 }
			 return result;
		 }
		 Node result=this.last;
		 for(int i=size-1;i>index;i--){
			 result=result.prev;
		 }
		 return result;
	}
	private boolean isLinkElement(int index){
		return index>0&&index<size;
	}
}		

 

   当将接口覆写完成之后,为了方便用户使用,在工厂类中用静态方法返回实例化后的对象。

package 双向链表;

public class Factory {
	private Factory(){		
	}
	public static Link getLinkInstance(){
		return new LinkImpl();
	}
	
}

 

在主方法中取得实例化对象,然后测试功能,一切都正常运行无bug:

 

package 双向链表;

public class Test {

	
	public static void main(String[] args) {
		
		Link link=Factory.getLinkInstance();
		link.add("火车头");
		link.add("车厢1");
		link.add("车厢2");
		link.add("车厢3");
		link.add("火车尾");
		link.remove(4);
		link.printLink();
	}

}

博主用C语言写过链表,用JAVA也写过链表,但两者直接的在博主心里还是有很大差别的,C语言是直接以代码的形式将链表的增删改查一一实现,在使用的时候关于传参等方面要很严格的遵守要求。

JAVA中比较人性化,使用者直接根据内容进行增删改查就好了,不必要过多于在意传参等小细节,更注重了用户的体验。

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值