特殊使用功能 - 两个列表的产品
Special use function - product of two lists
我得到了一个函数:
template <typename Key, typename Info>
Sequence<Key,Info> produce( Sequence<Key,Info> &s1, int start1, int length1, Sequence<Key,Info> &s2, int start2, int length2, int limit)
其中 s1 和 s2 是两个序列,start1 和 start2 - 两个序列的起始位置,长度 1 和长度 2 - 偏移长度和限制 - 整个生成序列的长度(PRODUCE 的乘积(。
例s1=[1 2 3 4 5]
s2=[10 20 30 40 50]
s3= produce(s1, 2, 2, s2, 1, 3, 12(=[3 4 20 30 40 5 1 50 10 2]
限制是 12,但我们使用了两个列表中的所有元素
class Sequence
{
struct Node{
Key key;
Info info;
Node *next;
};
Node *head = NULL;
当 s1、s2 为空或 limit=0 时,则返回空列表:
if ((k == 0 && l == 0)|limit==0)
{
return prod; // lays in a Sequence construction
}
否则:
typename Sequence<Key,Info>::iterator q;
typename Sequence<Key,Info>::iterator r;
q = s1.begin();
q = q + start1;
r = s2.begin();
r = r + start2;
我想出了与这些块一起移动的片段:
prod.insertFront(s1.get_key(q), s1.get_info(q));
if (s1.end(q))
{
q = s1.begin();
continue;
}
q = q + 1;
和
prod.insertFront(s2.get_key(r), s2.get_info(r));
if (s2.end(r))
{
r = s2.begin();
continue;
}
r = r + 1;
现在我想知道如何将这些块放在一起,当我必须获取剩下的元素时,当迭代器位于序列的末尾并且必须转到头部时使它们连接,就像在示例中一样。
template <typename Key, typename Info>
Sequence<Key,Info> produce( Sequence<Key,Info> &s1, int start1, int length1, Sequence<Key,Info> &s2, int start2, int length2, int limit)
{
Sequence<Key,Info> prod;
int k = s1.length();
int l = s2.length();
int m;
if ((k == 0 && l == 0)|limit==0)
{
cout<<"Test"<<endl;
return prod;
}
typename Sequence<Key,Info>::iterator q;
typename Sequence<Key,Info>::iterator r;
q = s1.begin();
q = q + start1;
r = s2.begin();
r = r + start2;
if(length2!=0|| length1!=0 )
while (prod.length() < limit )
{
if(k==0)
{
for (int j = 0; j < length2 && prod.length() < limit; j++)
{
prod.insertEnd(s2.get_key(r), s2.get_info(r));
if (s2.end(r))
{
r = s2.begin();
continue;
}
r = r + 1;
}
}
if(l==0)
{
for (int i = 0; i < length1 && prod.length() < limit; i++)
{
prod.insertEnd(s1.get_key(q), s1.get_info(q));
if (s1.end(q))
{
q = s1.begin();
continue;
}
q = q + 1;
}
}
//cout<<"zdjjdh"<<endl;
if (k!= 0 && l!=0)
{
for (int i = 0; i < length1 && prod.length() < limit; i++)
{
prod.insertEnd(s1.get_key(q), s1.get_info(q));
if (s1.end(q))
{
q = s1.begin();
continue;
}
q = q + 1;
}
for (int j = 0; j < length2 && prod.length() < limit; j++)
{
prod.insertEnd(s2.get_key(r), s2.get_info(r));
if (s2.end(r))
{
r = s2.begin();
continue;
}
r = r + 1;
}
}
}
return prod;
cout<<endl;
}
相关文章:
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 如何在两个列表中比较和获取非包含值
- 将两个列表合并为一个蛇形列表
- 使用特征查找两个列表之间的差异
- c 中的两个列表在o(1)复杂性中
- 关于合并两个列表的算法的一些问题
- 特殊使用功能 - 两个列表的产品
- 如何获取两个列表的公共元素并将其存储在新列表中而不会重复
- 使用两个列表中的组合生成函数调用
- (C++)如何创建一个函数来接收两个排序的链表并返回出现在两个列表中的第三个元素列表
- 从两个列表中随机配对
- 在有限边界下有效地合并两个列表
- 用c++合并两个列表
- 用merge在C++中合并两个列表
- 核对两个列表
- 读取Txt文件,逐行,在两个列表中存储多个类型,如果存在的话
- 在两个列表中匹配元素算法
- 比较两个列表并查找缺失元素的有效方法
- 如何在C++中应用两个列表之间的交集