AAnonymous
这是个链表#include <cstdio>
struct StudentNode {
char name[10];
int rank;
int score;
StudentNode *pNext;
StudentNode() : name(""), rank(0), score(0), pNext(nullptr) {}
};
struct StudentLinkList {
StudentNode *pHead;
StudentLinkList() : pHead(new StudentNode) { }
~StudentLinkList() {
StudentNode *node = pHead;
while (node != nullptr) {
StudentNode *tmp = node->pNext;
delete node;
node = tmp;
}
}
void createLinkList(int n) {
printf("creating linklist with %d nodes\n", n);
StudentNode *node = pHead;
StudentNode *tmp;
for (auto i = 1; i <= n; ++i) {
tmp = new StudentNode;
printf(" >> input %dst node's infos: name, rank, score: ", i);
scanf("%s%d%d", tmp->name, &(tmp->rank), &(tmp->score));
node->pNext = tmp;
node = tmp;
tmp = nullptr;
}
printf("linklist created\n\n");
}
void showLinkList() {
printf("display all elements of linklist\n >> ");
StudentNode *node = pHead;
while (node->pNext != nullptr) {
printf("| %s | %d | %d | --> ", node->pNext->name, node->pNext->rank, node->pNext->score);
node = node->pNext;
}
printf("null\n\n");
}
};
int main() {
StudentLinkList linkList;
linkList.createLinkList(3);
linkList.showLinkList();
}C:\Users\xxx\CLionProjects\Test\cmake-build-debug\Test.execreating linklist with 3 nodes >> input 1st node's infos: name, rank, score: WANG 1 65 >> input 2st node's infos: name, rank, score: ZHANG 2 98 >> input 3st node's infos: name, rank, score: LI 3 85linklist createddisplay all elements of linklist >> | WANG | 1 | 65 | --> | ZHANG | 2 | 98 | --> | LI | 3 | 85 | --> nullProcess finished with exit code 0