大家来看看这个言简意赅、形象生动的指针链表吧!
查看原帖
大家来看看这个言简意赅、形象生动的指针链表吧!
206814
封禁用户楼主2022/8/22 08:18

毕竟它差一个点过不去

#include <bits/stdc++.h>
using namespace std;
struct List{
    int data;
    List *prev = NULL, *next = NULL;
}*head, *tail;
vector<List*> students;
signed main(){
    int n, m;
    scanf("%d", &n);
    students.resize(n + 10);
    head = new List;
    head->data = 1, students[1] = head;
    tail = head, head->prev = tail;
    for(int i = 2, k, p; i <= n; i++){
        k = read(), p = read();
        scanf("%d %d", &k, &p);
        List *target = students[k], *now = new List;
        now->data = i;
        if(p == 0){
            if(target == head){
                now->prev = tail, now->next = head;
                head->prev = now, head = now;
                goto end;
            }
            List* tmp = target->prev;
            now->next = target, now->prev = tmp;
            tmp->next = now, target->prev = now;
        }else{
            if(target->next == NULL) target->next = now;
            List* tmp = target->next;
            now->prev = target, now->next = tmp;
            tmp->prev = now, target->next = now;
            if(target == tail) tail = now, head->prev = tail;
        }
end:    students[i] = now, tail->next = head;
    }
    scanf("%d", &m);
    for(int i = 1, x; i <= m; i++){
        scanf("%d", &x);
        students[x]->data = 0;
    }
    List *it;
    for(it = head; it->next != head; it = it->next){
        if(it->data == 0) continue;
        printf("%d ", it->data);
        //printf("{address = 0x%x, data = %d, prev = 0x%x, next = 0x%x}\n", it, it->data, it->prev, it->next);
    }
    if(it->data == 0) return 0;
    printf("%d ", it->data);
    return 0;
}
2022/8/22 08:18
加载中...