自己实现LinkedList类

package cn.hxd.collection;
/**
 * 创建一个节点类
 * @author Administrator
 *
 */
public class Node {
	Node previous;
	Object object;
	Node next;
	public Node() {
	}
	
	public Node(Node privious, Object object, Node next) {
		super();
		this.previous = privious;
		this.object = object;
		this.next = next;
	}
	
	public Node getPrivious() {
		return previous;
	}
	public void setPrivious(Node privious) {
		this.previous = privious;
	}
	public Object getObject() {
		return object;
	}
	public void setObject(Object object) {
		this.object = object;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	
	
}

  

package cn.hxd.collection;
/**
 * 自己实现的Linkedlist
 * @author Administrator
 *
 */
public class MyLinkedList {
	private Node first;
	private Node last;
	private int size;
	
	public void add(Object obj) {//在末尾添加元素
		Node n = new Node();
		if(first==null) {
			n.setPrivious(null);
			n.setObject(obj);
			n.setNext(null);
			first = n;
			last = n;
		}else {
			n.setPrivious(last);
			n.setObject(obj);
			n.setNext(null);
			last.setNext(n);
			last = n;
		}
		size++;
	}
	
	public void add(int index,Object obj) {//在指定位置添加元素
		Node temp = node(index);
		Node newNode = new Node();
		newNode.object = obj;
		if(index==0) {//指定第一个位置添加元素
			first = newNode;
			temp.previous = newNode;
			newNode.next = temp;
			size++;
		}else if(index ==size) {//指定最后一个位置添加元素
			add(obj);
		}else {
			Node up = temp.previous;
			up.next = newNode;
			newNode.previous = up;
			newNode.next = temp;
			temp.previous = newNode;
			size++;
		}
	}
	
	public int size() {//返回列表大小
		return size;
	}
	
	public Object get(int index) {//返回指定位置的对象
		rangeCheck(index);
		Node temp = node(index);
		if(temp!=null) {
			return temp.object;
		}
		return null;
		
	}
	
	private void rangeCheck(int index) {
		if(index<0||index>=size) {
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void remove(int index){//删除指定位置的对象元素
		Node temp = node(index);
		if(index==0) {
			temp.next.previous = null;
			first = temp.next;
		}
		else if(temp.next==null) {
			last = temp.previous;
		}else {
			Node up = temp.previous;
			Node down = temp.next;
			up.next = down;
			down.previous = up;
		}
//		if(temp!=null&&index != 0&&index!=size-1){
//			Node up = temp.previous;
//			Node down = temp.next;
//			up.next = down;
//			down.previous = up;
//		}else if(index==0) {
//			temp.next.previous = null;
//			first = temp.next;
//		}else if(temp.next==null) {
//			last = temp.previous;
//		}
		size--; 
	}
	public void clear() {
		
	}
	
	public Node node(int index) {//返回指定位置的对象元素
		Node temp = null;
		if(first!=null) {
			if(index<(size>>1)) {
				temp = first;
			for(int i=0;i<index;i++) {
				temp = temp.next;
			}
			
			}else {
				temp = last;
				for(int i=size-1;i>index;i--) {
					temp = temp.previous;
				}
			}
		}
		return temp;
	}
	
	
	
	public static void main(String[] args) {
		MyLinkedList list= new MyLinkedList();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		//list.add(0,"000");
		//list.add(3, 333);
		//list.remove(2);
		System.out.println(list.size);
		System.out.println(list.get(3));
		for(int i=0;i<list.size;i++) {
			System.out.print(list.get(i)+" ");
		}
	}
}

  

posted @ 2017-06-16 16:17  hou-xudong  阅读(186)  评论(0编辑  收藏  举报