package LinkedList; import java.util.Scanner; public class DoubleLinkedList { private DoubleLinkedList.DoubleLinkedListNode head = new DoubleLinkedList.DoubleLinkedListNode(0, 0); public void add(DoubleLinkedListNode node){ DoubleLinkedListNode temp = head; while (true) { if (temp.next == null){ break; } temp = temp.next; } temp.next = node; node.prev = temp; } //todo 设计按照顺序插入双向链表的方法 public void modify(DoubleLinkedListNode node){ DoubleLinkedListNode temp = head; if (head.next == null){ System.out.println("链表为空"); return; } while (true) { temp = temp.next; if (temp == null){ System.out.println("未找到更新的数据"); return; } if (temp.nodeId == node.nodeId){ temp.data = node.data; System.out.println("数据更新成功"); return; } } } public void delete(int nodeId){ DoubleLinkedListNode temp = head.next; if (temp == null){ System.out.println("链表为空"); return; } while (true) { if (temp == null){ System.out.println("未找到需要删除的节点"); return; } if (temp.nodeId == nodeId){ if (temp.next == null){ temp.prev.next = null; return; } temp.next.prev = temp.prev; temp.prev.next = temp.next; return; } temp = temp.next; } } private void show(){ DoubleLinkedListNode temp = head; if (head.next == null){ System.out.println("当前双向链表为空"); } while(true){ if (temp.next == null){ break; } temp = temp.next; System.out.println(temp.print()); } } public static void main(String[] args) { DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); Scanner scan = new Scanner(System.in); while (true) { System.out.println("a添加节点, s查看节点, r按顺序加入, u更新数据, d删除数据 exit退出:"); String handle = scan.next(); if ("exit".equals(handle)){ break; } if ("a".equals(handle)){ System.out.println("请输入节点编号:"); int no = scan.nextInt(); System.out.println("请输入节点数据:"); int data = scan.nextInt(); DoubleLinkedList.DoubleLinkedListNode node = new DoubleLinkedList.DoubleLinkedListNode(no,data); doubleLinkedList.add(node); } if ("r".equals(handle)){ System.out.println("请输入节点编号:"); int no = scan.nextInt(); System.out.println("请输入节点数据:"); int data = scan.nextInt(); DoubleLinkedList.DoubleLinkedListNode node = new DoubleLinkedList.DoubleLinkedListNode(no,data); } if ("s".equals(handle)){ doubleLinkedList.show(); } if ("u".equals(handle)){ System.out.println("请输入更新节点编号:"); int no = scan.nextInt(); System.out.println("请输入更新节点数据:"); int data = scan.nextInt(); DoubleLinkedList.DoubleLinkedListNode node = new DoubleLinkedList.DoubleLinkedListNode(no,data); doubleLinkedList.modify(node); } if ("d".equals(handle)){ System.out.println("请输入删除节点编号:"); int no = scan.nextInt(); doubleLinkedList.delete(no); } } scan.close(); } public static class DoubleLinkedListNode{ private int nodeId; private int data; private DoubleLinkedListNode next; private DoubleLinkedListNode prev; public DoubleLinkedListNode(int nodeId, int data){ this.nodeId = nodeId; this.data = data; } public int getNodeId() { return nodeId; } public void setNodeId(int nodeId) { this.nodeId = nodeId; } public int getData() { return data; } public void setData(int data) { this.data = data; } public DoubleLinkedListNode getNext() { return next; } public void setNext(DoubleLinkedListNode next) { this.next = next; } public DoubleLinkedListNode getPrev() { return prev; } public void setPrev(DoubleLinkedListNode prev) { this.prev = prev; } public String print() { return "DoubleLinkedListNode{" + "nodeId=" + nodeId + ", data=" + data + ", next=" + (next == null ? "null" : String.valueOf(next.nodeId)) + ", prev=" + (prev == null ? "null" : String.valueOf(prev.nodeId)) + '}'; } } }