特殊用途功能 - 两个清单的产品

问题描述 投票:0回答: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)

其中s1和s2是两个序列,start1和start2-两个序列的起始位置,length1和length2-偏移长度和限制 - 整个产生序列的长度(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]

limit是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的末尾时使它们连接并且必须转到头部,就像在示例中一样。

c++ singly-linked-list
1个回答
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;

}
© www.soinside.com 2019 - 2024. All rights reserved.