#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct node {
node *next;
int data;
};
int main() {
cout << "l1=";
int a[50], b[50];
int m = 1;
int n = 1;
while (cin.peek() != '\n') {
cin >> a[m++];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
node* p, * head, * now;
head = new node;
head->data = a[1];
head->next = NULL;
now = head;
for (int i = 2; i <= m; i++) {
p = new node;
p->data = a[i];
p->next = NULL;
now->next = p;
now = p;
}
cout << "l2=";
while (cin.peek() != '\n') {
cin >> b[n++];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
node* pp, * headd, * noww;
headd = new node;
headd->data = a[1];
headd->next = NULL;
noww = headd;
for (int i = 2; i <= n; i++) {
pp = new node;
pp->data = b[i];
pp->next = NULL;
noww->next = pp;
noww = pp;
}
node* ppp, * headdd, * nowww;
headdd = new node;
headdd->data = 0;
headdd->next = NULL;
nowww = headdd;
for (int i = 2; i < m + n; i++) {
ppp = new node;
ppp->data = 0;
ppp->next = NULL;
nowww->next = ppp;
nowww = ppp;
}
now = head;
noww = headd;
nowww = headdd;
for (int i = 1; i < n + m; i++) {
if (now->data >= noww->data) {
nowww->data = noww->data;
noww = noww->next;
nowww = nowww->next;
if (now->next == NULL) {
while (noww->next != NULL) {
nowww->data = noww->data;
nowww = nowww->next;
noww = noww->next;
}
break;
}
if (noww->next == NULL) {
while (now->next != NULL) {
nowww->data = now->data;
nowww = nowww->next;
now = now->next;
}
break;
}
}
else {
nowww->data = now->data;
now = now->next;
nowww = nowww->next;
if (now->next == NULL) {
while (noww->next != NULL) {
nowww->data = noww->data;
nowww = nowww->next;
noww = noww->next;
}
break;
}
if (noww->next == NULL) {
while (now->next != NULL) {
nowww->data = now->data;
nowww = nowww->next;
now = now->next;
}
break;
}
}
}
nowww = headdd;
cout << "[";
while (nowww->next != NULL) {
cout << nowww->data;
if (nowww->next->next == NULL) { break; }
else {cout << ",";}
nowww = nowww->next;
}
cout << "]";
return 0;
}
合并两个有序非增链表
最新推荐文章于 2024-09-01 16:42:17 发布