关闭

java (双)链表实现

248人阅读 评论(0) 收藏 举报

 

package hb.struct;


/**
 * 双向链表
 * @author huangbiao
 * @date 2013-9-20
 */
public class MyList {

	public static void main(String[] args) {
		MyList mylist = new MyList();
		mylist.addFirst("huangbiao_first1");
		mylist.addFirst("huangbiao_first2");
		mylist.addLast("huangbiao_last1");
		mylist.addLast("huangbiao_last2");
		
		System.out.println("-------------add(int,Object)-------------------");
		mylist.add(1, "add(int,Object)");
		mylist.addBefor("addBefore", mylist.getNode(2));
		
		for(int i=0;i<mylist.size;i++){
			System.out.println(mylist.getNode(i).getItem());
		}
		
		System.out.println("-------------getFirst()-------------------");
		System.out.println(mylist.getFirst().getItem());
		System.out.println(mylist.getLast().getItem());
		
		System.out.println("-------------remove(Node)-------------------");
		
		try {
			mylist.remove(mylist.getNode(2));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(int i=0;i<mylist.size;i++){
			System.out.println(mylist.getNode(i).getItem());
		}
		
		System.out.println("-------------remove(int)-------------------");
		
		try {
			mylist.remove(2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(int i=0;i<mylist.size;i++){
			System.out.println(mylist.getNode(i).getItem());
		}
		
		System.out.println("------------removeLast()--------------------");
		
		mylist.removeLast();
		
		for(int i=0;i<mylist.size;i++){
			System.out.println(mylist.getNode(i).getItem());
		}
		
	}

	private Node head;
	private Node tail;
	private int size;

	MyList() {
		head = new Node();
		tail = new Node();
		head.setNext(tail);
		tail.setPre(head);
		size = 0;
	}
	
	public Node getNode(int i){
		Node result = head;
		if(0<= i && i < size){
			for (int j = 0; j <= i; j++)
				result = result.next;
		}else{
			return result;
		}
		return result;
	}
	
	public Node getFirst(){
		Node result = null;
		if(size > 0){
			result = head.next;
		}
		return result;
	}
	
	public Node getLast(){
		Node result = null;
		if(size > 0){
			result = tail.pre;
		}
		return result;
	}
	
	private boolean isPositionIndex(int i){
		return i>=0 && i<=size;
	}
	
	private void checkPositionIndex(int i){
		if(!isPositionIndex(i)){
			throw new IndexOutOfBoundsException(outOfBoundsMsg(i));
		}else{
			return ;
		}
	}
	
	private String outOfBoundsMsg(int i) {
		return (new StringBuilder()).append("Index: ").append(i).append(", Size: ").append(size).toString();
	}

	public void addFirst(Object item) {
		Node n = new Node(item, head, head.getNext());
		head.getNext().setPre(n);
		head.setNext(n);
		size++;
	}
	
	public int size(){
		return this.size;
	}
	
	public void add(int index,Object item){
		checkPositionIndex(index);
		if (index == size)
			addLast(item);
		else
			addBefor(item, getNode(index));
	}
	
	public void addBefor(Object obj,Node beforeNode){
		Node node2 = beforeNode.pre;
		Node node = new Node(obj,node2,beforeNode);
		//
		beforeNode.pre = node;
		node2.next = node;
		size++;
	}
	

	public void addLast(Object item) {
		Node n = new Node(item, tail.getPre(), tail);
		tail.getPre().setNext(n);
		tail.setPre(n);
		size++;
	}

	public void remove(Node node) throws Exception {
		Node n = check(node);
		n.getPre().setNext(n.getNext());
		n.getNext().setPre(n.getPre());
		size--;
	}
	
	public void remove(int index) throws Exception{
		if(index<=size){
			Node target = head;
			for(int i=0;i<this.size;i++){
				target = target.next;
			}
			this.remove(target);
		}
	}
	
	public void removeLast(){
		if(size>0){
			Node last = tail.pre;
			last.pre.setNext(last.next);
			last.next.setPre(last.pre);
			size--;
		}
	}

	protected Node check(Node node) throws Exception {
		if (node == null) {
			throw new Exception("节点为空!");
		}
		if (node == head) {
			throw new Exception("节点不能指向头节点!");
		}
		if (node == tail) {
			throw new Exception("节点不能指向尾节点!");
		}
		return node;
	}

	/**
	 * 定义了一个类为node,用来存储数据
	 * @author Administrator
	 */
	private class Node {
		Object item;
		Node pre;
		Node next;

		Node(Object item, Node pre, Node next) {
			this.item = item;
			this.pre = pre;
			this.next = next;
		}

		Node(Object item) {
			this(item, null, null);
		}

		Node() {
			this(null, null, null);
		}

		public Object getItem() {
			return item;
		}

		public void setItem(Object item) {
			this.item = item;
		}

		public Node getPre() {
			return pre;
		}

		public void setPre(Node pre) {
			this.pre = pre;
		}

		public Node getNext() {
			return next;
		}

		public void setNext(Node next) {
			this.next = next;
		}

	}
}

 

打印结果:

-------------add(int,Object)-------------------

huangbiao_first2

add(int,Object)

addBefore

huangbiao_first1

huangbiao_last1

huangbiao_last2

-------------getFirst()-------------------

huangbiao_first2

huangbiao_last2

-------------remove(Node)-------------------

huangbiao_first2

add(int,Object)

huangbiao_first1

huangbiao_last1

huangbiao_last2

-------------remove(int)-------------------

huangbiao_first2

add(int,Object)

huangbiao_first1

huangbiao_last1

------------removeLast()--------------------

huangbiao_first2

add(int,Object)

huangbiao_first1

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:309428次
    • 积分:8708
    • 等级:
    • 排名:第2263名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论