Java实现双向链表

package Link;

interface Link{
	void add(Object obj);    //添加节点
	boolean remove(int index);    //删除下标为index的节点
	boolean contains(Object obj);    //判断链表是否包含值为obj的节点
	int indexOf(Object obj);    //查找值为obj的节点的下标
	boolean set(int index,Object obj);    //将下标为index的节点的值改为obj
	Object get(int index);    //获取下标为index的节点的值
	int length();    //获取链表的长度
	void clear();    //清空链表
	Object[] toArray();    //将链表中节点的值转为数组
	void printLink();    //打印链表
}
class Factory{
	private Factory() {}
	public static Link getLinkInstance() {
		return new LinkImpl();
	}
}
class LinkImpl implements Link{
	private int size = 0;
	private Node first;
	private Node last;
	public class Node{
		private Node prev;
		private Object data;
		private Node next;
		private Node(Node prev,Object data,Node next) {
			this.prev = prev;
			this.data = data;
			this.next = next;
		}
	}
	public void add(Object obj) {
		Node temp = this.last;
		Node newNode = new Node(temp,obj,null);
		this.last = newNode;
		if(temp == null) {
			this.first = newNode;
		}else {
			temp.next = newNode;
		}
		this.size++;
	}
	public int length() {
		return this.size;
	}
	
	public int indexOf(Object obj) {
		int index = 0;
		if(obj == null) {
			for(Node temp = this.first;temp != null;temp=temp.next) {
				if(temp.data == null) {
					return index;
				}
				index++;
			}
		}else {
			for(Node temp = this.first;temp != null;temp = temp.next) {
				if(temp.data.equals(obj)) {
					return index;
				}
				index++;
			}
		}
		return -1;
	}
	
	private boolean isElementIndex(int index) {
		return index>=0 && index<=size;
	}
	
	public void clear() {
		Node temp = this.first;
		for(temp = this.first;temp != null;) {
			Node x = temp.next;
			temp.prev = null;
			temp.data = null;
			temp.next = null;
			temp = x;
			this.size--;
		}
		this.first = this.last = null;
	}
	public Object get(int index) {
		if(!isElementIndex(index)) {
			return false;
		}
		int i=0;
		for(Node temp = this.first;temp != null;temp = temp.next) {
			if(i == index) {
				return temp.data;
			}
			i++;
		}
		return false;
	}
	public boolean contains(Object obj) {
		for (Node temp = this.first;temp != null;temp = temp.next) {
			if(temp.data == obj) {
				return true;
			}
		}
		return false;
	}
	public boolean remove(int index) {
		
		Node temp = this.first;
		if(0 == index) {
			this.first = temp.next;
			temp = null;
			this.size--;
			return true;
		}
		
		Node temp2 = this.last;
		if(this.size-1 == index) {
			this.last = temp2.prev;
			this.last.next = null;
			temp2 = null;
			this.size--;
			return true;
		}
		
		int i = 1;
		Node temp3 = this.first.next;
		for(;temp3 != null;) {
			Node x = temp3.next;
			if(i == index){
				temp3.next.prev = temp3.prev;
				temp3.prev.next = temp3.next;
				temp3 = null;
				this.size--;
				return true;
			}
			temp3 = x;
			i++;
		}
		
		return false;
	}
	
	public boolean set(int index,Object obj) {
		Node temp = this.first;
		int i = 0;
		for(;temp != null;temp =temp.next) {
			if(i == index) {
				temp.data = obj;
				return true;
			}
			i++;
		}
		return false;
	}
	public Object[] toArray() {
		Object[] result = new Object[size];
		int i = 0;
		for(Node temp = this.first;temp != null;temp = temp.next) {
			result[i++] = temp.data;
		}
		return result;
	}
	public void printLink() {
		Object[] result = this.toArray();
		for(int i = 0;i < result.length;i++) {
			System.out.println(result[i]);
		}
	}
}

public class linkLink {
	public static void main(String[] args) {
		Link link = Factory.getLinkInstance();
		link.add("火车头");
		link.add("车厢1");
		link.add("车厢2");
		link.add("火车尾");
//		link.printLink();
//		
//		link.remove(2);
//		link.printLink();
		
//	        link.set(3, "车厢3");
//	        link.printLink();
		
//		System.out.println(link.indexOf("火车5"));
//		System.out.println(link.indexOf("火车尾"));
		
//		link.clear();
//		link.printLink();
		
		System.out.println(link.contains("火车头"));
		System.out.println(link.contains("车厢5"));
	
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值