特殊用途功能-两个清单的乘积

时间:2019-04-09 01:01:44

标签: c++ singly-linked-list

我有一个功能:

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-起始位置,length1和length2-偏移长度,以及limit-整个产生的序列的长度(PRODUCE的乘积)。

示例 s1 = [1 2 3 4 5]

s2 = [10 20 30 40 50]

s3 =农产品(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;

现在,我想知道当迭代器位于Sequence的末尾且必须移至头部时(例如,例。

1 个答案:

答案 0 :(得分:0)

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;

}