80分TLE求助!!手搓Node链表
查看原帖
80分TLE求助!!手搓Node链表
927750
CHENXI989楼主2023/9/8 14:37
#include<iostream>
using namespace std;
struct Node {
	int val;
	Node* next;
};
Node* head=NULL;
void Print() {
	Node* temp = head;
	while (temp != NULL) {
		cout << temp->val<<" ";
		temp = temp->next;
	}
	cout << "\n";
}

void Insert(int x, int y) {
	Node* temp = new Node;
	temp->val = y;
	Node* p = head;
	while (p->val != x) {
		p = p->next;
	}
	temp->next = p->next;
	p->next = temp;
	//cout << "现在链表为:";
	//Print();
}
void Research(int x) {
	Node* p=head;
	while (p->val != x) {
		p = p->next;
		if (p->next == NULL) {
			cout << 0<<endl;
			return;
	}
	}
	p = p->next;
	cout << p->val << endl;
	return;
}

void del(int x) {
	Node* p = head;
	while (p->val != x) {
		p = p->next;		
	}
	Node* p1=p->next;
	Node* p2 = p->next;
	p1=p1->next;
	p->next = p1;
	/*
	cout << "现在链表为:";
	Print();*/
	delete p2;
}
int main() {
	/*head->val = 1;*/
	Node* temp = new Node();
		temp->val = 1;
		temp->next = head;
		head = temp;
	int t;
	cin >> t;
	while (t--) {
		int op,x,y;
		cin >> op;
		if (op == 1) {
			cin >> x>>y;
			Insert(x,y);
		}
		else if (op == 2) {
			cin >> x;
			Research(x);
		}
		else if (op == 3) {
			cin >> x;
			del(x);
		}
	}
	return 0;
}
2023/9/8 14:37
加载中...