package train;
import java.util.Arrays;
public class Demo66 {
public static void main(String[] args) {
CreateLink nodes = new CreateLink("jemmy", "jenifer", "hellen", "waylen");
nodes.addNode(0,"kobe");
nodes.addNode(5,"trump");
nodes.delNode(0);
nodes.handleAndErg(SEQUENCE.POSITIVE);
}
public static class CreateLink{
public Node[] nodes;
public Node first;
public Node last;
public void linking(){ //用来排列
this.first = this.nodes[0];
this.last = this.nodes[this.nodes.length-1];
for (int i = 0; i < this.nodes.length-1; i++) { //正向联结
this.nodes[i].next=this.nodes[i+1];
if(i==this.nodes.length-2)nodes[i+1].next=null;//变动的情况需要重置尾联结
}
for (int i = this.nodes.length-1; i >0 ; i--) {//逆向联结
this.nodes[i].previous=this.nodes[i-1];
if(i==1)nodes[i-1].previous=null;//变动的情况需要重置首联结
}
}
public CreateLink(Object ... link) {//函数重载
Node[] nodes = new Node[link.length];
for (int i = 0; i < link.length; i++) {
nodes[i] = new Node(link[i]);//逐个放在nodes数组里
}
this.nodes=nodes;
}
public void handleAndErg(SEQUENCE sequence ){//正向遍历和逆向遍历
linking();
if(sequence==Demo66.SEQUENCE.POSITIVE){
while (true){
if(this.first ==null)break;
System.out.println(this.first);
this.first=this.first.next;//联结右移
}
}
if(sequence== Demo66.SEQUENCE.NEGATIVE){
while (true){
if(last ==null)break;
System.out.println(last);
last=last.previous;//联结左移
}
}
}
public void addNode(int index,Object singleData){
this.nodes=Arrays.copyOf(this.nodes,nodes.length+1);//数组先扩容一位
Node data = new Node(singleData);//装箱成Node
if (index < 0 ||index >= nodes.length){
System.out.println("索引不在合理范围");
System.exit(0);//正常退出程序
}
else if(index==0){//放在最前
for (int i = nodes.length-2; i >=0 ; i--) {
nodes[i+1]=nodes[i];
}
this.nodes[0]=data;
nodes[0].next=this.nodes[1];
}
else if(index==nodes.length-1)//放在最後
{
nodes[nodes.length-1]=data;
nodes[nodes.length-1].previous=nodes[nodes.length-2];
nodes[nodes.length-2].next=nodes[nodes.length-1];
}
else {//中间范围插入
for (int i = nodes.length-2; i >=index ; i--) {
nodes[i+1]=nodes[i];
}
nodes[index]=data;
nodes[index].next=nodes[index+1];
nodes[index+1].previous=nodes[index];
nodes[index].previous=nodes[index-1];
nodes[index-1].next=nodes[index];
}
}
public void delNode(int index) {
if (index < 0 ||index > nodes.length-1){
System.out.println("索引不在合理范围");
System.exit(0);//正常退出程序
}
else if (index==0){
this.nodes = Arrays.copyOfRange(this.nodes, 1, this.nodes.length );
}
else if (index==nodes.length-1){
this.nodes = Arrays.copyOfRange(this.nodes, 0, this.nodes.length - 1);
}
else {
Node[] arrFront = Arrays.copyOfRange(this.nodes,0,index);//注意是前闭後开
Node[] arrBack = Arrays.copyOfRange(this.nodes, index + 1, this.nodes.length);
Node[] finalArr = new Node[arrFront.length+arrBack.length];
System.arraycopy(arrFront,0,finalArr,0,arrFront.length);
System.arraycopy(arrBack,0,finalArr,arrFront.length,arrBack.length);
this.nodes=finalArr;
}
}
}
public enum SEQUENCE{//枚举类 用于排序
POSITIVE,NEGATIVE
}
public static class Node{//node类
public Object item;
public Node next;
public Node previous;
public Node(Object item) {
this.item = item;
}
@Override
public String toString() {
return "item=" + item;
}
}
}
item=jemmy
item=jenifer
item=hellen
item=waylen
item=trump