这辈子我都不想用链表了
查看原帖
这辈子我都不想用链表了
1401746
Existence楼主2024/8/2 16:16
#include <bits/stdc++.h>
#define f(A,B) for(int A=1;A<=B;++A)

using namespace std;

struct node{
	int data;
	node *next=nullptr;
}; 

node *head=nullptr;
node *p=nullptr;
node *now=nullptr;
node *tail=nullptr;

void addnode(int x,int y){
	p=head;
	bool pd=false;
	
	do{
		if(p->data==x) {
			pd=true;
			break;
		}
		p=p->next;
	}while (p!=head);
	
	if(pd==false) return;
	
	now=new node;
	now->data=y;
	now->next=p->next->next;
	p->next=now;
	
	
	if(now->next==head) {
		tail=now;
		tail->next=head;
	}
	now=nullptr;
}

int asknode(int x){
	p=head;
	bool pd=false;
	
	do{
		if(p->data==x) {
			pd=true;
			break;
		}
		p=p->next;
	}while (p!=head);
	
	if(pd==false) return 0;
	
	if(!p->next) return 0;
	else return p->next->data;
	
}
void popnode(int x){
	p=head;
	bool pd=false;
	
	do{
		if(p->data==x) {
			pd=true;
			break;
		}
		p=p->next;
		
	}while (p!=head);
	
	if(pd==false) return;
	
	if(p->next==head){
	
		tail=p;
		head=head->next;
		tail->next=head;
	}
	else if(p->next->next==head){
	
		p->next=head;
		tail=p;
		
	}
	else {
		
		p->next=p->next->next;
		
	}
	
}

int main(){
	int q,n,x,y,ele;
	head = new node;
	tail = new node;
	head->data=1;
	head->next=tail;
	tail->next=head;
	scanf("%d",&q);
	
	f(i,q){
		scanf("%d",&n);
		
		if(n==1) {
			scanf("%d%d",&x,&y);
			addnode(x,y);
		}
		
		else if(n==2) {
			scanf("%d",&x);
			ele=asknode(x);
			printf("%d\n",ele);
		}
		
		else {
			scanf("%d",&x);
			popnode(x);
			
		}
		
	}
}

126行代码换来1个AC...

以及9个TLE。

2024/8/2 16:16
加载中...