C++链表STL
程序员文章站
2022-10-16 16:56:14
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上STL的list容器的头文件:#include ......
#include <iostream> #include <list> #include <algorithm> #include <stdlib.h> #include <string.h> typedef struct info_s { int nnumber; bool operator==(struct info_s b) const { return this->nnumber == b.nnumber; } bool operator!=(struct info_s b) const { return this->nnumber != b.nnumber; } bool operator>=(struct info_s b) const { return this->nnumber >= b.nnumber; } bool operator<=(struct info_s b) const { return this->nnumber <= b.nnumber; } bool operator>(struct info_s b) const { return this->nnumber > b.nnumber; } bool operator<(struct info_s b) const { return this->nnumber < b.nnumber; } }info_t; typedef std::list< info_t > list_t; void append(list_t &list, info_t &info) { std::cout<<"***append****"<<std::endl; list.push_back(info); } void for_each(list_t &list) { std::cout<<"***for_each****"<<std::endl; list_t::iterator iter; for(iter = list.begin(); iter != list.end() ;iter++) { std::cout<< iter->nnumber <<std::endl; } } void del_end_info(list_t &list) { std::cout<<"***del_end_info****"<<std::endl; if(! list.empty()) { list.pop_back(); } } void for_each_delete(list_t &list) { list_t::iterator iter; for(iter = list.begin(); iter != list.end() ;) { std::cout<< "delete before iter->number:"<<iter->nnumber <<std::endl; iter = list.erase(iter); std::cout<< "delete after iter->number:"<< iter->nnumber <<std::endl; } } int insert_one(list_t &list , info_t &info, int iplace) { int i = 0; std::cout<<"insert_one"<<std::endl; if(iplace < 0) { std::cout<<"insert_one param error"<<std::endl; return -1; } list_t::iterator iter = list.begin(); while(iter != list.end()) { //std::cout<<" dump "<< (*ivector)<<std::endl; if(i == iplace) { iter = list.insert(iter , info); //此时insert的返回值是迭代器,插入成功后ivector指向插入的位置 std::cout<<" insert_one after list point "<<iter->nnumber <<std::endl; return 0; } i++; ++iter; } iter = list.insert(list.end() , info); return 0; } void find_one(list_t &list,info_t info ) { std::cout<<"find_one"<<std::endl; list_t::iterator iter ; iter = std::find(list.begin(),list.end(), info); if(iter != list.end()) { std::cout<<"find it"<<std::endl; } else { std::cout<<"not find it"<<std::endl; } } void sort(list_t & list) { std::cout<<"sort it"<<std::endl; list.sort(); for_each(list); } int main() { //初始化 list_t list; info_t info; memset(&info, 0, sizeof(info_t)); //添加 info.nnumber = 8; append(list, info); info.nnumber = 5; append(list, info); info.nnumber = 7; append(list, info); info.nnumber = 1; append(list, info); info.nnumber = 1; append(list, info); info.nnumber = 2; append(list, info); info.nnumber = 1; append(list, info); //遍历 for_each(list); //插入 info.nnumber = 80; insert_one(list,info,3); for_each(list); //查找 find_one(list,info); //排序 sort(list); //删除末尾 del_end_info(list); for_each(list); std::cout<< " size:"<<list.size()<<std::endl; //删除所有 // list.clear(); for_each_delete(list); for_each(list); std::cout<< " size:"<<list.size()<<std::endl; return 0; }
上一篇: 购物广场看到的是想要吓死人吗