求调教------>9TLE1RE
查看原帖
求调教------>9TLE1RE
776801
DH_Ding楼主2024/7/25 08:40
#include<bits/stdc++.h>
using namespace std;
struct ListNode{
	int val;
	ListNode* next;
}; 
//插入 
//v---> No.v  在数值v,v存在于链表"L"中 
//num--->  插入num在"L"后面
ListNode* addNode(ListNode* L,int v,int num){
	ListNode* dummyHead  = new ListNode;
	dummyHead->val=0;
	dummyHead->next=L;
	//连接虚拟头, L为真实头节点
	ListNode* cur = dummyHead;
	while(cur!=NULL){
		if(cur->val==v){//1.连接链表 2.创建node,node实则为num 
			ListNode* node =new ListNode;
			node->val=num;
			node->next=cur->next;
			cur->next=node; 
		}
		cur =cur ->next;
	}
	return dummyHead->next;
}
//删除
ListNode* deleteList(ListNode* L,int constant){
	//创建虚拟头节点
	ListNode* dummyHead = new ListNode;
	dummyHead->val=0; 
	dummyHead->next=L;
	
	//虚拟头头头头节点 
	ListNode* cur =dummyHead;
	while(cur->next!=NULL){
		if(cur->next->val==constant){
			//先定义temp
			ListNode* temp = cur->next;//将cur存到temp中	 
			//不断链
			cur->next=cur->next->next;
			//释放内存并删除
			delete temp; 
		}
		else{
			cur = cur->next;
		}
	}
	return dummyHead->next; 
}
//查询
ListNode* askList(ListNode* L,int constant){//constant常量 
	ListNode* cur = L;
	while(cur!=NULL){
		if(cur->val==constant){
			if(cur->next->next==NULL){
				cout<<"0"<<endl;
				break;
			}
			else{
				cout<<cur->next->val<<endl;
				break;
			}
		}
		else{
			cur=cur->next; 
		}
	}
//	return L;
}




//输出 
void printList(ListNode *L){
	ListNode* cur=L;
	while(cur!=NULL){
		cout<<cur->val<<" ";
		cur=cur->next;
	}
	cout<<endl;
}
int n,m;
int main(){
	
	cin>>n;
	ListNode* L=new ListNode;
	L->val=1;
	L->next=NULL;
	for(int i=1;i<=n;i++){
		int aa;
		cin>>aa;
		if(aa==1){
			int bb,cc;cin>>bb>>cc;
			addNode(L,bb,cc);
			//插入大法 
		}
		if(aa==2){
			int bb;cin>>bb;
			askList(L,bb);	
			//询问大法 
		}
		if(aa==3){
			int bb;cin>>bb;
			deleteList(L,bb);
			//删除大法 
		}
	}
//	printList(L); 
	return 0;
}
2024/7/25 08:40
加载中...