求好心人指点,只过了第一个,第二个错了,后三个超时,可我就是用的正常的链表写的啊
查看原帖
求好心人指点,只过了第一个,第二个错了,后三个超时,可我就是用的正常的链表写的啊
866969
telankesi楼主2022/11/14 16:23
#include <stdio.h>
#include <stdlib.h>;
typedef struct Stu {
	int data;
	struct Stu* next;
}stu;
int n;//加入人数
int m;//删除人数
int count=0;//记录打印的人数
int left;
void create(stu** head);
void print(stu* head);
void delete(stu** head);
int main() {
	
	scanf("%d", &n);
	stu* head;
	head = (stu*)malloc(sizeof(stu));
	head->next = NULL;
	head->data = 0;
	create(&head);

	delete(&head);
	 left = n - m;
	print(head);
	printf("\n");
	return 0;
}
void create(stu **head) {
	stu* new;
	stu* p;//标记位置
	
	new = (stu*)malloc(sizeof(stu));
	new->next = NULL;
	new->data = 1;
	(*head)->next = new;
	for (int i = 2; i <= n; i++) {
		p = *head;
		int num;//查找的标号
		new = (stu*)malloc(sizeof(stu));
		new->next = NULL;
		new->data = i;
		scanf("%d", &num);
		int flag;
		scanf("%d", &flag);
		if (flag == 0) {
			while (p ->next->data!=num) {
				p = p->next;
			}
			new->next = p->next;
			p->next = new;
		}
		else if (flag == 1) {
			while (p->data != num) {
				p = p->next;
			}
			new->next = p->next;
			p->next = new;
		}
	}

}
void delete(stu **head) {
	int a[200000]={0};
	stu* front;
	front = (stu*)malloc(sizeof(stu));
	
	front = head;

	scanf("%d", &m);
	for (int i = 1; i <= m; i++) {
		stu* now;
		now = (stu*)malloc(sizeof(stu));
		now = (*head)->next;
		int number;
		scanf("%d", &number);
		if (a[number - 1] == 0) {
			int flag = 1;//标记是否找到m
			while (now->data != number) {

				front = now;
				now = now->next;
				if (now == NULL) {

					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				front->next = now->next;
				a[number - 1] = 1;
				free(now);
			}
		}
		}
	
}
void print(stu* head) {  //遍历打印
	stu* q;
	q = head;
	while (q != NULL) {
		q = q->next;
		
		if (q) { printf("%d", q->data);
		if (count != left) printf(" ");
		count++;
		}
	}
	
}
2022/11/14 16:23
加载中...