说是访问了无效内存,不太懂,样例可过,debug无误
查看原帖
说是访问了无效内存,不太懂,样例可过,debug无误
692458
z21371173楼主2022/3/20 09:21
#include<stdio.h>
#include<stdlib.h>
typedef struct s {
	int order;
	struct s* next;
	struct s* front;
} ss;
int hash[100005];
int main() {
	ss *list,*p,*temp;
	list=(ss*)malloc(sizeof(ss)); 
	list->order=1;
	list->front=list->next=NULL;
	int n,m;
	int stu,lor;
	scanf("%d",&n);
	for(int i=1; i<n; i++) {
		scanf("%d%d",&stu,&lor);
		//插入学生
		p=(ss *)malloc(sizeof(ss));//开辟空间
		p->order=i+1;//给他序号
		if(lor==0) { //插在左边
			temp=list;//从头开始找
			while(temp!=NULL) {//别忘了break退出 
				if(temp->order==stu) { //找到了
//特判是不是list
					if(temp==list) {
						temp->front=p;
						p->next=temp;
						list=p;
					} else {
						p->front=temp->front;
						temp->front->next=p;
						p->next=temp;
						temp->front=p;
					}
					break; 
				} else { //不是这个
					temp=temp->next;
				}
			}
		}
		if(lor==1) { //插在you边
			temp=list;//从头开始找
			while(temp!=NULL) {//别忘了break退出 
				if(temp->order==stu) { //找到了
						p->front=temp;
						p->next=temp->next;
						temp->next=p;
					break; 
				} else { //不是这个
					temp=temp->next;
				}
			}
		}
	}
	scanf("%d",&m);
	for(int i=0; i<m; i++) {
		scanf("%d",&stu);
		temp=list;
		if(hash[stu]==1) {
			continue;
		}
		hash[stu]=1;//标记下,再遇到就不用找了
		while(temp!=NULL) {
			if(temp->order==stu) { //找到这个点
				if(temp==list) { //特判头节点
					list=temp->next; 
					free(temp);
					
				}
				else{
					temp->front->next=temp->next;
					temp->next->front=temp->front;
					free(temp);
				}
				break;
			}
			else temp=temp->next;
		}
	}
	temp=list;
	while(temp!=NULL){
		printf("%d ",temp->order);
		temp=temp->next;
	}
}
2022/3/20 09:21
加载中...