入门:链表
链表
作者为蒟蒻高中牲,若有不妥之处请多多包含,并望于评论区指正,不胜感激!
目录:
链表是一种动态的数据结构,每一个节点存储两个数据: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;
}