fork download
  1. #include <iostream>
  2.  
  3. struct Node{
  4. int value;
  5. Node* next;
  6. };
  7.  
  8. struct LinkedList{
  9. Node* head = nullptr ;
  10. void append(int);
  11. void printll();
  12. };
  13.  
  14. void LinkedList::append(int data){
  15. Node* cur = head;
  16. Node* tmp = new Node;
  17. tmp->value = data;
  18. tmp->next = nullptr;
  19.  
  20. if(!cur){
  21. head = tmp;
  22. }
  23. else{
  24. while(cur->next != nullptr){
  25. cur = cur->next;
  26. }
  27. cur->next = tmp;
  28. }
  29. }
  30.  
  31. void LinkedList::printll(){
  32. Node* cur = head;
  33. while(cur != nullptr){
  34. std::cout << cur->value << '\n';
  35. cur = cur->next;
  36. }
  37. }
  38.  
  39.  
  40. int main(){
  41. LinkedList LL;
  42. LL.append(5);
  43. LL.append(6);
  44. LL.append(7);
  45. LL.printll();
  46. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
5
6
7