cb02a_c++_数据结构_顺序容器_STL_list类_双向链表

/*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
实例化std::list对象
在list开头插入元素
在list末尾插入元素
在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
删除list中的元素
对list中元素进行反转和排序

通过指针指向下一个节点
//链表不是数组,没有下标。只能使用迭代器
*/

 1 /*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
 2 实例化std::list对象
 3 在list开头插入元素
 4 在list末尾插入元素
 5 在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
 6 删除list中的元素
 7 对list中元素进行反转和排序
 8 
 9 通过指针指向下一个节点
10 //链表不是数组,没有下标。只能使用迭代器
11 */
12 
13 #include <iostream>
14 #include <list>
15 
16 using namespace std;
17 
18 void PrintListContents(const list<int>& listInput);
19 
20 int main()
21 {
22     list <int> a;//list也是一个模板类,a就是双向链表
23     a.push_front(10);//链表前端添加数据
24     a.push_front(9);
25     a.push_front(8);
26     a.push_front(7);
27     a.push_back(11);//链表后端添加数据
28 
29     //a.insert(a.begin(), 10);//在开头的前面插入10。 a.begin()就是迭代器
30     
31 
32     
33     list<int> b;
34     b.push_back(100);
35     b.push_back(200);
36     b.push_back(300);
37     b.push_back(400);
38     b.push_back(500);
39 
40     //链表不是数组,没有下标
41     std::list<int>::iterator iter;//迭代器就是指针
42 
43     iter = a.begin();
44     a.insert(iter, 11);//在开头的前面插入11。
45     a.insert(a.end(), 3, 30);//在后端插入3个30,a.end()就是迭代器
46 
47     ++iter;
48     a.insert(iter, 11);//在开头的下一个位置插入11.++iter指针移动了位置
49     //a.insert(a.end(), b.begin(), b.end());//把list b的数据全部插入到list a的末尾
50 
51     a.insert(a.end(),++b.begin(),--b.end());//b的第二个位置数据到 b结尾倒数一个数。一起插入
52 
53     cout << "show list a data..." << endl;
54     PrintListContents(a);
55 
56     /*for (iter = a.begin(); iter != a.end(); ++iter)
57     {
58         cout << *iter << endl;
59     }*/
60     cout << "show list b data" << endl;
61     PrintListContents(b);
62 
63     return 0;
64 }
65 void PrintListContents(const list<int>& listInput)
66 {
67     std::list<int>::const_iterator iter;
68     for (iter = listInput.begin(); iter != listInput.end(); ++iter)
69         cout << *iter << endl;
70 }
 1 /*cb02b_c++_
 2 
 3 */
 4 
 5 /*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
 6 实例化std::list对象
 7 在list开头插入元素
 8 在list末尾插入元素
 9 在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
10 删除list中的元素
11 对list中元素进行反转和排序
12 
13 通过指针指向下一个节点
14 //链表不是数组,没有下标。只能使用迭代器
15 */
16 
17 #include <iostream>
18 #include <list>
19 
20 using namespace std;
21 
22 void PrintListContents(const list<int>& listInput);
23 
24 int main()
25 {
26     std::list<int> a;
27     a.push_front(4);
28     a.push_front(3);
29 
30     list<int>::iterator iElementValueTwo;
31     iElementValueTwo = a.insert(a.begin(),2);//insert返回一个迭代器,指向2.
32     a.push_front(1);
33     a.push_front(0);
34 
35     PrintListContents(a);//
36 
37     //iElementValueTwo--迭代器,指向2.
38     a.erase(iElementValueTwo);//删除一个元素:2
39     cout << "把2删除后显示:" << endl;
40     PrintListContents(a);//
41 
42     //a.erase(a.begin(), iElementValueTwo);//从开始到2,2不包括。删除。
43     a.erase(iElementValueTwo, a.end());//从2开始直到最后的所有数据,删除。
44 
45 
46     return 0;
47 }
48 void PrintListContents(const list<int>& listInput)
49 {
50     std::list<int>::const_iterator iter;
51     for (iter = listInput.begin(); iter != listInput.end(); ++iter)
52         cout << *iter << endl;
53 }
 1 /*cb02c_c++_反转和排序
 2 
 3 */
 4 
 5 /*cb02a_c++_数据结构_顺序容器_STL_list类_双向链表
 6 实例化std::list对象
 7 在list开头插入元素
 8 在list末尾插入元素
 9 在list中间插入元素,插入时间恒定,非常快。数组:中间插入慢。
10 删除list中的元素
11 对list中元素进行反转和排序
12 
13 通过指针指向下一个节点
14 //链表不是数组,没有下标。只能使用迭代器
15 */
16 
17 #include <iostream>
18 #include <list>
19 
20 using namespace std;
21 
22 void PrintListContents(const list<int>& listInput);
23 
24 int main()
25 {
26     std::list<int> a;
27     a.push_front(4);
28     a.push_front(3);
29     a.push_front(8);
30     a.push_front(1);
31     a.push_front(0);
32 
33     PrintListContents(a);
34 
35     //翻转
36     cout << "数据翻转: " << endl;
37     a.reverse();
38     PrintListContents(a);
39 
40     cout << "链表数据排序:" << endl;
41     a.sort();
42     PrintListContents(a);
43 
44 
45 
46 
47     return 0;
48 }
49 void PrintListContents(const list<int>& listInput)
50 {
51     std::list<int>::const_iterator iter;
52     for (iter = listInput.begin(); iter != listInput.end(); ++iter)
53         cout << *iter << endl;
54 }
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/12304249.html