插入节点时间复杂度,因为是静态链表,所以查找插入节点的时间复杂度为
删除节点需要头节点开始查找, 再删除
优点是高效的同时不需要手动释放内存,缺点是 如果很小,空间浪费比较大,为
a[p] => p->next && p->val; 下标就是指针,里面的值既代表val也代表下一个节点指针
exp: phead = 0; a[pHead] = 2; 表示pHead是头节点指针,值是2,下一个节点指针是2。
所以在插入的时候,这里的数组既有 链表 的作用,也有 哈希 的作用。
#include <bits/stdc++.h>
using namespace std;
int a[10005];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, head;
while (cin >> n >> head) {
fill(a, a + 10005, -1);
a[0] = head;
for (int i = 1; i < n; i++) {
int cur, pre;
cin >> cur >> pre;
int nx = a[pre];
a[pre] = cur;
a[cur] = nx;
}
int del;
cin >> del;
int pHead = 0, pcur = pHead;
while (pcur != -1) {
int nx = a[pcur];
if (nx == del) a[pcur] = a[nx];
else pcur = a[pcur];
}
pcur = pHead;
while (a[pcur] != -1) {
cout << a[pcur] << " ";
pcur = a[pcur];
}
cout << endl;
}
return 0;
}


京公网安备 11010502036488号