最后两个节点超时
查看原帖
最后两个节点超时
1314520
laowangwaimaidian楼主2024/12/17 23:16
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
	int data;
	struct Node* next;
}Node;

Node* init() {
	Node*head=(Node*)malloc(sizeof(Node));
	head->next = NULL;
	return head;
}
void find(Node* head, int e) {
	Node* p = head->next;
	while (p) {
		if (p->data == e) {
			if (p->next) {
				printf("%d\n", p->next->data);
			}
			else {
				printf("%d\n", 0);
			}
			return;
		}
		p = p->next;
	}
}
void insert(Node* head, int x,int y) {
	Node* b = (Node*)malloc(sizeof(Node));
	b->data = y;
	Node* p = head->next;
	while (p) {
		if (p->data == x) {
			b->next = p->next;
			p->next = b;
			return;
		}
		p = p->next;
	}
}

void delete(Node* head, int x) {
	Node* p = head->next;
	while (p) {
		if (p->data == x) {
			Node* temp = p->next;
			p->next = temp->next;
			return;
		}
		p = p->next;
	}
}
int main() {
	Node* head= init();
	Node* first = (Node*)malloc(sizeof(Node));
	first->data = 1;
	head->next = first;
	first->next = NULL;
	int num;
	scanf("%d", &num);
	for (int i = 0; i < num; i++) {
		int n;
		scanf("%d", &n);
		int x, y;
		if (n == 1) {
			scanf("%d %d", &x, &y);
			insert(head, x, y);

		}
		else if (n == 2) {
			scanf("%d", &x);
			find(head, x);
		}
		else {
			scanf("%d", &x);
			delete(head, x);
		}
	}
	return 0;
}
2024/12/17 23:16
加载中...