简单实现LinkedList(双向链表)

看源码和视频自己写了写,当然和源码差了n个数量级. 也算复习了下链表

SetLinkedList类

package cn.hjy.collection;

import java.util.List;
/** 
 * @author 543363559@qq.com
 * @date 2017年4月30日 下午4:47:34 
 */
public class SetLinkedList {
    private Node first;
    private Node last;

    private int size;
/**
 * 
 * @Description: 添加结点 建first or 往last.next添加
 * @param @param obj
 * @param @return   
 * @return boolean
 */
    public boolean 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结点后增加新的结点
            n.setPrevious(last);
            n.setObj(obj);
            n.setNext(null);

            last.setNext(n);

            last = n;
        }
        size++;
        return true;
    }
/**
 * 
 * @Description: 返回链表大小
 * @param @return   
 * @return int
 */
    public int size(){
        return size;
    }
/**
 * 
 * @Description: 删除指定索引位置对象
 * @param @param index   
 * @return void
 */
    public void remove(int index){
        //得到结点
        Node temp = node(index);

        if (temp != null) {
            Node up = temp.getPrevious();
            Node down = temp.getNext();
            up.setNext(down);
            if (down != null) {
                down.setPrevious(up);
            }
        } 
        size--;
    }
/**
 * 
 * @Description: 获得想要位置的结点
 * @param @param index
 * @param @return   
 * @return Node
 */
    public Node node(int index) {
        Node temp = null;
        if (first != null) {
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.getNext();
            }
        }
        return temp;
    }
/**
 * 
 * @Description: 得到index的obj值
 * @param @param index
 * @param @return   
 * @return Object
 */
    public Object get(int index){
        rangeCheck(index); //越界处理

        Node temp = node(index);
        if (temp != null) {
            return temp.getObj();           
        } else {
            return null;
        }
    }
/**
 * 
 * @Description: 在链表index位置添加结点
 * @param @param index
 * @param @param obj   
 * @return void
 */
    public void add(int index,Object obj) {
        Node temp = node(index);

        Node newNode = new Node();
        newNode.setObj(obj);

        if (temp != null) {
            Node up = temp.getPrevious();
            up.setNext(newNode);
            newNode.setPrevious(up);

            newNode.setNext(temp);
            temp.setPrevious(newNode);
        }
        size++;
    }
/**
 * 
 * @Description: 修改链表中 index的obj
 * @param @param index
 * @param @param obj   
 * @return void
 */
    public void set(int index,Object obj) {
        Node temp = node(index);
        if (temp != null) {
            temp.setObj(obj);
        }
    }
/**
 * 
 * @Description: 判断index是否超出链表范围,超出则抛出异常
 * @param @param index   
 * @return void
 */
    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
/**
 * 
 * @Description: 遍历显示
 * @param    
 * @return void
 */
    public void show() {
        Node temp = new Node();
        if (first != null) {
            temp = first;
            while (temp != null) {
                System.out.print(temp.getObj() + " ");
                temp = temp.getNext();
            }
        }

    }
    public SetLinkedList() { 

    }

    public static void main(String[] args) {
        SetLinkedList list = new SetLinkedList();
        list.add("aaa");
        list.add("bbb");

        list.add(1, "www");
        System.out.println(list.get(1)); //www
        list.set(1, "qqq");
        System.out.println(list.get(1)); //qqq
        list.remove(1);
        System.out.println(list.get(1)); //bbb
        list.show(); //aaa bbb
        System.out.println(list.size()); //2
    }

}

Node类

package cn.hjy.collection;

class Node {
    private Node previous;
    private Object obj;
    private Node next;

    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、付费专栏及课程。

余额充值