学习版
【C语言】
#include<iostream>
using namespace std;
typedef struct LNode {char data;struct LNode* next;LNode(char x) :data(x), next(nullptr) {}
}LNode;
void creatlist(LNode *&L) {int n;char e;cin >> n;LNode* p1, * p2;p1 = L;for (int i = 1; i <= n; i++) {cin >> e;p2 = new LNode(e);p2->next = p1->next;p1->next = p2;}
}
void listprint(LNode* L) {L = L->next;while (L) {cout << L->data << " ";L = L->next;}
}
int main()
{LNode* L;L = new LNode('/0');creatlist(L);listprint(L);
}
【C++】
#include<iostream>class LinkedList {
public:struct LinkedNode {char val;LinkedNode* prev;LinkedNode* next;LinkedNode(char x) :val(x), prev(nullptr), next(nullptr) {};};LinkedList(){dummyHead = new LinkedNode('0');tail = nullptr;}void initList(int n) {char c;LinkedNode* cur = dummyHead;while (n--) {std::cin >> c;LinkedNode* newNode = new LinkedNode(c);cur->next = newNode;newNode->prev = cur;cur = cur->next;}tail = cur;}void printList() {LinkedNode* cur = tail;while (cur!= dummyHead) {std::cout << cur->val << " ";cur = cur->prev;}}
private:LinkedNode* dummyHead;LinkedNode* tail;
};
int main() {LinkedList list;int n;std::cin >> n;list.initList(n);list.printList();return 0;
}
【STL】
#include<iostream>
#include<vector>int main() {int n;char c;std::cin >> n;std::vector<char> vec(n);for (int i = 0; i < n; i++) std::cin >> vec[i];for (int i = vec.size() - 1; ~i; i--) std::cout << vec[i] << " ";return 0;
}