实训C++语言设计——接受两个非递减的线性表,并将它们合并为非递减的Lc

本文介绍了一个使用VC++实现的线性表合并程序,该程序能够接收两个由用户输入的线性表,对这两个线性表进行排序后合并成一个新的非递减线性表并输出。

平台:VC++ 2005 测试通过!
.vcproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件。
它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
StdAfx.h, StdAfx.cpp
这些文件用于生成名为 twod.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。
这些都是使用应用程序向导生成的 VC++ 文件故不列出
我只列出程序主要部分!

// MergeList_Vector.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

void MergeList_L( const vector<int>& La, const vector<int>& Lb, vector<int>& Lc);

int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> La, Lb, Lc;
 cout <<"****************MergeList*******************"<<endl;
 cout <<"请输入La线性表的元素(元素间用空格隔开, -1为序列结束符): ";
 int input = -1;
 while ( (cin >> input) && ( input != -1 ))
           La.push_back(input);
   
 cout <<"请输入Lb线性表的元素(元素间用空格隔开, -1为序列结束符): ";
 input = -1;
 while ( (cin >> input) && ( input != -1 ))
           Lb.push_back(input);
 //对La,Lb的元素进行排序,得到两个非递减的线性表
    vector<int>::iterator pos;
    sort(La.begin(), La.end());
 cout <<"La排序后的内容: ";
 for ( int i  = 0; i < La.size(); i++)
       cout << La[i] <<" ";
 cout << endl;
 
 sort(Lb.begin(), Lb.end());
 cout <<"Lb排序后的内容: ";
 for ( int i  = 0; i < Lb.size(); i++)
       cout << Lb[i] <<" ";
 cout << endl;
 
 Lc.resize(Lc.size() + (La.size()+Lb.size()));

 MergeList_Sq(La, Lb, Lc);

 cout <<"Lc的内容: ";
 for ( int i  = 0; i < Lc.size(); i++)
       cout << Lc[i] <<" ";
 cout << endl;

 return 0;
}

//接受两个非递减的线性表,并将它们合并为非递减的Lc
void MergeList_Sq( const vector<int>& La, const vector<int>& Lb, vector<int>& Lc)
{
      vector<int>::const_iterator pa = La.begin();
   vector<int>::const_iterator pb = Lb.begin();
   vector<int>::const_iterator pa_last = La.end();
   vector<int>::const_iterator pb_last = Lb.end();  
         vector<int>::iterator pc = Lc.begin();

   //注意while循环中是pa < pa_last而不是pa <= pa_last
   //这是因为pa_last是指向向量中最末一个元素的下一个位置
   while ( (pa < pa_last) && (pb < pb_last) ){
              if( *pa <= *pb ) *pc++ = *pa++;
     else *pc++ = *pb++;
   }
   while ( pa < pa_last ) *pc++ = *pa++;
   while ( pb < pb_last ) *pc++ = *pb++;

在头歌编程题实训中,基于链表实现两个非递减有序序列的合并,其核心思路是创建一个新的链表,然后比较两个输入链表的节点值,依次将较小值的节点添加到新链表中,直到其中一个链表遍历完,最后将另一个未遍历完的链表剩余部分直接连接到新链表的末尾。 以下是Python代码示例: ```python # 定义链表节点类 class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def merge_two_lists(l1, l2): # 创建一个虚拟头节点 dummy = ListNode(0) current = dummy # 遍历两个链表,比较节点值添加到新链表 while l1 and l2: if l1.val <= l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next # 将剩余的节点添加到新链表 if l1: current.next = l1 if l2: current.next = l2 return dummy.next # 辅助函数:将列表转换为链表 def list_to_linked_list(lst): dummy = ListNode(0) current = dummy for val in lst: current.next = ListNode(val) current = current.next return dummy.next # 辅助函数:将链表转换为列表 def linked_list_to_list(head): result = [] current = head while current: result.append(current.val) current = current.next return result # 示例用法 l1 = list_to_linked_list([1, 3, 5]) l2 = list_to_linked_list([2, 4, 6]) merged_head = merge_two_lists(l1, l2) print(linked_list_to_list(merged_head)) # 输出合并后的链表 ``` 在上述代码中,首先定义了链表节点类`ListNode`,然后实现了`merge_two_lists`函数用于合并两个有序链表。通过创建一个虚拟头节点`dummy`,避免了处理合并过程中头节点的特殊情况。在遍历两个链表时,比较当前节点的值,将较小值的节点连接到新链表上。最后将剩余的链表直接连接到新链表末尾。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值