PAT (Advanced Level) Practice 鏈表題node
結構體類型名和結構體變量名儘可能不一樣ios
struct Node{ int address; // 記錄地址(若後面排序則不能以數組下標代替) typename data; int next; XXX... } node[100001];
for (int i = 0; i < 100001; i++){ node[i].XXX = 0; }
int n, head, address, key, next; scanf("%d%d", &n, &head); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &key, &next); node[address] = {address, key, next, 0}; }
依據題目所給鏈表首地址和各結點信息遍歷鏈表上的結點,標記性質,記錄個數算法
int p = head, count = 0; while (p != -1){ XXX = 1; count++; p = node[p]->next; }
int count = 0; for (int p = head; p != -1; p = node[p].next){ node[p].XXX = 1; count++; }
bool cmp (Node a, Node b){ if (a.XXX == -1 || b.XXX = -1){ // 比較標記性質,如有無效點此性質較小 -> 被放到數組後面 return a.XXX > b.XXX; } else{ // 按要求排序 } }
#include<iostream> using namespace std; struct Node{ char data; int next; bool inFirst; } node[100001]; int main() { int head1, head2, n, address, next, p; char data; scanf("%d%d%d", &head1, &head2, &n); for (int i = 0; i < n; i++){ scanf("%d %c %d", &address, &data, &next); node[address] = {data, next, false}; } for (p = head1; p != -1; p = node[p].next) node[p].inFirst = true; for (p = head2; p != -1; p = node[p].next) if (node[p].inFirst == true) break; if (p == -1) printf("-1"); else printf("%05d", p); return 0; }
#include<iostream> #include<algorithm> using namespace std; struct Node{ int address, key, next, inList; } node[100001]; bool cmp (Node a, Node b){ return !a.inList || !b.inList ? a.inList > b.inList : a.key < b.key; } int main() { int n, head, address, key, next, count = 0; scanf("%d%d", &n, &head); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &key, &next); node[address] = {address, key, next, 0}; } for (int i = head; i != -1; i = node[i].next){ node[i].inList = 1; count++; } if (!count) printf("0 -1\n"); else{ sort(node, node+100001, cmp); printf("%d %05d\n", count, node[0].address); for (int i = 0; i < count - 1; i++) printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+1].address); printf("%05d %d %d\n", node[count-1].address, node[count-1].key, -1); } return 0; }
for (int i = 0; i < vector.size() - 1; i++
改成 for (int i = 1; i < vector.size(); i++
避開 size()-1#include<iostream> #include<cmath> #include<set> #include<vector> using namespace std; struct Node{ int address, data, next; } node[100001]; int main() { int head, n, address, data, next; scanf("%d%d", &head, &n); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &data, &next); node[address] = {address, data, next}; } vector<Node> result, removed; set<int> occured; for (int p = head; p != -1; p = node[p].next){ if (occured.find(abs(node[p].data)) != occured.end()) removed.push_back(node[p]); else{ occured.insert(abs(node[p].data)); result.push_back(node[p]); } } for (int i = 1; i < result.size(); i++) printf("%05d %d %05d\n", result[i-1].address, result[i-1].data, result[i].address); if (result.size() > 0) printf("%05d %d -1\n", result[result.size()-1].address, result[result.size()-1].data); for (int i = 1; i < removed.size(); i++) printf("%05d %d %05d\n", removed[i-1].address, removed[i-1].data, removed[i].address); if (removed.size() > 0) printf("%05d %d -1\n", removed[removed.size()-1].address, removed[removed.size()-1].data); return 0; }
#include<iostream> #include<algorithm> using namespace std; int K = 0; struct Node{ int address, data, next, inList, oriorder; } node[100001]; bool cmp (Node a, Node b){ if (!a.inList || !b.inList) return a.inList > b.inList; if ((a.data >= 0 && b.data < 0) || (a.data < 0 && b.data >= 0)) return a.data < b.data; if ((a.data > K && b.data <= K) || (a.data <= K && b.data > K)) return a.data < b.data; return a.oriorder < b.oriorder; } int main() { int n, head, address, data, next, count = 0, order = 0; scanf("%d%d%d", &head, &n, &K); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &data, &next); node[address] = {address, data, next, 0, 0}; } for (int p = head; p != -1; p = node[p].next){ node[p].inList = 1; node[p].oriorder = order; count++; order++; } sort(node, node+100001, cmp); for (int i = 0; i < count - 1; i++) printf("%05d %d %05d\n", node[i].address, node[i].data, node[i+1].address); printf("%05d %d %d\n", node[count-1].address, node[count-1].data, -1); return 0; }
#include<iostream> #include<vector> using namespace std; struct Node{ int address, data, next; } node[100000]; int main() { int head, n, k, address, data, next; scanf("%d%d%d", &head, &n, &k); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &data, &next); node[address] = {address, data, next}; } vector<Node> result; for (int p = head; p != -1; p = node[p].next) if (node[p].data < 0) result.push_back(node[p]); for (int p = head; p != -1; p = node[p].next) if (node[p].data >= 0 && node[p].data <= k) result.push_back(node[p]); for (int p = head; p != -1; p = node[p].next) if (node[p].data > k) result.push_back(node[p]); for (int i = 1; i < result.size(); i++) printf("%05d %d %05d\n", result[i-1].address, result[i-1].data, result[i].address); printf("%05d %d -1\n", result[result.size()-1].address, result[result.size()-1].data); return 0; }
node *p, *pre, *head; head = new node(); head->next = NULL; pre = head; for (int i = 0; i < n; i++){ p = new node; p->data = data[i]; p->next = NULL; pre->next = p; pre = p; }
// 查找鏈表中 x 並記錄個數 node *p = head->next; while (p != NULL){ if (p->data == x){ count++; } p = p->next; }
// 將 x 插入鏈表第 pos 個位置 node *p = head; for (int i = 0; i < pos-1; i++){ p = p->next; } node *q = new node; q->data = x; q->next = p->next; p->next = q;
// 刪除鏈表中數據域爲 x 的結點 node *p = head->next; node *pre = head; while (p != NULL){ if (p->data = x){ pre->next = p->next; delete(p); p = pre->next; } else{ pre = p; p = p->next; } }