{"id":98,"date":"2018-03-28T16:44:14","date_gmt":"2018-03-28T08:44:14","guid":{"rendered":"http:\/\/dragonyue.fortiddns.com\/blog\/?p=98"},"modified":"2018-03-29T10:18:29","modified_gmt":"2018-03-29T02:18:29","slug":"java-2018-03-28-hw","status":"publish","type":"post","link":"https:\/\/dragonyue.fortiddns.com\/blog\/java-2018-03-28-hw\/","title":{"rendered":"Java 2018-03-28 HW"},"content":{"rendered":"<pre class=\"prettyprint linenums\">\r\nimport java.util.*;\r\n\r\n\/**\r\n * Java_2018_03_28_HW\r\n * Author:Dragonyue\r\n *\/\r\nclass Record {\r\n    int Data = -1;\r\n    Record nextRecord;\r\n\r\n    public Record(int Data) {\r\n        this.Data = Data;\r\n    }\r\n\r\n    public static void ShowData(Record record) {\r\n        while (record.nextRecord != null) {\r\n            System.out.print(record.Data + \" \");\r\n            record = record.nextRecord;\r\n        }\r\n        System.out.println(record.Data);\r\n    }\r\n\r\n    public static Record Insert(Record aRecord, int input, List<Record> records) {\r\n        Record firstRecord = aRecord;\r\n        if (input <= aRecord.Data) {\r\n            Record temp = new Record(input);\r\n            temp.nextRecord = aRecord;\r\n            records.add(aRecord);\r\n            return temp;\r\n        }\r\n        Record lastrRecord = null;\r\n        while (aRecord.Data <= input) {\r\n            lastrRecord = aRecord;\r\n            if (aRecord.nextRecord != null)\r\n                aRecord = aRecord.nextRecord;\r\n            else\r\n                break;\r\n        }\r\n        Record newRecord = new Record(input);\r\n        records.add(newRecord);\r\n        if (lastrRecord.nextRecord != null)\r\n            newRecord.nextRecord = lastrRecord.nextRecord;\r\n        else\r\n            newRecord.nextRecord = null;\r\n        lastrRecord.nextRecord = newRecord;\r\n        return firstRecord;\r\n    }\r\n\r\n    public static void Revise(Record aRecord, int nowvalue, int newValue) {\r\n        while (aRecord.nextRecord != null) {\r\n            if (aRecord.Data == nowvalue)\r\n                aRecord.Data = newValue;\r\n            aRecord = aRecord.nextRecord;\r\n        }\r\n        if (aRecord.Data == nowvalue)\r\n            aRecord.Data = newValue;\r\n    }\r\n\r\n    public static Record Delete(Record aRecord, int Value, List<Record> records) {\r\n        if (aRecord == null) {\r\n            System.out.println(\"\u8cc7\u6599\u70ba\u7a7a!\");\r\n            return null;\r\n        }\r\n        Record firstRecord = aRecord;\r\n        Record lastRecord = null;\r\n        if (aRecord.Data == Value) {\r\n            Record nextRecord = aRecord.nextRecord;\r\n            if (records.indexOf(aRecord) != -1)\r\n                records.remove(aRecord);\r\n            firstRecord = nextRecord;\r\n            return firstRecord;\r\n        }\r\n        while (aRecord.nextRecord != null) {\r\n            lastRecord = aRecord;\r\n            if (aRecord.nextRecord.Data == Value) {\r\n                lastRecord.nextRecord = aRecord.nextRecord.nextRecord;\r\n                records.remove(aRecord.nextRecord);\r\n                return firstRecord;\r\n            } else\r\n                aRecord = aRecord.nextRecord;\r\n        }\r\n        return firstRecord;\r\n    }\r\n\r\n    public static void Append(Record aRecord, int input, List<Record> records) {\r\n        while (aRecord.nextRecord != null)\r\n            aRecord = aRecord.nextRecord;\r\n        Record newRecord = new Record(input);\r\n        aRecord.nextRecord = newRecord;\r\n        records.add(newRecord);\r\n    }\r\n}\r\n\r\npublic class Java_2018_03_28_HW {\r\n\r\n    public static void main(String[] args) {\r\n        Scanner scanner = new Scanner(System.in);\r\n        Record aRecord = null;\r\n        int method;\r\n        int input;\r\n        List<Record> records = new ArrayList<>();\r\n        while (true) {\r\n            System.out.print(\"\u8f38\u5165(0:\u96e2\u958b 1:\u63d2\u5165 2:\u4fee\u6539 3:\u522a\u9664\uff1a4\uff1a\u65b0\u589e)\uff1a\");\r\n            method = scanner.nextInt();\r\n            if (method == 0)\r\n                break;\r\n            else if (method == 1) {\r\n                System.out.print(\"\u8f38\u5165\u65b0\u589e\u7684\u503c\uff1a\");\r\n                input = scanner.nextInt();\r\n                if (aRecord == null)\r\n                    aRecord = new Record(input);\r\n                else {\r\n                    aRecord = Record.Insert(aRecord, input, records);\r\n                }\r\n            } else if (method == 2) {\r\n                System.out.print(\"\u8acb\u8f38\u5165\u8981\u4fee\u6539\u7684\u503c\u8207\u65b0\u7684\u503c(ex 12 3)\uff1a\");\r\n                int nowvalue = scanner.nextInt();\r\n                int newValue = scanner.nextInt();\r\n                Record.Revise(aRecord, nowvalue, newValue);\r\n            } else if (method == 3) {\r\n                System.out.print(\"\u8acb\u8f38\u5165\u8981\u522a\u9664\u7684\u503c\uff1a\");\r\n                input = scanner.nextInt();\r\n                aRecord = Record.Delete(aRecord, input, records);\r\n            } else if (method == 4) {\r\n                input = scanner.nextInt();\r\n                if (aRecord != null)\r\n                    Record.Append(aRecord, input, records);\r\n                else\r\n                    aRecord = new Record(input);\r\n            }\r\n            if (aRecord != null)\r\n                Record.ShowData(aRecord);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import java.util.*; \/** * Java_2018_03_28_HW * Author:D [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":0,"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dragonyue.fortiddns.com\/blog\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}