JAVAコードはダブルチェーンテーブルの添削を実現する


ダブルチェーンテーブルの追加削除
今日はダブルチェーンテーブルの添削修正操作を学びました.もし間違いがあったら、指摘してください.私は必ずタイムリーに修正します.
package com.LinkedList.DoubleLinkedList;

/*
 *         
 */
public class DoubleLinkedListDemo2 {

	public static void main(String[] args) {
		Node node1 = new Node(1, "A");
		Node node2 = new Node(2, "B");
		Node node3 = new Node(3, "C");
		Node node4 = new Node(4, "D");
		DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
		//        
		/*doubleLinkedList.addLast(node1);
		doubleLinkedList.addLast(node3);
		doubleLinkedList.addLast(node2);
		doubleLinkedList.addLast(node4);
		doubleLinkedList.list();*/
		//        
		doubleLinkedList.addByOrder(node3);
		doubleLinkedList.addByOrder(node2);
		doubleLinkedList.addByOrder(node4);
		doubleLinkedList.addByOrder(node1);
		doubleLinkedList.list();
		//      
		doubleLinkedList.updateByOrder(new Node(2, "BBB"));
		doubleLinkedList.list();
		//       
		doubleLinkedList.deleteByOrder(3);
		doubleLinkedList.list();

	}

}

//     
class DoubleLinkedList {
	private Node head = new Node(0, "");

	public Node getHead() {
		return head;
	}

	/**
	 *           
	 * 
	 * @param node
	 */
	public void addLast(Node node) {
		Node temp = head;
		//       
		while (true) {
			if (temp.getNext() == null) {
				break;
			}
			temp = temp.getNext();
		}
		//       
		temp.setNext(node);
		node.setPre(temp);
	}

	/**
	 *          
	 * 
	 * @param node
	 */
	public void addByOrder(Node node) {
		Node temp = head;
		boolean flag = false;
		//       
		while (true) {
			if (temp.getNext() == null) {
				break;
			}
			if (temp.getNext().getNo() > node.getNo()) {
				break;
			}
			if (temp.getNext().getNo() == node.getNo()) {
				flag = true;
				break;
			}
			temp = temp.getNext();
		}
		if (flag) {
			System.out.printf("   %d       ,    !
", node.getNo()); } else { if (temp.getNext() != null) {// temp.getNext().setPre(node); node.setNext(temp.getNext()); } // temp.setNext(node); node.setPre(temp); } } /** * * * @param node */ public void updateByOrder(Node node) { if (head.getNext() == null) { System.out.println(" , !"); return; } Node temp = head; boolean flag = false; while (true) { if (temp.getNext() == null) { break; } if (temp.getNext().getNo() == node.getNo()) { flag = true; break; } temp = temp.getNext(); } if (flag) { temp.getNext().setName(node.getName()); } else { System.out.printf(" %d !
", node.getNo()); } } /** * * * @param no */ public void deleteByOrder(int no) { if (head.getNext() == null) { System.out.println(" , !"); return; } // , Node temp = head.getNext(); boolean flag = false; while (true) { if (temp == null) { break; } if (temp.getNo() == no) { flag = true; break; } temp = temp.getNext(); } if (flag) { if (temp.getNext() != null) {// , , temp.getNext().setPre(temp.getPre()); } temp.getPre().setNext(temp.getNext()); } else { System.out.printf(" %d !
", no); } } /** * */ public void list() { if (head.getNext() == null) { System.out.println(" , !"); return; } Node temp = head.getNext(); while (true) { if (temp == null) { break; } System.out.println(temp); temp = temp.getNext(); } } } // class Node { private int no;// private String name;// private Node pre;// private Node next;// public int getNo() { return no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setNo(int no) { this.no = no; } public Node getPre() { return pre; } public void setPre(Node pre) { this.pre = pre; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } // public Node(int no, String name) { this.no = no; this.name = name; } @Override public String toString() { return "Node [no=" + no + ", name=" + name + "]"; } }