fork(1) download
  1. #include <iostream>
  2. #define MAX_ITEMS 10
  3. struct node
  4. {
  5. int value;
  6. node *nextNode;
  7. };
  8. node *front, *rear, *temp;
  9. void populateList(void);
  10. void delMemory(void);
  11. void printList(void);
  12. int main(void)
  13. {
  14. populateList();
  15. printList();
  16. delMemory();
  17. return 0;
  18. }
  19. void populateList(void)
  20. {
  21. for (auto x = 0; x < MAX_ITEMS; x++)
  22. {
  23. if (!front)
  24. {
  25. front = new node;
  26. (*front).nextNode = nullptr;
  27. rear = front;
  28. }
  29. else
  30. {
  31. (*rear).nextNode = new node;
  32. rear = (*rear).nextNode;
  33. (*rear).nextNode = nullptr;
  34. }
  35. (*rear).value = x;
  36. }
  37. }
  38. void printList(void)
  39. {
  40. auto itemsCount{0};
  41. if (!front)
  42. {
  43. std::cout<<"No items in the list"<<std::endl;
  44. }
  45. else
  46. {
  47. temp = front;
  48. while (temp)
  49. {
  50. std::cout<<"Node #"<<++itemsCount;
  51. std::cout<<" Value: "<<(*temp).value<<std::endl;
  52. temp = (*temp).nextNode;
  53. }
  54. }
  55. }
  56. void delMemory(void)
  57. {
  58. temp = front;
  59. while (temp)
  60. {
  61. temp = (*front).nextNode;
  62. delete front;
  63. front = temp;
  64. }
  65. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Node #1 Value: 0
Node #2 Value: 1
Node #3 Value: 2
Node #4 Value: 3
Node #5 Value: 4
Node #6 Value: 5
Node #7 Value: 6
Node #8 Value: 7
Node #9 Value: 8
Node #10 Value: 9