caltern1.cpp

来自「数据结构c++语言描述 Borland C++实现」· C++ 代码 · 共 60 行

CPP
60
字号
// alternating meld of two chains

#include <iostream.h>
#include "echain.h"
#include "citer.h"

template<class T>
void Alternate(const Chain<T>& A,
          const Chain<T>& B, Chain<T>& C)
{// Meld alternately from A and B to get C.
   // initialize
   ChainIterator<T> a,  // iterator for A
                    b;  // iterator for B
   T *DataA = a.Initialize(A); // first element of A
   T *DataB = b.Initialize(B); // first of B
   C.Erase(); // empty C

   // create result
   while (DataA && DataB) {
      C.Append(*DataA);
      C.Append(*DataB);
      DataA = a.Next();
      DataB = b.Next();
      }
   // append the rest
   // at most one of A and B can be nonempty now
   while(DataA) {
      C.Append(*DataA);
      DataA = a.Next();
      }
   while(DataB) {
      C.Append(*DataB);
      DataB = b.Next();
      }
}

void main()
{
   int n = 10, m = 5;
   Chain<int> A, B, C;

   // create A
   for (int i = 0; i < n; i++)
      A.Insert(0, 2*(n - i));
   
   // create B
   for (int i = 0; i < m; i++)
      B.Insert(0,2*(n - i) + 1);
   
   cout << "First list is" << endl;
   cout << A << endl;
   cout << "Second list is" << endl;
   cout << B << endl;

   Alternate(A,B,C);

   cout << "Melded list is" << endl;
   cout << C << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?