三十分求讲解
  • 板块B3631 单向链表
  • 楼主Emil_
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/8/13 09:22
  • 上次更新2024/8/13 10:00:25
查看原帖
三十分求讲解
1393222
Emil_楼主2024/8/13 09:22
多个循环会超时,怎么改啊
#include <bits/stdc++.h>
using namespace std;
list<int> a(1,1);	
list<int> ::iterator it=a.begin();
int f,x,y,n;
bool flag;
//void print(){
//	for(it=a.begin();it!=a.end();it++)
//		cout<<*it<<" ";
//	cout<<endl;
//}
int main(){
	cin>>n;
	while(n--){
		cin>>f;
		it=a.begin();
		if(f==1){
			cin>>x>>y;
			while(1){
				if(*it==x){
					a.insert(++it,y);
					break;
				}
				it++;
			}
		}else if(f==2){
			flag=false;
			cin>>x;
			while(1){
				if(*it==x){
					it++;
					cout<<*it<<endl;
					flag=true;
					break;
				}
				it++;
			}
			if(!flag)
				cout<<0<<endl;
		}else{
			cin>>x;
			while(1){
				if(*it==x){
					it++;
					a.erase(it);
					break;
				}
				it++;
			}
		}
	}
//	print();
	return 0;
}
2024/8/13 09:22
加载中...