fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct list
  6. {
  7. int value;
  8. list* next;
  9. };
  10.  
  11. list* addNewElement(list* p_head, int elems)
  12. {
  13. if (elems >= 1)
  14. {
  15. list* p_list = new list;
  16.  
  17. cout << "Enter a value: ";
  18. cin >> p_list->value;
  19.  
  20. p_list->next = addNewElement(p_head, elems - 1);
  21. return p_list;
  22. }
  23. return p_head;
  24. }
  25.  
  26. void printList(list* p_head)
  27. {
  28. list* p_cur = p_head;
  29.  
  30. cout << "ELEMENTS: " << endl;
  31.  
  32. while (p_cur != NULL)
  33. {
  34. cout << p_cur->value;
  35. p_cur = p_cur->next;
  36. }
  37.  
  38. cout << endl;
  39. }
  40.  
  41. int main()
  42. {
  43. list* p_head = NULL;
  44.  
  45. int elemNR;
  46.  
  47. cout << "Enter how many elements do you want in the list: ";
  48. cin >> elemNR;
  49.  
  50. p_head = addNewElement(p_head, elemNR);
  51.  
  52. cout << endl;
  53.  
  54. printList(p_head);
  55.  
  56. cout << endl;
  57.  
  58. cout << "PRESS <ENTER> TO CONTINUE...";
  59.  
  60. cin.ignore();
  61. cin.get();
  62. }
Success #stdin #stdout 0s 3476KB
stdin
4
1111
2222
3333
4444
stdout
Enter how many elements do you want in the list: Enter a value: Enter a value: Enter a value: Enter a value: 
ELEMENTS: 
1111222233334444

PRESS <ENTER> TO CONTINUE...