#include<iostream> using namespace std; const int N = 1005; int head, e[N], ne[N], idx; void init() { head = -1; idx = 0; } void add_to_b(int a, int b) { e[idx] = a; int tmp = head; while(e[tmp] != b) tmp = ne[tmp]; ne[idx] = ne[tmp]; ne[tmp] = idx++; } void delete_x(int x) { int tmp = head, tmp1 = ne[tmp]; while(e[tmp1] != x) { tmp = ne[tmp]; tmp1 = ne[tmp1]; } ne[tmp] = ne[ne[tmp]]; } int main() { int n, x; cin >> n >> x; init(); e[idx] = x; ne[idx] = -1; head = idx++; for (int i = 0; i < n -1; i++) { int a, b; cin >> a >> b; add_to_b(a, b); } int value; cin >> value; delete_x(value); for(int i = head; i != -1; i = ne[i]) cout << e[i] << " "; return 0; }
数组模拟链表,删除的那里用了两个指针,一个指针用来判断值那个点在哪里,另一个指针指向前一个,然后删除点