fork(1) 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* newNode = new Node();
  39. newNode->data = newVal;
  40. newNode->next = NULL;
  41. if (head == NULL)
  42. {
  43. head = newNode;
  44. }
  45. else
  46. {
  47. Node* temp = head;
  48. while (temp->next != head)
  49. {
  50. temp = temp->next;
  51. }
  52. temp->next = newNode;
  53. }
  54. newNode->next = head;
  55. }
  56.  
  57. void displayData()
  58. {
  59. if (head)
  60. {
  61. Node* temp = head;
  62. do
  63. {
  64. cout << temp->data << " ";
  65. temp = temp->next;
  66. }
  67. while(temp != head);
  68. }
  69. }
  70. };
  71.  
  72. int main()
  73. {
  74. CircularLinkedList c;
  75. c.appendNode(10);
  76. c.appendNode(20);
  77. c.displayData();
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
10 20