package 数据结构.链表.双链表;
public class DoubleNode {
private DoubleNode precursor;
private int date;
private DoubleNode next;
public DoubleNode(int date) {
this.date = date;
}
public DoubleNode(DoubleNode precursor, int date, DoubleNode next) {
this.precursor = precursor;
this.date = date;
this.next = next;
}
public DoubleNode getPrecursor() {
return precursor;
}
public int getDate() {
return date;
}
public DoubleNode getNext() {
return next;
}
public void setPrecursor(DoubleNode precursor) {
this.precursor = precursor;
}
public void setDate(int date) {
this.date = date;
}
public void setNext(DoubleNode next) {
this.next = next;
}
}
----------------
package 数据结构.链表.双链表;
/**
* Author : PingLu
* TimeBegin : 2022/03/06
* 双链表 实现增删改操作 CURD
* DoneTime 2022/03/07
*/
public class DoubleLink {
private DoubleNode head;
int size = 0;
public DoubleLink(){
//默认构造一个带有头系结点的双链表
head = new DoubleNode(null,0,null);
}
public DoubleNode getHead() {
return head;
}
public void setHead(DoubleNode head) {
this.head = head;
}
public int getSize() {
return size;
}
//判断链表是否为空
public boolean LinkIsEmpty(){
return head.getNext() == null;
}
//遍历输出链表
public void printDoubleLink(){
DoubleNode pMove = head;
System.out.println("当前链表为:"+head.getDate()+"\t大小为:"+this.size);
while (pMove.getNext() != null){
System.out.print(pMove.getNext().getDate()+"<-->");
pMove = pMove.getNext();
}
System.out.println();
}
//增加结点 效率高
public void addNode(DoubleNode node){
//如果链表中只有头节点结点直接加到头结点之后,否认需要找到链表的最后一个元素加到最后一个元素的后面
if(head.getNext() == null){
head.setNext(node);
node.setPrecursor(head);
node.setNext(null);
size ++;
return;
}
DoubleNode pMove = head;
while (pMove.getNext() != null){
pMove = pMove.getNext();
}
pMove.setNext(node);
node.setPrecursor(pMove);
node.setNext(null);
size ++;
}
//删除结点 效率高
public void delNode(int date){
if(LinkIsEmpty()){
System.out.println("Link is Empty!");
return;
}
if(!findNode(date)){
return;
}
DoubleNode tempNode = head;
while(tempNode.getDate() != date){
tempNode = tempNode.getNext();
}
//删除操作
DoubleNode f = tempNode.getPrecursor();
DoubleNode t = tempNode.getNext();
f.setNext(t);
t.setPrecursor(f);
size --;
System.out.println("Delete Node"+date+"Successful!");
}
//查找结点
// public void findNode(int date){
// if(LinkIsEmpty()){
// System.out.println("Link is Empty!");
// return;
// }
// DoubleNode pMove = head;
// while(pMove.getNext() != null){
// if(pMove.getDate() == date){
// System.out.println(pMove.getDate()+"Find!!!");
// return;
// }
// pMove = pMove.getNext();
// }
// System.out.println("Not Find This Node!");
// }
//查找结点
public boolean findNode(int date){
DoubleNode tmpNode = head;
if(LinkIsEmpty()){
System.out.println("Double Link Is Empty!");
return false;
}
while(tmpNode.getDate() != date){
tmpNode = tmpNode.getNext();
if(tmpNode.getNext() == null){
System.out.println("No This DoubleNode!");
return false;
}
}
System.out.println(tmpNode.getDate());
return true;
}
//修改结点 修改结点前需要判断结点是否存在
public void modifyNode(int date, int updateDate){
if(LinkIsEmpty()){
System.out.println("Link is Empty!");
return;
}
if(!findNode(date)){
return;
}
DoubleNode tempNode = head;
while(tempNode.getDate() != date){
tempNode = tempNode.getNext();
}
//修改操作
tempNode.setDate(updateDate);
System.out.println("MetaDate:"+date+"\tUpdate:"+updateDate+"Successful!");
}
}
------
package 数据结构.链表.双链表;
public class DoubleLinkTest {
public static void main(String[] args) {
DoubleNode node1 = new DoubleNode(2);
DoubleNode node2 = new DoubleNode(34);
DoubleNode node3 = new DoubleNode(4);
DoubleLink DL1 = new DoubleLink();
DL1.printDoubleLink();
System.out.println(DL1.LinkIsEmpty());
DL1.addNode(node1);
DL1.printDoubleLink();
DL1.addNode(node3);
DL1.printDoubleLink();
DL1.addNode(node2);
DL1.printDoubleLink();
DoubleNode node4 = new DoubleNode(41);
DL1.addNode(node4);
DL1.addNode(new DoubleNode(343));
DL1.printDoubleLink();
System.out.println(DL1.findNode(41));
System.out.println("删除结点操作");
DL1.delNode(41);
DL1.printDoubleLink();
DL1.modifyNode(4,888);
DL1.printDoubleLink();
}
}
-------
标签:head,java,DoubleNode,链表,DL1,双向,date,pMove,public From: https://www.cnblogs.com/pinglu/p/16652463.html