特殊使用功能 - 两个列表的产品

Special use function - product of two lists

本文关键字:两个 列表 功能      更新时间:2023-10-16

我得到了一个函数:

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;
}