📅  最后修改于: 2023-12-03 14:56:24.440000             🧑  作者: Mango
本文介绍了如何使用C++编写一个程序,通过链表表示的两个数字相乘。该程序实现了链表的创建、链表相乘和结果显示。通过链表表示两个数字可以处理较大的数字,避免了溢出的问题。
首先,我们需要定义链表的节点结构,每个节点包含一个数字和一个指向下一个节点的指针。
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
接下来,我们需要实现一个函数来创建链表。该函数根据输入的数组创建一个链表,并返回链表的头节点。
ListNode* createLinkedList(vector<int>& nums) {
ListNode* prev = new ListNode(0);
ListNode* curr = prev;
for (int num : nums) {
curr->next = new ListNode(num);
curr = curr->next;
}
return prev->next;
}
然后,我们需要实现一个函数来计算链表表示的两个数字的乘积。该函数遍历两个链表,每个节点的数字相乘并累加上一位的进位。最后,我们需要处理进位,并将结果保存在一个新的链表中。
ListNode* multiplyLinkedLists(ListNode* l1, ListNode* l2) {
int m = getLinkedListLength(l1);
int n = getLinkedListLength(l2);
vector<int> result(m + n, 0);
ListNode* curr1 = l1;
ListNode* curr2 = l2;
for (int i = m - 1; i >= 0; i--) {
int carry = 0;
ListNode* temp1 = curr1;
int x = curr1->val;
curr2 = l2;
for (int j = n - 1; j >= 0; j--) {
int y = curr2->val;
int product = x * y + carry + result[i + j + 1];
result[i + j + 1] = product % 10;
carry = product / 10;
curr2 = curr2->next;
}
result[i] += carry;
curr1 = curr1->next;
}
ListNode* prev = new ListNode(0);
ListNode* curr = prev;
for (int num : result) {
curr->next = new ListNode(num);
curr = curr->next;
}
return prev->next;
}
最后,我们需要实现一个函数来显示链表的结果。
void displayLinkedList(ListNode* head) {
ListNode* curr = head;
while (curr != NULL) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
下面是一个使用示例的完整代码:
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createLinkedList(vector<int>& nums) {
ListNode* prev = new ListNode(0);
ListNode* curr = prev;
for (int num : nums) {
curr->next = new ListNode(num);
curr = curr->next;
}
return prev->next;
}
ListNode* multiplyLinkedLists(ListNode* l1, ListNode* l2) {
int m = getLinkedListLength(l1);
int n = getLinkedListLength(l2);
vector<int> result(m + n, 0);
ListNode* curr1 = l1;
ListNode* curr2 = l2;
for (int i = m - 1; i >= 0; i--) {
int carry = 0;
ListNode* temp1 = curr1;
int x = curr1->val;
curr2 = l2;
for (int j = n - 1; j >= 0; j--) {
int y = curr2->val;
int product = x * y + carry + result[i + j + 1];
result[i + j + 1] = product % 10;
carry = product / 10;
curr2 = curr2->next;
}
result[i] += carry;
curr1 = curr1->next;
}
ListNode* prev = new ListNode(0);
ListNode* curr = prev;
for (int num : result) {
curr->next = new ListNode(num);
curr = curr->next;
}
return prev->next;
}
void displayLinkedList(ListNode* head) {
ListNode* curr = head;
while (curr != NULL) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
vector<int> nums1 = {1, 2, 3};
vector<int> nums2 = {4, 5, 6};
ListNode* l1 = createLinkedList(nums1);
ListNode* l2 = createLinkedList(nums2);
cout << "The first linked list is: ";
displayLinkedList(l1);
cout << "The second linked list is: ";
displayLinkedList(l2);
ListNode* result = multiplyLinkedLists(l1, l2);
cout << "The product of the linked lists is: ";
displayLinkedList(result);
return 0;
}
通过链表表示的两个数字相乘可以处理较大的数字,并避免了溢出的问题。本文介绍了如何使用C++编写一个程序,通过链表创建、链表相乘和结果显示。你可以通过阅读示例代码来理解并运行该程序。