Java-筆算加法

給定兩個用鏈表表示的正整數,每個結點包含一個數位。編寫代碼對這兩個鏈表表示的整數求和。要求編寫一個函數,接受兩個鏈表作爲輸入。返回值是一個表示兩數和的鏈表。

在這些鏈表中,數位是反向存放的,也就是個位排在鏈表首部。

例如:正整數 698 表示爲 8 -> 9 -> 6,875表示爲 5 -> 7 -> 8,那麼輸入鏈表 8 -> 9 -> 6 和 5 -> 7 -> 8,應該返回鏈表 3 -> 7 -> 5 -> 1(即表示 1573 的鏈表)。

提示:如果您不記得鏈表怎麼表達的,下面的圖是 "8 -> 9 -> 6" 這個鏈表的示例:

鏈表有結點構成,最簡單的鏈表結點裏麪包含兩個字段:一個表示值的字段、另一個鏈接下一個結點的指針或引用。Head 表示一個指向頭結點的指針。最後一個結點(尾結點)的下一個結點是空指針(空結點),表示鏈表沒有下一個結點了。

請先定義鏈表的數據結構,然後編寫代碼完成上述“求和”的操作。這實際上模擬的是筆算加法的操作,注意進位。

  1 public class ExaminationTest {
  2     public static void main(String[] args) {
  3         test();  
  4     }
  5 
  6     public static void test(){
  7         NumberNode x11 = new NumberNode(3, null);
  8         NumberNode x12 = new NumberNode(6, x11);
  9         NumberNode headX = new NumberNode(4, x12);
 10 
 11         NumberNode y11 = new NumberNode(7, null);
 12         NumberNode y12 = new NumberNode(8, y11);
 13         NumberNode headY = new NumberNode(9, y12);
 14 //        NumberNode calculationLinkedX = calculationLinked(headX, headY);
 15 //        print(calculationLinkedX);
 16 
 17         // 以下爲:結果反轉爲整數形式
 18 //        int reversal = reversal(calculationLinkedX);
 19 //        System.out.println("結果:" + reversal);
 20 
 21 
 22         // 另外一組測試數據
 23         NumberNode a25 = new NumberNode(6, null);
 24         NumberNode a24 = new NumberNode(6, a25);
 25         NumberNode d23 = new NumberNode(6, a24);
 26         NumberNode c22 = new NumberNode(9, d23);
 27         NumberNode headA = new NumberNode(8, c22);
 28 
 29         NumberNode a32 = new NumberNode(8, null);
 30         NumberNode a22 = new NumberNode(7, a32);
 31         NumberNode a11 = new NumberNode(5, a22);
 32         NumberNode headB = new NumberNode(1, a11);
 33         // 另外一組測試數據
 34         NumberNode calculationLinkeA = calculationLinked(headA, headB);
 35         print(calculationLinkeA);
 36 
 37         // 以下爲:結果反轉爲整數形式
 38         int reversal = reversal(calculationLinkeA);
 39         System.out.println("正整數:" + reversal);
 40 
 41     }
 42 
 43     private static NumberNode calculationLinked(NumberNode a, NumberNode b){
 44         NumberNode result = new NumberNode(0, null);
 45         if(a == null && b == null){
 46             return result;
 47         }
 48         if(b == null){
 49             return a;
 50         }
 51         if(a == null){
 52             return b;
 53         }
 54         return plus(a, b, result);
 55     }
 56 
 57     private static NumberNode plus(NumberNode a, NumberNode b, NumberNode result){
 58         NumberNode tail = result;
 59         while (tail.next != null){
 60             tail = tail.next;
 61         }
 62 
 63         NumberNode next = new NumberNode(0, null);;
 64         int headValue = a.value + b.value;
 65         int nextV = 0;
 66         int currentV = headValue;
 67         if(headValue >= 10){
 68             currentV = headValue - 10;
 69             nextV = 1;
 70             next = new NumberNode(nextV, null);
 71         }
 72         tail.value += currentV;
 73         tail.next = next;
 74 
 75         if(a.next == null && b.next == null){
 76             return result;
 77         }
 78         if(a.next != null && b.next != null){
 79             result = plus(a.next, b.next, result);
 80         }
 81         if(a.next == null){
 82             tail.next = b.next;
 83         }
 84         if(b.next == null){
 85             tail.next = a.next;
 86         }
 87         return result;
 88     }
 89 
 90     private static int reversal(NumberNode numberNode){
 91         if(numberNode == null){
 92             return 0;
 93         }
 94         NumberNode next = numberNode;
 95         String result = numberNode.value+"";
 96         while (next.next != null){
 97             next = next.next;
 98             result = next.value+""+result;
 99         }
100         return Integer.valueOf(result);
101     }
102 
103     // 正向打印鏈表
104     private static void print(NumberNode a){
105         System.out.print("鏈表:");
106         if(a == null){
107             System.out.print(0);
108             return;
109         }
110         System.out.print(a.value + " -> ");
111         NumberNode next = a;
112         while (next.next != null){
113             System.out.print(next.next.value);
114             next = next.next;
115             System.out.print(" -> ");
116         }
117         System.out.print("null");
118         System.out.println();
119     }
120 
121     static class NumberNode{
122         NumberNode next;
123         int value;
124         public NumberNode() {}
125         public NumberNode(int value, NumberNode next) {
126             this.next = next;
127             this.value = value;
128         }
129         public boolean isTail(){
130             if(this.next == null){
131                 return true;
132             }
133             return false;
134         }
135     }
136 }

運行效果:

1 鏈表:9 -> 4 -> 4 -> 5 -> 6 -> null
2 正整數:65449

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章