#include <iostream> #include <stdio.h> using namespace std; int a[] = {10, 40, 70, 80, 90, 99}; int b[] = {20, 30, 40, 50, 60, 70, 85, 90, 95, 97, 99}; int *result_data; int write_point=0; void print_data(char, int* , int ); void save_data(int); const int END_OF_DATA=99; void main() { int read_point=0; //초기화 result_data=(int*)malloc((sizeof(a)+sizeof(b))*sizeof(int)); //연산 for (register int i=0; a[i]!=END_OF_DATA; ++i) { while (a[i]>=b[read_point]) { save_data(b[read_point]); if (a[i]==b[read_point]) ++i; ++read_point; } save_data(a[i]); } save_data(END_OF_DATA); //출력 cout << "***************\n*MERGE PROGRAM*\n***************\n"; print_data('a', a , sizeof(a)/sizeof(int) ); print_data('b', b , sizeof(b)/sizeof(int) ); print_data('c', result_data , write_point ); //메모리 해방 free(result_data); } void print_data(char what_is, int *data_for_print, int size) { cout << what_is << "= "; for (register int i=0; i<size; ++i) cout << data_for_print[i] << " "; cout << "\n"; } void save_data(int data_for_save) { result_data[write_point]=data_for_save; ++write_point; }
1> lists:merge([10, 40, 70, 80, 90, 99], [20, 30, 40, 50, 60, 70, 85, 90, 95, 97, 99]). [10,20,30,40,40,50,60,70,70,80,85,90,90,95,97,99,99]