编写程序,对单链表做生成运算。 1)头插法 2)尾插法 3)改写为类模板,将上述两个封装为Link类中的方法。
时间: 2023-06-14 10:03:21 浏览: 72
以下是使用C++语言编写的单链表生成运算的程序,包括头插法和尾插法,以及使用类模板封装的Link类中的方法:
```C++
#include <iostream>
using namespace std;
// 定义单链表结构体
template <typename T>
struct ListNode {
T val;
ListNode<T>* next;
ListNode(T x) : val(x), next(NULL) {}
};
// 头插法
template <typename T>
ListNode<T>* createListHead(int n) {
ListNode<T>* head = new ListNode<T>(0);
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
p->next = head->next;
head->next = p;
}
return head->next;
}
// 尾插法
template <typename T>
ListNode<T>* createListTail(int n) {
ListNode<T>* head = new ListNode<T>(0);
ListNode<T>* tail = head;
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
tail->next = p;
tail = p;
}
return head->next;
}
// Link类
template <typename T>
class Link {
public:
// 头插法
static ListNode<T>* createListHead(int n) {
ListNode<T>* head = new ListNode<T>(0);
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
p->next = head->next;
head->next = p;
}
return head->next;
}
// 尾插法
static ListNode<T>* createListTail(int n) {
ListNode<T>* head = new ListNode<T>(0);
ListNode<T>* tail = head;
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
tail->next = p;
tail = p;
}
return head->next;
}
};
int main() {
// 头插法生成单链表
cout << "Please input the length of the list:";
int n;
cin >> n;
cout << "Please input the list:";
ListNode<int>* head1 = createListHead<int>(n);
cout << "The list created by head insertion is:";
while (head1) {
cout << head1->val << " ";
head1 = head1->next;
}
cout << endl;
// 尾插法生成单链表
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head2 = createListTail<int>(n);
cout << "The list created by tail insertion is:";
while (head2) {
cout << head2->val << " ";
head2 = head2->next;
}
cout << endl;
// 使用Link类生成单链表
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head3 = Link<int>::createListHead(n);
cout << "The list created by head insertion is:";
while (head3) {
cout << head3->val << " ";
head3 = head3->next;
}
cout << endl;
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head4 = Link<int>::createListTail(n);
cout << "The list created by tail insertion is:";
while (head4) {
cout << head4->val << " ";
head4 = head4->next;
}
cout << endl;
return 0;
}
```
使用时,可以根据需要选择相应的方法。例如,以下是使用头插法生成单链表的示例:
```
Please input the length of the list:5
Please input the list:1 2 3 4 5
The list created by head insertion is:5 4 3 2 1
```
以上程序中,我们使用了模板类,可以根据需要修改元素类型。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)