第6章19题

// 6_19.cpp : 设有两个顺序表A和B,元素的个数分别是m和n,若表中的数据都是从小到大顺序排列的,且这(m+n)个数据中没有相同的。试设计算法将A和B合并成一个线性表C,并存储到另一个向量中。
 
node* GetCombineList(node * a, node * b)
{
     node *ra = a;
     node *rb = b;
     node *t = NULL;
     node *head = NULL;
     t = head;
     if (ra->data > rb->data)
     {
          head = rb;
          t = rb;
          rb = rb->pNext;
     }
     else
     {
          head = ra;
          t = ra;
          ra = ra->pNext;
     }
 
     while (ra != NULL && rb != NULL)
     {    
          if (ra->data > rb->data)
         {
             t->pNext = rb;
             t = rb;
             rb = rb->pNext;
         }
         else
        {
             t->pNext = ra;
             t = ra;
             ra = ra->pNext;
        }
     }
     if (ra == NULL)
     {
          ra = rb;
      }
 
     t->pNext = ra;
 
     return head;
}
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: