java链表实现大数减法
时间: 2024-01-15 07:17:52 浏览: 43
以下是Java链表实现大数减法的示例代码:
```java
import java.util.*;
public class BigIntegerSubtraction {
public static void main(String[] args) {
// 生成两个随机数
String num1 = generateRandomNumber();
String num2 = generateRandomNumber();
System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
// 将两个数存储到链表中
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
for (int i = num1.length() - 1; i >= 0; i--) {
list1.Insert(num1.charAt(i) - '0');
}
for (int i = num2.length() - 1; i >= 0; i--) {
list2.Insert(num2.charAt(i) - '0');
}
// 对两个链表进行减法操作
LinkList result = new LinkList();
int borrow = 0;
Node p = list1.front, q = list2.front;
while (p != null || q != null) {
int x = p != null ? p.data : 0;
int y = q != null ? q.data : 0;
int z = x - y - borrow;
if (z < 0) {
z += 10;
borrow = 1;
} else {
borrow = 0;
}
result.Insert(z);
if (p != null) {
p = p.next;
}
if (q != null) {
q = q.next;
}
}
// 输出结果
System.out.print("result: ");
result.PrintList();
}
// 生成1~50位的随机数
public static String generateRandomNumber() {
Random random = new Random();
int length = random.nextInt(50) + 1;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(random.nextInt(10));
}
return sb.toString();
}
}
class LinkList {
public Node front;
public int length = 0;
public LinkList() {
front = null;
}
public void Insert(int data) {
Node newNode = new Node(data);
if (front == null) {
front = newNode;
} else {
Node p = front;
while (p.next != null) {
p = p.next;
}
p.next = newNode;
}
length++;
}
public void GetList() {
Node p = front;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
}
public void PrintList() {
Node p = front;
while (p != null && p.data == 0) {
p = p.next;
}
if (p == null) {
System.out.print("0");
} else {
while (p != null) {
System.out.print(p.data);
p = p.next;
}
}
System.out.println();
}
public int GetRear() {
Node p = front;
while (p.next != null) {
p = p.next;
}
return p.data;
}
}
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
next = null;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)