直接上代码
//节点
class HeroNodeD{
int no;
String name;
String nickName;
HeroNodeD pre;//前一节点
HeroNodeD next;//后一节点
public HeroNodeD(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNodeD{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
双向链表:
//双向链表
class DoublleLinkList{
public HeroNodeD head = new HeroNodeD(0,"","");
public void setHead(HeroNodeD head) {
this.head = head;
}
//0, 获取头结点
public HeroNodeD getHead(){
return head;
}
//1, 获取尾结点
public HeroNodeD getLastNode(){
HeroNodeD temp = head;
while(true){
if(temp.next == null){
break;//尾节点
}
temp = temp.next;
}
return temp;
}
//2, 添加(添加节点到链表的最后面)
public void add(HeroNodeD node){
HeroNodeD temp = head;
while(true){
if(temp.next == null){
break;//尾节点
}
temp = temp.next;
}
//循环结束后,此时temp就是尾节点
temp.next = node;
node.pre = temp;
}
//3, 添加(根据节点的no(排名)的大小, 有序添加)
public void addByNo(HeroNodeD node){
HeroNodeD temp = head;
while (true) {
if(temp.next == null){
break;//链表尾部就跳出
}
if(temp.next.no > node.no){
break;
}
temp = temp.next;
}
//循环结束, 说明temp的下一节点, 就是node的插入位置
node.next = temp.next;//设置node和 temp.next的后节点
temp.next.pre = node;//设置node和 temp.next的 前节点
node.pre = temp;//设置temp 和 node的 前节点
temp.next = node;//设置temp 和 node的 后节点
}
//4, 双向链表的 遍历,从前向后遍历
public void showBeginToEnd(){
HeroNodeD temp = head;
while(true){
if(temp.next == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
System.out.println(temp);
}
//5:从后向前遍历
public void showEndToBegin(){
//获取尾节点
HeroNodeD temp = getLastNode();
while(true){
if(temp.pre == null){
break;
}
System.out.println(temp);
temp = temp.pre;
}
System.out.println(temp);
}
//6, 链表的长度// 头节点不计入长度
public int length(){
HeroNodeD temp = head;
int count=0;
while(true){
if(temp.next == null){
break;
}
count++;
temp = temp.next;
}
return count;
}
//7, 删除第n个节点
public void delNode(int n){
int count = 0;
boolean flag = false;
HeroNodeD temp = head.next;
while (temp != null) {
count++;
if (count == n) {
flag = true;
break;
}
temp = temp.next;
}
if(flag){
//跳出循环,说明找到这个节点, 删除该节点
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}else{
System.out.println("不存在这个节点");
}
}
//8, 更改某个节点
public void updateNode(int n, HeroNodeD node){
boolean flag = false;
int count = 0;
HeroNodeD temp = head.next;
while (temp != null) {
if(temp.next == null){
break;
}
count++;
if(count == n){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
//更换这个节点
temp.pre.next = node;
node.pre = temp.pre;
node.next = temp.next;
temp.next.pre = node;
}else{
System.out.println("不存在这个节点");
}
}
}
测试代码:
public static void main(String[] args){
DoublleLinkList doublelink = new DoublleLinkList();
HeroNodeD node1 = new HeroNodeD(1, "宋江", "及时雨");
HeroNodeD node2 = new HeroNodeD(2, "吴用", "神算子");
HeroNodeD node3 = new HeroNodeD(3, "卢俊义", "玉麒麟");
HeroNodeD node4 = new HeroNodeD(4, "武松", "打老虎");
HeroNodeD node5 = new HeroNodeD(5, "吕财", "打老虎");
doublelink.add(node1);
doublelink.add(node3);
doublelink.addByNo(node2);//有序添加
doublelink.showBeginToEnd();//1-2-3
System.out.println("==========================");
doublelink.showEndToBegin();//3-2-1
System.out.println(doublelink.length());//长度为3
doublelink.delNode(4);//没有这个节点
doublelink.showBeginToEnd();
System.out.println("==========");
doublelink.updateNode(2,node5);//更换第二个节点
doublelink.showBeginToEnd();
}
测试结果: