fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. #include <iostream>
  6.  
  7. struct Node {
  8. int data;
  9. Node* next;
  10. };
  11.  
  12. class LinkedList {
  13. private:
  14. Node* head;
  15.  
  16. public:
  17. LinkedList() {
  18. head = nullptr;
  19. }
  20.  
  21. void insert(int value) {
  22. Node* newNode = new Node;
  23. newNode->data = value;
  24. newNode->next = nullptr;
  25.  
  26. if (head == nullptr) {
  27. head = newNode;
  28. } else {
  29. Node* temp = head;
  30. while (temp->next != nullptr) {
  31. temp = temp->next;
  32. }
  33. temp->next = newNode;
  34. }
  35. }
  36.  
  37. void remove(int value) {
  38. if (head == nullptr) {
  39. return;
  40. }
  41.  
  42. if (head->data == value) {
  43. Node* temp = head;
  44. head = head->next;
  45. delete temp;
  46. return;
  47. }
  48.  
  49. Node* prev = head;
  50. Node* curr = head->next;
  51.  
  52. while (curr != nullptr && curr->data != value) {
  53. prev = curr;
  54. curr = curr->next;
  55. }
  56.  
  57. if (curr != nullptr) {
  58. prev->next = curr->next;
  59. delete curr;
  60. }
  61. }
  62.  
  63. bool search(int value) {
  64. Node* temp = head;
  65. while (temp != nullptr) {
  66. if (temp->data == value) {
  67. return true;
  68. }
  69. temp = temp->next;
  70. }
  71. return false;
  72. }
  73.  
  74. void display() {
  75. Node* temp = head;
  76. while (temp != nullptr) {
  77. std::cout << temp->data << " ";
  78. temp = temp->next;
  79. }
  80. std::cout << std::endl;
  81. }
  82.  
  83. int size() {
  84. int count = 0;
  85. Node* temp = head;
  86. while (temp != nullptr) {
  87. count++;
  88. temp = temp->next;
  89. }
  90. return count;
  91. }
  92.  
  93. void clear() {
  94. while (head != nullptr) {
  95. Node* temp = head;
  96. head = head->next;
  97. delete temp;
  98. }
  99. }
  100. };
  101. return 0;
  102. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty