自己实现LinkedList集合add,get,remove

自己实现LinkedList集合add,get,remove

  • Node类

    public class Node {

    ....public Node previous;

    ....public Object obj;

    ....public 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;

    ....}

    }

    MyLinkedList类

    public class MyLinkedList {

        private Node first;

        private Node last;

        private int size=0;

       

        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

        ....{

        ........n.setPrevious(last);

        ........n.setObj(obj);

        ........n.setNext(null);

        ........

        ........last.setNext(n);

        ........last = n;

        ....}

        ....size++;

        }

        public Object get(int index)

        {

    ........Node ntemp = node(index);

        ....return ntemp.getObj();

        }

       

        public void remove(int index)

        {

        ....Node ntemp = node(index);

        ....if(ntemp != null)

        ....{

        ........Node up = ntemp.getPrevious();

        ........Node down = ntemp.getNext();

        ........up.setNext(down);

        ........down.setPrevious(up);

        ....}

        }

       

        public Node node(int index)

        {

        ....Node nodeTemp = null;

        ....if(first != null)

        ....{

        ........nodeTemp = first;

        ........for(int i=0 ; i<index ; i++)

        ........{

        ............nodeTemp = nodeTemp.getNext();

        ........}

        ....}

        ....return nodeTemp;

        }

       

        public int size()

        {

        ....return size;

        }

       

        public static void main(String[] args) {

        ....MyLinkedList linkList = new MyLinkedList();

        ....linkList.add("aa");

        ....linkList.add("bb");

        ....linkList.add("cc");

        ....linkList.remove(1);

        ....System.out.println("0:"+linkList.get(1));

        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值