先定义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());
}
}