[Java算法分析与设计]单向链表(List)的实现和应用

先定义List接口

package com.chen.arithmetic_test.list_test;

/**
 * Created by ChenMP on 2017/7/3.
 */
public interface List {
    //获得长度
    public int size();
    //插入元素
    public boolean insert(int index, Object o) throws Exception;
    //新增元素
    public boolean add(Object o) throws Exception;
    //删除元素
    public boolean remove(int index) throws Exception;
    //获取元素
    public Object get(int index) throws Exception;
    //判断线性表是否为空
    public boolean isEmpty();
}

编写节点类Node

package com.chen.arithmetic_test.list_test;

/**
 * Created by ChenMP on 2017/7/4.
 */
public class Node {

    private Object nodeData;
    private Node nextNode;

    public Node() {
        super();
    }

    public Node(Object nodeData) {
        this.nodeData = nodeData;
    }

    public Object getNodeData() {
        return nodeData;
    }

    public void setNodeData(Object nodeData) {
        this.nodeData = nodeData;
    }

    public Node getNextNode() {
        return nextNode;
    }

    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }
}

编写LinkList的实现类

package com.chen.arithmetic_test.list_test;

/**
 * Created by ChenMP on 2017/7/4.
 */
public class LinkList implements List {
    private Node fristNode;//开始节点
    private Node lastNode;//结束节点
    private int size;//List长度
    private boolean isFixed;//是否限定List长度
    private int fixedLength;//List定长

    public LinkList() {
        this.size = 0;
        this.fristNode = null;//第一次成功插入时定义
        this.lastNode = null;
        this.isFixed = false;//不限定List长度
        this.fixedLength = -1;//不限定长度
    }

    public LinkList(int length) {
        this.fristNode = null;//第一次成功插入时定义
        this.lastNode = null;
        this.size = 0;
        this.isFixed = true;//限定List长度
        this.fixedLength = length;//设置限定长度
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean insert(int index, Object o) throws Exception {
        if(index > size)
            throw new Exception("IndexOutOfBoundsException");

        if (index == size && this.isFixed && size>=this.fixedLength)
            throw new Exception("IndexOutOfBoundsException");

        Node previousNode = null; //遍历节点,用于存放更新节点的前一个节点
        Node currentNode = this.fristNode; //遍历节点,用于存放当前节点
        for (int i=1; i<=index; i++) {
            if (null == currentNode) //插入节点前有空节点
                throw new Exception("IndexOutOfBoundsException");

            previousNode = currentNode;
            currentNode = previousNode.getNextNode();
        }

        if (null == currentNode) { //把节点插入到最后
            currentNode = new Node(o);

            if (null != previousNode) { //fristNode不为空
                previousNode.setNextNode(currentNode);
            } else { //fristNode不为空,更新fristNode
                this.fristNode = currentNode;
            }

            this.lastNode = currentNode;
            size++;
        } else { //节点不为空,取代原节点数据
            currentNode.setNodeData(o);
        }

        return true;
    }

    @Override
    public boolean add(Object o) throws Exception {
        if (this.isFixed && size == fixedLength)
            throw new Exception("IndexOutOfBoundsException");

        Node currentNode = new Node(o);
        if (0 == size) {//List中插入第一个元素
            this.fristNode = currentNode;
            this.lastNode = currentNode;
            size++;
        } else {
            this.lastNode.setNextNode(currentNode);
            this.lastNode = currentNode;
            size++;
        }

        return true;
    }

    @Override
    public boolean remove(int index) throws Exception {
        if(index < 0 || index >= size)
            throw new Exception("IndexOutOfBoundsException");

        Node previousNode = null; //遍历节点,用于存放删除节点的前一个节点
        Node currentNode = this.fristNode; //遍历节点,用于存放删除节点
        for (int i=1; i<=index; i++) {
            if (null == currentNode) //删除节点前有空节点
                throw new Exception("IndexOutOfBoundsException");

            previousNode = currentNode;
            currentNode = previousNode.getNextNode();
        }

        previousNode.setNextNode(currentNode.getNextNode());
        currentNode = null;
        size--;

        return true;
    }

    @Override
    public Object get(int index) throws Exception {
        if(index < 0 || index >= size)
            throw new Exception("IndexOutOfBoundsException");

        Node currentNode = this.fristNode; //遍历节点,用于存放查询节点
        for (int i=1; i<=index; i++) {
            if (null == currentNode) //删除节点前有空节点
                throw new Exception("IndexOutOfBoundsException");

            currentNode = currentNode.getNextNode();
        }

        return currentNode.getNodeData();
    }

    @Override
    public boolean isEmpty() {
        return this.size>0?false:true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node currentNode = this.fristNode; //遍历节点,用于存放查询节点

        while (null != currentNode) {
            sb.append(currentNode.getNodeData()).append(",");
            currentNode = currentNode.getNextNode();
        }
        return sb.toString();
    }
}

编写测试代码

package com.chen.arithmetic_test.list_test;

import java.util.LinkedList;

/**
 * Created by ChenMP on 2017/7/3.
 */
public class TestList {

    public static void main(String[] args) throws Exception {
        List list = new LinkList(3);
        list.insert(0,0);
//        list.add(0);
        list.add(1);
        list.add(2);
//        list.add(3);
        System.out.print("测试定长list: " + list.toString() + "|| list长度为: " + list.size());

        System.out.println();
        List list2 = new SequenceList();
        list2.add(0);
        list2.add(1);
        list2.add(2);
        list2.add(3);
        System.out.print("测试不定长list: " + list2.toString() + "|| list长度为: " + list2.size());


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值