JAVA-在JAVA中实现双向链表的增删查改

在这个链表中,函数具有的功能有:尾插一个新的链表环节,删除指定下标的环节,根据内容查改内容是否存在于链表当中,根据内容返回下标,指定下标的改值,获取指定下标的值,获取链表长度和清空链表。

该代码中重点在于区分java和C中对于链表的定义和节点插入的定义,其余基本差不多,概念也相差无几

package linkList;



interface Link{
	void add(Object obj);
	boolean remove(int index);
	boolean contain(Object obj);
	int indexOf(Object obj);
	boolean set(int index,Object obj);
	Object get(int index);
	int length();
	void clear();
	Object[] toArray();
	void printLink();
	boolean inList(int index);
}

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


class LinkImpl implements Link{
	private Node first;
	private Node last;
	private int size;
	
	private 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;
		}
	}
	@Override
	public void add(Object obj) {
		Node tmp = this.last;
		Node newNode=new Node(tmp,obj,null);
		last=newNode;
		if(tmp == null) {
			this.first=newNode;
		}
		else {
			tmp.next=newNode;
		}
		this.size++;
		return;
	}
	@Override
	public boolean remove(int index) {
		if(!inList(index)) {
			return false;
		}
		Node curNode=toNode(index);
		if(curNode == first) {
			if(curNode == last) {
				curNode.data=null;
				curNode=null;
				this.size--;
			}
			first=curNode.next;
			first.prev=null;
			curNode.next=null;
			curNode.data=null;
			this.size--;
			return true;
		}
		if(curNode == last) {
			last=curNode.prev;
			last.next=null;
			curNode.prev=null;
			curNode.data=null;
			this.size--;
			return true;
		}
		else {
			curNode.prev.next=curNode.next;
			curNode.next.prev=curNode.prev;
			curNode.prev=null;
			curNode.next=null;
			curNode.data=null;
			this.size--;
			return true;
		}
	}
	@Override
	public boolean contain(Object obj) {
		for(Node curNode=first;curNode != null;curNode=curNode.next) {
			if(curNode.data == obj) {
				return true;
			}
		}
		return false;
	}
	@Override
	public int indexOf(Object obj) {
		int i=0;
		for(Node curNode=first;curNode !=null;curNode=curNode.next) {
			if(curNode.data == obj) {
				return i;
			}
			i++;
		}
		return -1;
	}
	@Override
	public boolean set(int index, Object obj) {
		if(!inList(index)) {
			return false;
		}
		Node curNode=toNode(index);
		curNode.data=obj;
		return true;
	}
	@Override
	public Object get(int index) {
		if(!inList(index)) {
			return null;
		}
		Object result;
		Node curNode=toNode(index);
		result=curNode.data;
		return result;
	}
	@Override
	public int length() {
		return size;
	}
	@Override
	public void clear() {
		Node curNode=first;
		for(;curNode != null;) {
			Node tmp=curNode.next;
			curNode.next=null;
			curNode.prev=null;
			curNode.data=null;
			curNode=tmp;
		}
		this.last=this.first=null;
		this.size=0;
		return;
	}
	@Override
	public Object[] toArray() {
		Object[] tar = new Object[size];
		int i=0;
		for(Node curNode=first;curNode != null;curNode=curNode.next) {
			tar[i]=curNode.data;	
			i++;
		}
		return tar;
	}
	@Override
	public void printLink() {
		Object[] result = this.toArray();
		for(Object object : result) {
			System.out.println(object);
		}
	}
	@Override
	public boolean inList(int index) {
		if(index >=0 && index <=size) {
			return true;
		}
		return false;
	}
	private Node toNode(int index) {
		if(!inList(index)) {
			return first ;
		}
		if(index <(size>>1)) {
			int i=0;
			Node curNode=first;
			for(;i!=index;curNode=curNode.next,i++) {}
			return curNode;
		}
		else {
			int i=size;
			Node curNode=last;
			for(;i!=index;curNode=curNode.prev) {i--;}
			return curNode;
			}
		}
	}

public class Test {
	public static void main(String[] args) {
	Link link=Factory.getlinkInstance();
	link.add("一");
	link.add("二");
	link.add("三");
	link.add("四");
	link.set(1, "哈");
	System.out.println(link.indexOf("一"));
	System.out.println(link.contain("一"));
	System.out.println(link.length());
	link.remove(3);
	link.printLink();
	}
}


阅读更多
个人分类: 作业
上一篇JAVA-异常处理及toString的覆写
下一篇JAVA-泛型类
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭