学习版
【c语言】
【C艹】
#include<iostream>
class LinkedList {
public:
struct LinkedNode {
char val;
LinkedNode* next;
LinkedNode(char val) :val(val), next(NULL) {};
};
LinkedList(){
dummyHead = new LinkedNode('0');
tail = dummyHead;
}
~LinkedList() {
while (dummyHead) {
LinkedNode* tmp = dummyHead;
dummyHead = dummyHead->next;
delete tmp;
}
}
void addTail(char value) {
LinkedNode* newNode = new LinkedNode(value);
tail->next = newNode;
tail = tail->next;
}
void appendList(LinkedList& list2) {
LinkedNode* cur = list2.dummyHead;
while (cur->next != NULL) {
tail->next = cur->next;
tail = tail->next;
cur = cur->next;
}
}
void printList() {
LinkedNode* cur = dummyHead;
while (cur->next != NULL) {
std::cout << cur->next->val << " ";
cur = cur->next;
}
exit(-1);
}
private:
LinkedNode* dummyHead;
LinkedNode* tail;
};
int main() {
LinkedList list1, list2;
int n, m;
char a;
std::cin >> n;
while (n--) {
std::cin >> a;
list1.addTail(a);
}
std::cin >> m;
while (m--) {
std::cin >> a;
list2.addTail(a);
}
list1.appendList(list2);
list1.printList();
return 0;
}
【STL】
#include<iostream>
#include<vector>
int main() {
int n, m;
std::cin >> n;
std::vector<char> list1(n);
for (int i = 0; i < n; i++) std::cin >> list1[i];
std::cin >> m;
std::vector<char> list2(m);
for (int i = 0; i < m; i++) std::cin >> list2[i];
list1.insert(list1.end(), list2.begin(), list2.end());
for (int i = 0; i < list1.size(); i++)
std::cout << list1[i] << " ";
return 0;
}