1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import java.util.*; /** * Java_2018_03_28_HW * Author:Dragonyue */ class Record { int Data = -1; Record nextRecord; public Record(int Data) { this.Data = Data; } public static void ShowData(Record record) { while (record.nextRecord != null) { System.out.print(record.Data + " "); record = record.nextRecord; } System.out.println(record.Data); } public static Record Insert(Record aRecord, int input, List<Record> records) { Record firstRecord = aRecord; if (input <= aRecord.Data) { Record temp = new Record(input); temp.nextRecord = aRecord; records.add(aRecord); return temp; } Record lastrRecord = null; while (aRecord.Data <= input) { lastrRecord = aRecord; if (aRecord.nextRecord != null) aRecord = aRecord.nextRecord; else break; } Record newRecord = new Record(input); records.add(newRecord); if (lastrRecord.nextRecord != null) newRecord.nextRecord = lastrRecord.nextRecord; else newRecord.nextRecord = null; lastrRecord.nextRecord = newRecord; return firstRecord; } public static void Revise(Record aRecord, int nowvalue, int newValue) { while (aRecord.nextRecord != null) { if (aRecord.Data == nowvalue) aRecord.Data = newValue; aRecord = aRecord.nextRecord; } if (aRecord.Data == nowvalue) aRecord.Data = newValue; } public static Record Delete(Record aRecord, int Value, List<Record> records) { if (aRecord == null) { System.out.println("資料為空!"); return null; } Record firstRecord = aRecord; Record lastRecord = null; if (aRecord.Data == Value) { Record nextRecord = aRecord.nextRecord; if (records.indexOf(aRecord) != -1) records.remove(aRecord); firstRecord = nextRecord; return firstRecord; } while (aRecord.nextRecord != null) { lastRecord = aRecord; if (aRecord.nextRecord.Data == Value) { lastRecord.nextRecord = aRecord.nextRecord.nextRecord; records.remove(aRecord.nextRecord); return firstRecord; } else aRecord = aRecord.nextRecord; } return firstRecord; } public static void Append(Record aRecord, int input, List<Record> records) { while (aRecord.nextRecord != null) aRecord = aRecord.nextRecord; Record newRecord = new Record(input); aRecord.nextRecord = newRecord; records.add(newRecord); } } public class Java_2018_03_28_HW { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Record aRecord = null; int method; int input; List<Record> records = new ArrayList<>(); while (true) { System.out.print("輸入(0:離開 1:插入 2:修改 3:刪除:4:新增):"); method = scanner.nextInt(); if (method == 0) break; else if (method == 1) { System.out.print("輸入新增的值:"); input = scanner.nextInt(); if (aRecord == null) aRecord = new Record(input); else { aRecord = Record.Insert(aRecord, input, records); } } else if (method == 2) { System.out.print("請輸入要修改的值與新的值(ex 12 3):"); int nowvalue = scanner.nextInt(); int newValue = scanner.nextInt(); Record.Revise(aRecord, nowvalue, newValue); } else if (method == 3) { System.out.print("請輸入要刪除的值:"); input = scanner.nextInt(); aRecord = Record.Delete(aRecord, input, records); } else if (method == 4) { input = scanner.nextInt(); if (aRecord != null) Record.Append(aRecord, input, records); else aRecord = new Record(input); } if (aRecord != null) Record.ShowData(aRecord); } } } |