用JAVA实现双向链表

interface Link{
	void add(Object obj);//尾插
	boolean remove(int index);//删除下标尾index的节点
	boolean set(int index,Object obj);//将下标为index的节点的值改为obj
	Object get(int index);//获取下标为index的节点的data值
	int contains(Object obj);//查找链表中有无obj内容的节点,有返回小标,无返回负一
	void clean();//清空链表
	void printList();//打印链表
	int length();//求链表长度
	Object[] toArray();//将链表转成数组
}
 class LinkImpl implements Link{
	 private Node first;
	 private Node last;
	 private int size=0;
	class Node{
		private Node prev;
		private Object data;
		private Node next;
		public Node(Node prev, Object data, Node next) {
			super();
			this.prev = prev;
			this.data = data;
			this.next = next;
		}
	}
	@Override
	public void add(Object obj) {
		Node tmp=this.last;
		Node newNode = new Node(tmp,obj,null);
		if(null==tmp){
			this.first=newNode;
			this.last=newNode;
			this.size++;
		}else{
			tmp.next=newNode;
			this.last=newNode;
			this.size++;
		}
		
	}
	//判断下标是否合法
	private boolean isLinkElement(int index){
		 if(index>=0 && index<this.size){
			 return true;
		 }
		 System.out.println("没有该元素");
		 return false;
	}
	public boolean remove(int index) {
		if(!(isLinkElement(index))){
			return false;
		}
		Node del=this.first;
		for(int i=0;i<index;i++){
			del=del.next;//此时del已经是索引元素;
		}
		//删除头节点
		if(del==this.first){
			if(del==this.last){
				this.first=this.last=null;
			}else{
				this.first=del.next;
				del.next.prev=null;
				del.next=null;
			}
		}
		//删除尾节点
		else if(del==this.last){
			this.last=del.prev;
			del.prev.next=null;
			del.prev=null;
		}else{
			//删除中间节点
			del.prev.next=del.next;
			del.next.prev=del.prev;
			del.prev=null;
			del.next =null;
		}
		this.size--;
		return true;
	}
	//找到小标为index的节点并返回该节点
	private Node findNode(int index){
		Node curNode=this.first;
		if(index<(this.size>>1))
		{
			for(int i=0;i<index;i++){
				curNode=curNode.next;
			}
			return curNode;
		}
		else{
			curNode=this.last;
			for(int i=this.size-1;i>index;i--){
				curNode=curNode.prev;
			}
			return curNode;
		}
	}
	public boolean set(int index, Object obj) {
		if(!(isLinkElement(index))){
			return false;
		}
		Node set = findNode(index);
		set.data =obj;
		return true;
	}
	@Override
	public Object get(int index) {
		if(!(isLinkElement(index))){
			return null;
		}
		Node get = findNode(index);
		return get.data;
	}
	@Override
	public int contains(Object obj) {
		Node cur = this.first;
		int i=0;
		while(cur!=null){
			if(obj==cur.data)
				return i;
			else{
				cur=cur.next;
				i++;
			}
		}
		return -1;
	}
	@Override
	public void clean() {
		Node del = null;
		while(this.first!=null){
			del =this.first;
			this.first=del.next;
			if(del.next!=null){
				del.next.prev=null;
				del.next=null;
			}else{
				del=null;
			}
			this.size--;
		}
		this.last =null;
	}
	@Override
	public void printList() {
		Object[] arr = this.toArray();
		for(int i=0;i<this.size;i++){
			System.out.println(arr[i]);
		}
	}
	@Override
	public int length() {
		
		return this.size;
	}
	@Override
	public Object[] toArray() {
		Object[] arr=new Object[size];
		int i=0;
		for(Node tmp=this.first;tmp!=null;tmp=tmp.next){
			arr[i++]=tmp.data;
		}
		return arr;
	}
	
}
 class Factory{
	 private Factory(){}
	 public static Link getInstance(){
		 return new LinkImpl();
	 }
 }
public class Test {
	public static void main(String[] args){
		Link link =Factory.getInstance();
		link.add("火车头");
		link.add("车厢1");
		link.add("车厢2");
		link.printList();
		System.out.println(link.length());
		//link.remove(4);
		//link.printList();
		//System.out.println(link.length());
//		link.set(1,"大卡车");
//		link.printList();
//		System.out.println(link.get(0));
//		System.out.println(link.contains("小汽车"));
		link.clean();
		link.printList();
		System.out.println(link.length());
	}
}

阅读更多
个人分类: 编程语言
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭