fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4.  
  5. struct link{
  6. char data;
  7. std::unique_ptr<link> next;
  8. };
  9.  
  10. class LinkedList {
  11. private:
  12. std::unique_ptr<link> first;
  13. public:
  14.  
  15. void Set(const std::string& s){
  16.  
  17. for (auto c : s) {
  18. std::unique_ptr<link> node = std::move(first);
  19. first = std::make_unique<link>();
  20. first->data = c;
  21. first->next = std::move(node);
  22. }
  23. }
  24.  
  25. void display() const
  26. {
  27. for (const link* node = first.get(); node != nullptr; node = node->next.get()) {
  28. std::cout << node->data;
  29. }
  30. }
  31.  
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. LinkedList l;
  38. l.Set("123");
  39. l.display();
  40. }
  41.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
321