自己写简单的LinkedList

最近复习基础知识,看了下LinkedList源码,决定自己也写写,当然方法不多,只是个简化版。

package 集合;

public class MyLinkList  {
	private transient int size = 0;
	private transient Node  first;
	private transient Node  last;

	public MyLinkList() {
	}

	/**
	 * 返回List的大小
	 * @return size List的大小
	 */
	public int size() {
		return size;
	}

	/**
	 * 添加元素
	 * @param obj 元素
	 */
	public void add(Object obj){
		Node node=new Node();
		node.obj=obj;
		if(first==null){
			node.previous=null;
			node.next=null;
			first=node;
			last=node;
		}else{
			node.previous=last;
			node.next=null;
			last.next=node;
			last=node;
		}
		size++;
	}
	
	/**
	 * 根据索引获取该元素
	 * @param index 索引
	 * @return Object
	 */
	public Object get(int index){
		checkRange(index);
		if(index<(size>>1)){
			Node temp=first;
			for(int i=0;i<index;i++) temp=temp.next;
			return temp.obj;
		}else{
			Node temp=last;
			for(int i=size-1;i>index;i--) temp=temp.previous;
			return temp.obj;
		}
	}
		
	/**
	 * 根据内容删除元素
	 * @param obj 元素内容
	 * @return boolean
	 */
	public boolean remove(Object obj){
		if(obj==null){
			for(Node node=first;node!=null;node=node.next){
				if(node.obj==null){
					RedirectPoint(node);
					return true;
				}					
			}
			return false;
		}else{
			for(Node node=first;node!=null;node=node.next){
				if(obj.equals(node.obj)){
					RedirectPoint(node);
					return true;
				}
			}
			return false;
		}
	}
	
	/**
	 * 根据索引删除元素
	 * @param index 元素的索引
	 * @return boolean 
	 */
	public Object remove(int index){
		checkRange(index);
		if(index<(size>>1)){
			Node node=first;
			for(int i=0;i<index;i++){
				node=node.next;
			}
			Object obj=node.obj;
			RedirectPoint(node);
			return obj;	
		}else{
			Node node=last;
			for(int i=size-1;i>index;i--){
				node=node.previous;
			}
			Object obj=node.obj;
			RedirectPoint(node);
			return obj;			
		}
	}
	
	/**
	 * 查找元素的索引,只返回元素第一次出现的索引,若没有该元素则返回-1;
	 * @param Object 要查找的元素
	 */
	public int find(Object obj){
		int index=0;
		if(obj==null){
			for(Node node=first;node!=null;node=node.next){
				if(node.obj==null) 
				return index;
				index++;
			}
		}else{
			for(Node node=first;node!=null;node=node.next){
				if(obj.equals(node.obj))
				return index;
				index++;
			}
		}
		return -1;
	}
	
	
	//检查索引是否越界
	private void checkRange(int index){
		if(index>=size||index<0){
			try{
				throw new Exception();
			}catch(Exception e){
				System.out.println("数组越界,请检查索引是否超出列表的大小");
				e.printStackTrace();
			}
		}
	}
	
	//删除的内部操作,指针重定向
	private void RedirectPoint(Node node){
		Node up=node.previous;
		Node down=node.next;
		up.next=down;
		down.previous=up;
		size--;
	}
	
	//MylinkList的内部类
	private class Node{
		Object obj;
		Node  previous;
		Node  next;
		Node(){}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值