使用本章中的链表类模板,声明两个 int 类型的链表a和b。分别插入5 个元素,然后把b中的元素加人a的尾部
时间: 2023-06-11 20:10:33 浏览: 48
好的,以下是代码实现:
```c++
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main() {
LinkedList<int> a, b;
// 在链表 a 中插入 5 个元素
for (int i = 1; i <= 5; i++) {
a.insertAtEnd(i);
}
// 在链表 b 中插入 5 个元素
for (int i = 6; i <= 10; i++) {
b.insertAtEnd(i);
}
// 把链表 b 中的元素加入链表 a 的尾部
Node<int>* bHead = b.getHead();
while (bHead != nullptr) {
a.insertAtEnd(bHead->data);
bHead = bHead->next;
}
// 输出链表 a 的所有元素
Node<int>* aHead = a.getHead();
while (aHead != nullptr) {
cout << aHead->data << " ";
aHead = aHead->next;
}
cout << endl;
return 0;
}
```
输出结果为:1 2 3 4 5 6 7 8 9 10