就是如果规定不能用vector那种函数啥的,只能用最基本的链表操作的话有办法优化吗
#include<stdio.h>
#include<malloc.h>
struct cell
{
int x;
struct cell* next;
};
int main()
{
int n;
scanf("%d", &n);
cell* head, * p, * tmp, * q;
p = (struct cell*)malloc(sizeof(struct cell));
p->x = 1;
p->next = NULL;
head = p;
int i, j, k, l;
for (i = 2; i <= n; i++)
{
scanf("%d%d", &k, &l);
if (l == 0)
{
if (head->x == k)
{
tmp = head;
p = (struct cell*)malloc(sizeof(struct cell));
p->x = i;
p->next = tmp;
head = p;
}
else
{
p = head;
while (p->next->x != k)
{
p = p->next;
}
tmp = (struct cell*)malloc(sizeof(struct cell));
tmp->x = i;
tmp->next = NULL;
q = p;
q = q->next;
p->next = tmp;
tmp->next = q;
}
}
if (l == 1)
{
p = head;
while (p->x != k)
{
p = p->next;
}
tmp = (struct cell*)malloc(sizeof(struct cell));
tmp->x = i;
tmp->next = NULL;
q = p;
q = q->next;
p->next = tmp;
tmp->next = q;
}
}
scanf("%d", &j);
int m;
for (i = 0; i < j; i++)
{
scanf("%d", &m);
p = head;
while (p->next!=NULL)
{
if (p->next->x == m) {
break;
}
p = p->next;
}
if (p->next == NULL) { continue; }
else {
q = p;
q = q->next;
p->next = q->next;
free(q);
}
}
p = head;
while (p != NULL)
{
printf("%d", p->x);
if (p->next != NULL)
{
printf(" ");
}
p = p->next;
}
return 0;
}