当前位置 : 主页 > 编程语言 > java >

Java实现双向链表

来源:互联网 收集:自由互联 发布时间:2022-07-13
package com . company ; /** * @author:抱着鱼睡觉的喵喵 * @date:2021/2/9 * @description: */ public class DoubleLinkedListDemo { public static void main ( String [] args ) { Node2 node1 = new Node2 ( 2 , 100 , "李四" ); Node2 node2


package com.company;

/**
* @author:抱着鱼睡觉的喵喵
* @date:2021/2/9
* @description:
*/
public class DoubleLinkedListDemo {

public static void main(String[] args) {
Node2 node1 = new Node2(2, 100, "李四");
Node2 node2 = new Node2(3, 99, "张三");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(node1);
doubleLinkedList.add(node2);
doubleLinkedList.list();
Node2 node3 = new Node2(1, 98, "zms");
doubleLinkedList.insert(node3);
System.out.println("插入1号数据之后:");
doubleLinkedList.list();
doubleLinkedList.delete(3);
System.out.println("删除3号数据之后:");
doubleLinkedList.list();
Node2 node4 = new Node2(2, 53, "zsh");
doubleLinkedList.modify(node4);
System.out.println("修改2号数据之后:");
doubleLinkedList.list();
}
}

class Node2 {
public Node2 next;
public Node2 pre;
public int sno;
public int score;
public String data;


public Node2() {
}

public Node2(int Sno, int NScore, String Data) {
this.sno = Sno;
this.score = NScore;
this.data = Data;
}

@Override
public String toString() {
return "Node2{" +
"sno=" + sno +
", score=" + score +
", data='" + data + '\'' +
'}';
}
}

class DoubleLinkedList {

private Node2 head = new Node2(0, 0, "");

public Node2 getHead() {
return head;
}

public void list() {
Node2 temp = head.next;
if (temp == null) {
System.out.println("LinkedList is empty!");
} else {
while (temp != null) {
System.out.println(temp);
System.out.println();
temp = temp.next;
}
}
}

public void add(Node2 node) {
Node2 temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = node;
node.pre = temp;
}

public void insert(Node2 node) {
Node2 temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.sno > node.sno) {
break;
}
if (temp.next.sno == node.sno) {
flag = true;
break;
}
temp = temp.next;
}
if (flag == true) {
System.out.println("the sno:" + node.sno + "already exists!");
return;
} else {
node.next = temp.next;
temp.next.pre = node;
temp.next = node;
node.pre = temp;
}

}

public void delete(int Sno) {

if (head.next == null) {
System.out.println("LinkedList is empty!");
return;
}
Node2 temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.sno == Sno) {
flag = true;
break;
}
temp = temp.next;
}
if (flag == false) {
System.out.println("查无此人:"+Sno+",无法删除!");
} else {
temp.pre.next = temp.next;
if (temp.next != null){
temp.next.pre = temp.pre;
}
}
}

public void modify(Node2 node) {
if (this.head == null) {
System.out.println("LinkedList is empty!");
return;
}
Node2 temp = head;
while(true) {
if (temp.next == null) {
System.out.println("查无此人:"+node.sno+",无法修改:");
break;
}
if (temp.next.sno == node.sno) {
temp.next.score = node.score;
temp.next.data = node.data;
break;
}
temp = temp.next;
}
}

}


上一篇:Java实现单链表的合并(保证数据的有序性)
下一篇:没有了
网友评论