放三个人之后长度是多少
运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Node {
string name;
Node* next = nullptr;
};
struct LinkList {
Node* head = nullptr;
void append(const string& name) {
Node* nd = new Node{name};
if (head == nullptr) {
head = nd;
return;
}
Node* cur = head;
while (cur->next != nullptr) cur = cur->next;
cur->next = nd;
}
int size() const {
int n = 0;
for (Node* cur = head; cur != nullptr; cur = cur->next) n++;
return n;
}
};
int main() {
LinkList lk;
for (const string& x : {"阿岚", "小满", "阿泰"}) lk.append(x);
cout << lk.size() << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论