双向链表list笔记
2025-08-31 10:04:17
发布于:上海
因为上次发链表学习笔记时帅童问我为啥不用list,所以直接原地更新
#include<bits/stdc++.h>
#include<list>//双向链表
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
list<int>a;//创建双向空链表
int n;
cin>>n;
//双向链表的输入
for(int i=0;i<n;i++)
{
int k;
cin>>k;
a.push_back(k);
}
//双向链表头插
a.push_front(100);
//双向链表尾插
a.push_back(999);
//双向链表中间插
auto it=a.begin();//移位
advance(it,3);
a.insert(it,666);//插入
//双向链表尾删
a.pop_back();
//双向链表头删
a.pop_front();
//双向链表中间删
it=a.begin();
advance(it,2);
auto p=a.erase(it);
//双向链表输出
for(auto it=a.begin();it!=a.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
cout<<*p;
return 0;
}
全部评论 6
%%%
9小时前 来自 浙江
09小时前 来自 浙江
09小时前 来自 浙江
0d
9小时前 来自 浙江
03天前 来自 上海
03天前 来自 上海
0%%%
3天前 来自 广东
0%%%
3天前 来自 上海
0
有帮助,赞一个