自己实现LinkedList:

简单的自己实现LinkedList的底层代码:


package testLinkedList;
/**
 * 自己实现LinkedList底层代码
 * @author *
 */

public class MyLinkedList {
	
	private Node first;             //头指针
	private Node last;              //尾指针
	private int size;               

	public int size() {
		return size;
	}
	
	public void add(Object obj) {              //添加元素
		Node n=new Node();
		if(first==null) {                   //注意是指针为空而不是指针中的元素
			n.setprevious(null);
			n.setObj(obj);
			n.setnext(null);
			first=n;
			last=n;
		}else {
			last.setnext(n);
			n.setprevious(last);
			n.setObj(obj);
			n.setnext(null);
			last=n;
		}
		size++;
	}
	
	public void insert(int index,Object obj) {       //指定位置插入元素
		Node nd=first;
		if(index>size||index<0) {          //判断是否越界
			try {
				throw new Exception();
			}catch(Exception e){
				e.printStackTrace();          //越界就抛出异常
			}
		}
		if(first!=null) {
			for(int i=0;i<index;i++) {
				nd=nd.next;
			}
			if(nd!=null) {
				Node n= new Node();
				n.setObj(obj);
				nd.next=n;
				n.previous=nd;
				n.next=nd.next;
				nd.next.previous=n;
			}
		}
		size++;
	}
	
	public Object get(int index) {             //读取元素
		Node nd=first;
		if(index>size||index<0) {          //判断是否越界
			try {
				throw new Exception();
			}catch(Exception e){
				e.printStackTrace();          //越界就抛出异常
			}
		}
		if(first!=null) {
			for(int i=0;i<index;i++) {
				nd=nd.next;
			}
		}
		return nd.obj;
	}
	
	public Object set(int index,Object obj) {
		Node nd=first;
		if(index>size||index<0) {          //判断是否越界
			try {
				throw new Exception();
			}catch(Exception e){
				e.printStackTrace();          //越界就抛出异常
			}
		}
		if(first!=null) {
			for(int i=0;i<index;i++) {
				nd=nd.next;
			}
			nd.obj=obj;
		}
		return nd.obj;
	}
	
	public void remove(int index) {        //删除指定元素
		Node nd=first;
		if(index>size||index<0) {          //判断是否越界
			try {
				throw new Exception();
			}catch(Exception e){
				e.printStackTrace();          //越界就抛出异常
			}
		}
		if(first!=null) {
			for(int i=0;i<index;i++) {
				nd=nd.next;
			}
			nd.next.setprevious(nd.previous);
			nd.previous.setnext(nd.next);
		}
		size--;
	}
	
	
	public static void main(String[] args) {
		MyLinkedList mll=new MyLinkedList();
		mll.add("aaa");               
		mll.add("bbb");
		mll.add("ccc");
	    //	mll.set(1, "ggg");
            //	mll.remove(1);
		mll.insert(2, "hh");
	
		System.out.println(mll.size);
		System.out.println(mll.get(2));
	}
	
}





package testLinkedList;

public class Node {            //定义节点           
	Node previous;         //头结点
	Object obj;            //结点内容
	Node next;             //下一结点
	public Node() {
		
	}
	public Node(Node previous, Object obj, Node next) {
		super();
		this.previous = previous;
		this.obj = obj;
		this.next = next;
	}
	public Node getprevious() {
		return previous;
	}
	public void setprevious(Node previous) {
		this.previous = previous;
	}
	public Object getObj() {
		return obj;
	}
	public void setObj(Object obj) {
		this.obj = obj;
	}
	public Node getnext() {
		return next;
	}
	public void setnext(Node next) {
		this.next = next;
	}
	
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值