fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class IntNode {
  5. public:
  6. IntNode(int value) {
  7. numVal = value;
  8. }
  9.  
  10. ~IntNode() {
  11. cout << numVal << endl;
  12. }
  13.  
  14. int numVal;
  15. IntNode* next;
  16. };
  17.  
  18. class IntLinkedList {
  19. public:
  20. IntLinkedList();
  21. ~IntLinkedList();
  22. void Prepend(int);
  23.  
  24. IntNode* head;
  25. };
  26.  
  27. IntLinkedList::IntLinkedList() {
  28. head = nullptr;
  29. }
  30.  
  31. IntLinkedList::~IntLinkedList() {
  32. while (head) {
  33. IntNode* next = head->next;
  34. delete head;
  35. head = next;
  36. }
  37. cout << "end of list" << endl;
  38. }
  39.  
  40. void IntLinkedList::Prepend(int dataValue) {
  41. IntNode* newNode = new IntNode(dataValue);
  42. newNode->next = head;
  43. head = newNode;
  44. }
  45.  
  46. int main() {
  47. IntLinkedList* list = new IntLinkedList();
  48.  
  49. list->Prepend(1);
  50. list->Prepend(3);
  51. list->Prepend(5);
  52. list->Prepend(7);
  53.  
  54. delete list;
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
7
5
3
1
end of list