[Algorithm and data structure combat] Linear table operation - merge elements in two linear tables

Input: Sequence A, Sequence B

Output: A sequence table C with elements of AB merged, where the elements in C are arranged in non-decreasing order

Analysis: The sequence table C is an empty table, first take out the elements in the sequence table A and B, and compare these two elements, if the element m1 in A is greater than the element n1 in B, then insert the element n1 in B into In C, continue to take out the next element n2 in B and compare it with the element m1 in A. If the element m1 in A is less than or equal to the element n1 in B, insert the element m1 in A into C, and continue to take out the next element m2 in A and compare it with the element n1 in B. And so on, until the elements in one table are compared, insert the remaining elements in the other table into C.

The following code compiles and passes in the VS2017 environment.

//Data Structure and Algorithm Fundamentals Question 1: Merge elements in two linear tables
//Enter a sequence table A, enter a sequence table B, and ask to merge AB into C, C is a non-decreasing arrangement

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#pragma warning(disable:4996)

using namespace std;

intmain()
{
	int num_of_elements_in_A = 0;
	int num_of_elements_in_B = 0;
	vector<int> list_A;
	vector<int> list_B;
	vector<int> list_C;//The sequence in which the result is stored

	cout << "Please enter the number of elements in list A: ";
	cin >> num_of_elements_in_A;
	if (num_of_elements_in_A <= 0) {
		cout << "The number of elements cannot be less than 0!"<<endl;
		return 1;
	}
	cout << "The number of elements in the A list is:" << num_of_elements_in_A << endl;
	for (int i = 0; i < num_of_elements_in_A; i++) {
		int temp = 0;
		cout << "Please enter the first "<< i + 1 << " element of list A: ";
		cin >> temp;
		list_A.push_back(temp);
	}
	cout << "Please enter the number of elements in list B: ";
	cin >> num_of_elements_in_B;
	if (num_of_elements_in_B <= 0) {
		cout << "The number of elements cannot be less than 0!" << endl;
		return 1;
	}
	cout << "The number of elements in the B list is:" << num_of_elements_in_B << endl;
	for (int i = 0; i < num_of_elements_in_B; i++) {
		int temp = 0;
		cout << "Please enter the first "<< i + 1 << " element of list B: ";
		cin >> temp;
		list_B.push_back(temp);
	}

	sort(list_A.begin(), list_A.end());//Arrange the elements in list A non-decreasingly
	sort(list_B.begin(), list_B.end());//Arrange the elements in list B non-decreasingly
	vector<int>::iterator it_A= list_A.begin(), it_B= list_B.begin();
	//Compare, insert AB elements into C according to the size in turn, and exit when any list pointer moves to the end
	while (it_A != list_A.end() && it_B != list_B.end()) {
		if (*it_A <= *it_B) {
			list_C.push_back(*it_A);
			it_A++;
		}
		else if (*it_B < *it_A) {
			list_C.push_back(*it_B);
			it_B++;
		}
	}
	//Process elements that have not yet been inserted into the C list
	if (it_A == list_A.end()) {
		for (; it_B != list_B.end(); it_B++)
		{
			list_C.push_back(*it_B);
		}
	}
	if (it_B == list_B.end()) {
		for (; it_A != list_A.end(); it_A++)
		{
			list_C.push_back(*it_A);
		}
	}
	//Output the elements of the final list C and check the results
	for (vector<int>::iterator it_C=list_C.begin(); it_C != list_C.end(); it_C++)
	{
		cout << *it_C << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325105403&siteId=291194637
Recommended