标签:tla on() 删除 int except 测试 void alt pre
从这个月开始得准备春招的东西,所以打算重新学习数据结构与算法,以后的博客就以这个为主。
今天是线性结构中的双向链表。
代码实现与测试:
DoubleLinkNode:
package linear.doublelink;
/**
* @Description: 链表节点结构体
* @Auther: Weijie.Zhang
* @Date: 2018/11/1 17:03
*/
public class DoubleLinkNode<T> {
public T value;
public DoubleLinkNode pre;
public DoubleLinkNode next;
public DoubleLinkNode(T value) {
this.value = value;
}
public DoubleLinkNode(T value, DoubleLinkNode pre, DoubleLinkNode next) {
this.value = value;
this.pre = pre;
this.next = next;
}
}
DoubleLink:
package linear.doublelink;
/**
* @Description: 链表实现
* @Auther: Weijie.Zhang
* @Date: 2018/11/1 17:04
*/
public class DoubleLink<T> {
//表头
private DoubleLinkNode<T> head;
//节点个数
private int count;
public DoubleLink() {
head = new DoubleLinkNode<T>(null, null, null);
head.pre = head.next = head;
count = 0;
}
//返回节点个数
public int size() {
return count;
}
//返回链表是否为空
public Boolean isEmpty() {
if (count == 0) {
return true;
} else {
return false;
}
}
//获取第index位置上的节点
public DoubleLinkNode<T> get(int index) {
if (index < 0 || index >= count) {
System.out.println("索引超出范围");
throw new IndexOutOfBoundsException();
}
//正向查找
if (index <= count / 2) {
DoubleLinkNode doubleLinkNode = head.next;
for (int i = 0; i < index; i++)
doubleLinkNode = doubleLinkNode.next;
return doubleLinkNode;
}
//反向查找
DoubleLinkNode doubleLinkNode = head.pre;
int pos = count - index - 1;
for (int i = 0; i < pos; i++)
doubleLinkNode = doubleLinkNode.pre;
return doubleLinkNode;
}
//获取第一个节点的值
public T getFirstValue() {
return get(0).value;
}
//获取最后一个节点的值
public T getLastValue() {
return get(count - 1).value;
}
//获取第index节点的值
public T getIndexValue(int index) {
return get(index).value;
}
//将节点插入到index位置
public void insert(int index, DoubleLinkNode<T> doubleLinkNode) {
if (index < 0 || index > count) {
System.out.println("超出索引范围");
throw new IndexOutOfBoundsException();
}
if (index == 0) {
head.next.pre = doubleLinkNode;
head.next = doubleLinkNode;
count++;
return;
}
if (index == count) {
doubleLinkNode.next = head;
doubleLinkNode.pre = head.pre;
head.pre.next = doubleLinkNode;
head.pre = doubleLinkNode;
count++;
return;
}
for (int i = 0; i < index; i++) {
DoubleLinkNode linkNode = get(index);
doubleLinkNode.pre = linkNode.pre;
doubleLinkNode.next = linkNode;
linkNode.pre.next = doubleLinkNode;
linkNode.pre = doubleLinkNode;
count++;
return;
}
}
//删除index上的节点
public void delete(int index) {
DoubleLinkNode doubleLinkNode = get(index);
doubleLinkNode.pre.next = doubleLinkNode.next;
doubleLinkNode.next.pre = doubleLinkNode.pre;
count--;
return;
}
//修改index节点上的值
public void change(int index, T value) {
get(index).value = value;
return;
}
}
MainTest:
package linear.doublelink;
/**
* @Description: 测试
* @Auther: Weijie.Zhang
* @Date: 2018/11/1 17:57
*/
public class MainTest {
public static void main(String[] args) {
DoubleLink<Integer> doubleLink = new DoubleLink<>();
int i;
for (i = 0; i < 10; i++) {
DoubleLinkNode<Integer> doubleLinkNode = new DoubleLinkNode<>(i + 22);
doubleLink.insert(i, doubleLinkNode);
}
System.out.println(doubleLink.getFirstValue());
System.out.println(doubleLink.getLastValue());
System.out.println(doubleLink.getIndexValue(7));
doubleLink.delete(4);
doubleLink.insert(9,new DoubleLinkNode<Integer>(10));
for (int j = 0; j < doubleLink.size(); j++) {
System.out.print(doubleLink.get(j).value + " ");
}
}
}
标签:tla on() 删除 int except 测试 void alt pre
原文地址:https://www.cnblogs.com/qugemingzihaonan13/p/9896591.html