214 字
1 分钟
入门:链表
链表
作者为蒟蒻高中牲,若有不妥之处请多多包含,并望于评论区指正,不胜感激!
链表是一种动态的数据结构,每一个节点存储两个数据:data和next。data是数据,next是下一个节点的地址。
链表经常是首尾相接的。
算法竞赛中,链表常用STL 中的list而非手写,以节约时间。
这里给到Luogu P1996的题解示范list的用法。
#include <bits/stdc++.h>using namespace std;
int main() { int n, m; cin >> n >> m; list<int> node;
for (int i = 1; i <= n; ++i) node.push_back(i); // 建立列表 list<int>::iterator it = node.begin(); // 创建迭代器 while (node.size() > 1) { for (int i = 1; i < m; i++) { it++; if (it == node.end()) it = node.begin(); } cout << *it << " "; list<int>::iterator next = ++it; if (next == node.end()) next = node.begin(); node.erase(--it); it = next; } cout << *it; return 0;}