#include<iostream>
#include<list>
using namespace std;
void display_list(list<int> &l)
{
list<int>::iterator it;
for(it=l.begin();it!=l.end();it++) cout<<*it<<" ";
cout<<endl;
}
void demo_test1()
{
list<int> l1;
list<int> l2(10);
list<int> l3(5,20);
list<int> l4(l3.begin(),l3.end());
list<int> l5(l4);
cout<<"***********************************"<<endl;
cout<<"打印LIST结构的结果:"<<endl;
display_list(l3);
cout<<"***********************************"<<endl;
}
void demo_test2()
{
list<int> l1;
cout<<"***********************************"<<endl;
cout<<"判断链表是否为空:"<<endl;
if(l1.empty()) cout<<"该链表为空!"<<endl;
else cout<<"该链表不为空!"<<endl;
cout<<"***********************************"<<endl;
}
void demo_test3()
{
list<int> l1;
cout<<"***********************************"<<endl;
cout<<"链表的基本操作:"<<endl;
l1.push_front(10);
l1.push_front(20);
l1.push_front(30);
l1.push_back(10);
l1.push_back(20);
l1.pop_back();
l1.pop_front();
display_list(l1);
cout<<"当前链表元素的数量是:"<<l1.size()<<endl;
cout<<"***********************************"<<endl;
}
void demo_test4()
{
cout<<"***********************************"<<endl;
cout<<"链表的增删操作:"<<endl;
list<int> l1;
l1.insert(l1.begin(),10); //在链表最前端插入数据10
l1.insert(l1.begin(),5,20); //在链表最前端插入5个数据内容为20
list<int> k(2,50); //创建一个新的链表k,其拥有2个元素内容均为50
l1.insert(l1.begin(),k.begin(),k.end()); //在链表v最前端插入链表上K的全部内容
l1.erase(l1.begin()); //删除第一个元素
//l1.erase(l1.begin(),l1.begin()+4);
display_list(l1);
cout<<"当前链表元素的数量是:"<<l1.size()<<endl;
cout<<"***********************************"<<endl;
}
int cmp(const int &a,const int &b)
{
return a>b;
}
void demo_test5()
{
list<int> l1;
l1.push_back(10);
l1.push_back(8);
l1.push_back(20);
l1.push_back(25);
cout<<"***********************************"<<endl;
cout<<"链表的排序操作:"<<endl;
cout<<"链表原始顺序:"<<endl;
display_list(l1);
cout<<"链表默认排序顺序:"<<endl;
l1.sort();
display_list(l1);
cout<<"链表自定义排序顺序:"<<endl;
l1.sort(cmp);
display_list(l1);
cout<<"***********************************"<<endl;
}
int main()
{
demo_test1();
demo_test2();
demo_test3();
demo_test4();
demo_test5();
return 0;
}
05-28
1536
06-22
1405