如何在这两个分类的链接列表之间获得开关的位置
How can I get the location of switches between these 2 sorted linked lists
我一直在为这项作业挣扎了很长时间,以至于我非常感谢我能得到的任何帮助。
我遇到的问题很简单,但有点令人困惑。
因此,解决了问题的很大一部分(如第一个代码注释中所述 - 找到这两个排序的链接列表之间的最大总路径(
/*
|--------------------------------------------------------------------------
| ADSL Assignment 1 | Maximum Sum Problem. |
|--------------------------------------------------------------------------
| * You are provided with two sorted lists as input.
| * Those lists have some nodes in common.
| * Find a new list composed of the union of the nodes of those two lists.
| * To create the new list, you should take parts (paths of one or more nodes) from each list and merge them together.
| * You are allowed to switch between the two original lists only at intersections (same node in both lists).
| * in order to figure out the resulting list having the path that represents the greatest maximum total.
| Input:
| * Two Sorted Linked Lists.
| Output:
| * New list containing maximum sum path.
| * Location of switches between the 2 lists.
*/
#include <iostream>
using namespace std;
class Node {
public:
int num;
Node* next;
Node(int data, Node* next = 0) { this->num = data; this->next = next; }
Node() { this->next = 0; }
};
class List {
public:
Node* head;
Node* tail;
List() { head = tail = 0; }
void AddToTail(int data) {
if (head == 0) head = tail = new Node(data);
else {
tail->next = new Node(data);
tail = tail->next;
tail->next = 0;
}
}
void Print() {
Node* temp = head;
if (temp == 0) cout << "Empty Listn";
while(temp != 0) {
cout << temp->num << "t";
temp = temp->next;
}
cout << "n";
}
};
List MaxSumPath(Node* head1, Node* head2)
{
List l;
Node* temp1 = 0;
Node* temp2 = 0;
Node* temp = 0;
Node* result = 0;
int sum1 = 0;
int sum2 = 0;
while (head1 || head2) {
temp1 = head1;
temp2 = head2;
sum1 = sum2 = 0;
while (head1 && head2)
{
if (head1->num < head2->num)
{
sum1 += head1->num;
head1 = head1->next;
}
else if (head2->num < head1->num)
{
sum2 += head2->num;
head2 = head2->next;
}
else break;
}
if (head1 == 0)
{
while (head2){
sum2 += head2->num;
head2 = head2->next;
}
}
if (head2 == 0)
{
while (head1){
sum1 += head1->num;
head1 = head1->next;
}
}
if (sum1 >= sum2)
{
if (result == 0) {
result = temp1;
temp = head1;
}
else {
temp->next = temp1;
temp = head1;
}
}
else if (sum1 < sum2) {
if(result == 0) {
result = temp2;
temp = head2;
}
else {
temp->next = temp2;
temp = head2;
}
}
if (head1 && head2 && temp) {
head1 = head1->next;
head2 = head2->next;
temp->next = 0;
}
}
while (result)
{
l.AddToTail(result->num);
result = result->next;
}
return l;
}
int main() {
List l1;
l1.AddToTail(1);
l1.AddToTail(3);
l1.AddToTail(30);
l1.AddToTail(90);
l1.AddToTail(120);
l1.AddToTail(240);
l1.AddToTail(511);
List l2;
l2.AddToTail(0);
l2.AddToTail(3);
l2.AddToTail(12);
l2.AddToTail(32);
l2.AddToTail(90);
l2.AddToTail(125);
l2.AddToTail(240);
l2.AddToTail(249);
cout << "List 1 = ";
l1.Print();
cout << "List 2 = ";
l2.Print();
cout << "---------------------------" << endl;
cout << "List 3 = ";
List result = MaxSumPath(l1.head, l2.head);
result.Print();
return 0;
}
我无法动手的是两个链接列表之间的开关,在示例中,它们应该为3&amp;240
一个开关是该路径继续以获取最大总和路径的数字,并且它只能是两个列表之间的常见节点。
geeksforgeeks上的原始问题
您可以创建一个滑动窗口,滑动窗口从第一个相交点到第二个。对于两个链接列表,存在两个滑动窗口。两者中具有较大总和的滑动窗口必须在新的链接列表中。在滑动窗口的第一个交叉点之后,您可以有两个临时变量指向每个链接列表中的第一个元素。一旦找出路径,您就可以将交叉节点指向该地址。
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表