-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
50 lines (36 loc) · 1.3 KB
/
Copy pathtest2.cpp
File metadata and controls
50 lines (36 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "mylist.h"
int main() {
// (1)初始化链表L(元素类型为char型)
mylist<char> L;
// (2)依次采用尾插法插入a, b, c, d, e元素
// 注:原类中未实现头插法,这里使用尾插法演示
L.push_back('a');
L.push_back('b');
L.push_back('c');
L.push_back('d');
L.push_back('e');
// (3)输出链表L
std::cout << "链表元素: ";
L.printf(); // 输出:a b c d e
// (4)输出链表L的长度
std::cout << "链表长度: " << L.getsize() << std::endl; // 输出:5
// (5)判断链表L是否为空
std::cout << "链表是否为空: " << (L.empty() ? "是" : "否") << std::endl; // 输出:否
// (6)输出链表L的第3个元素
std::cout << "第3个元素: " << L.at(3) << std::endl; // 输出:c
// (7)输出元素'a'的位置
std::cout << "元素'a'的位置: " << L.find('a') << std::endl; // 输出:0
// (8)在第4个元素位置上插入'f'元素(索引从0开始为位置3)
L.insert(4, 'f'); // 插入后链表为:a b c f d e
// (9)输出链表L
std::cout << "插入后链表: ";
L.printf(); // 输出:a b c f d e
// (10)删除链表L的第3个元素(索引从0开始为位置2)
L.erase(3); // 删除后链表为:a b f d e
// (11)输出链表
std::cout << "删除后链表: ";
L.printf(); // 输出:a b f d e
// (12)释放链表
std::cout << "程序结束,链表将自动释放" << std::endl;
return 0;
}