我同学告诉我用双链表写,但我觉得用单链表也可以
就写了写,为什么一个过了,另一个过不去呢?另外三个还是TLE...
希望大佬指点指点,Orz!
#include <stdio.h>
#include <stdlib.h>
int main()
{
typedef struct student
{
int num;
struct student * next;
}student, *roster;
int N;
scanf("%d",&N);
roster A_head, A_tail;
A_head = (roster)malloc(sizeof(student));
A_head->num = 0;
A_tail = A_head;
A_tail->next = (roster)malloc(sizeof(student));
A_tail = A_tail->next;
A_tail->num =1;
A_tail->next = A_head;
int i,j,k,p;
for(i = 1; i<N; i++)
{
scanf("%d %d", &k, &p);
roster new1 = (roster)malloc(sizeof(student));
new1->num = i+1;
if(p == 0) //前插
{
roster linshi; //linshi的指针用来找待插入位置的前一个结点
for(j=0; j<N; j++)
{
linshi = A_head;
if(linshi->next->num == k) //找到被插入结点的前一个结点
break;
else
linshi = linshi->next;
}
new1->next = linshi->next;
linshi->next = new1;
}
if(p == 1) //后插
{
roster linshi;
for(j=0; j<N; j++)
{
linshi = A_head->next;
if(linshi->num == k) //找到被插入结点
break;
else
linshi = linshi->next;
}
new1->next = linshi->next;
linshi->next = new1;
}
}
int M;
scanf("%d",&M);
for(i=0; i<M; i++)
{
int number;
scanf("%d", &number);
roster linshi;
linshi = A_head;
for(j = 0; j < N ; j++)
{
if(number == linshi->next->num)
linshi->next = linshi->next->next;
else
linshi = linshi->next;
}
}
roster linshi;
linshi = A_head->next;
for(i=0; i < N && linshi->num != 0;i++)
{
if(linshi->num == 0)
;
else
printf("%d ",linshi->num);
linshi = linshi->next;
}
putchar('\n');
return 0;
}