fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node
  5. {
  6. public:
  7. int data;
  8. Node* next;
  9. };
  10.  
  11. class CircularLinkedList
  12. {
  13. public:
  14. Node* head;
  15.  
  16. CircularLinkedList()
  17. {
  18. head = NULL;
  19. }
  20.  
  21. ~CircularLinkedList()
  22. {
  23. if (head)
  24. {
  25. Node *temp = head;
  26. do
  27. {
  28. Node *next = temp->next;
  29. delete temp;
  30. temp = next;
  31. }
  32. while (temp != head);
  33. }
  34. }
  35.  
  36. void appendNode(int newVal)
  37. {
  38. Node** temp = &head;
  39. if (head) {
  40. do {
  41. temp = &((*temp)->next);
  42. }
  43. while (*temp != head);
  44. }
  45. *temp = new Node;
  46. (*temp)->data = newVal;
  47. (*temp)->next = head;
  48. }
  49.  
  50. void displayData()
  51. {
  52. if (head)
  53. {
  54. Node* temp = head;
  55. do
  56. {
  57. cout << temp->data << " ";
  58. temp = temp->next;
  59. }
  60. while (temp != head);
  61. }
  62. }
  63. };
  64.  
  65. int main()
  66. {
  67. CircularLinkedList c;
  68. c.appendNode(10);
  69. c.appendNode(20);
  70. c.displayData();
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5416KB
stdin
Standard input is empty
stdout
10 20