java 模拟单链表操作
2021-07-12 04:04
标签:for art print int end ++ == ada tee next public class ListNode { } 节点操作: public static ListNode createHeadAddNodeList(int[] dataArray){//头插法插入数据 public static void printHeadAddNodeList(ListNode start){ } public static void deleteNodeByIndex(ListNode startNode,int start,int end){//删除节点 start值输入0 public static void putNode(ListNode startNode,ListNode dataNode,int start,int end){//参数1开始节点 参数2数据节点 } } java 模拟单链表操作 标签:for art print int end ++ == ada tee next 原文地址:http://blog.51cto.com/13919712/2165514
int data;//数据
ListNode next;//指针
public ListNode(int x){data=x;}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
public class ListNodeUtil { //链表
ListNode headNode=new ListNode(0);//创建头结点
headNode.setNext(null);
for(int i=0;i
newNode.setNext(headNode.getNext());//上一节点的指向赋值给下一节点的指向
headNode.setNext(newNode);//头结点指向尾部节点
}
return headNode;
}
public static ListNode createEndAddNodeList(int[] dataArray){//头插法插入数据
ListNode headNode=new ListNode(0);//创建头结点
headNode.setNext(null);
ListNode pointNode=headNode;//指针节点 一直指向尾节点
for(int i=0;i
newNode.setNext(pointNode.getNext());
pointNode.setNext(newNode);
pointNode=newNode;
}
return headNode;
} if(start!=null){
System.out.println(start.getData());
start=start.getNext();
printHeadAddNodeList(start);
}
if(startNode==null){return;}
if(start
startNode=startNode.getNext();
deleteNodeByIndex(startNode,start,end);//递归删除
}else{
startNode.setData(startNode.getNext().getData());
startNode.setNext(startNode.getNext().getNext());
}
} if(startNode==null){return;}
if(start