1. list使用:
数据结构的双链表一般来说对应stl中的list,并且list几乎提供了双链表操作的所有方法
常见的list操作有(至少是我用到的):
remove,
push_back,
</pre><pre name="code" class="cpp">#include<iostream> #include <list> using namespace std; struct node { int key; int val; }; void main() { node * nn; list<node*> ll; for(int i=0; i<5; i++) { node * tmp=new node; tmp->key=i; ll.push_back(tmp); if(i==3) nn=tmp; } ll.remove(nn); list<node*>::iterator it; cout<<"all the data in the list is:"<<endl; for(it=ll.begin(); it!=ll.end(); it++) { cout<<(*it)->key<<endl; } cout<<endl<<"after delete the node the original pointer's space is still exist"<<endl; cout<<nn->key<<endl<<endl; cout<<"ll's size is: "<<ll.size()<<endl; }
运行结果:
时间: 2024-11-05 12:28:15