fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6. int data;
  7. node *next;
  8. };
  9.  
  10. class linked_list {
  11. private:
  12. node *head;
  13.  
  14. public:
  15. linked_list() {
  16. head = NULL;
  17. }
  18.  
  19. bool isempty() {
  20. return (head == NULL);
  21. }
  22.  
  23. void display() {
  24. node *dis = head;
  25. while (dis != NULL) {
  26. cout << dis->data << "\t";
  27. dis = dis->next;
  28. }
  29. cout << endl;
  30. }
  31.  
  32. bool search(int num) {
  33. node *ser = head;
  34. while (ser != NULL) {
  35. if (ser->data == num) {
  36. return true;
  37. }
  38. ser = ser->next;
  39. }
  40. return false;
  41. }
  42.  
  43. void addstart(int value) {
  44. node *newnode = new node;
  45. newnode->data = value;
  46. newnode->next = head;
  47. head = newnode;
  48. }
  49.  
  50. void addend(int value) {
  51. node *newnode = new node;
  52. newnode->data = value;
  53. newnode->next = NULL;
  54.  
  55. if (head == NULL) {
  56. head = newnode;
  57. } else {
  58. node *temp = head;
  59. while (temp->next != NULL) {
  60. temp = temp->next;
  61. }
  62. temp->next = newnode;
  63. }
  64. }
  65.  
  66. void Delete(int item) {
  67. node *temp1 = NULL;
  68. node *temp2 = head;
  69.  
  70. while (temp2 != NULL && temp2->data != item) {
  71. temp1 = temp2;
  72. temp2 = temp2->next;
  73. }
  74.  
  75. if (temp2 == NULL) {
  76. cout << "Item not found in the list." << endl;
  77. return;
  78. }
  79.  
  80. if (temp1 == NULL) {
  81. head = temp2->next;
  82. } else {
  83. temp1->next = temp2->next;
  84. }
  85.  
  86. delete temp2;
  87. cout << "Item " << item << " deleted from the list." << endl;
  88. }
  89. };
  90.  
  91. int main() {
  92. linked_list froo ;
  93. int x;
  94.  
  95. cout << "Enter a value to add at the beginning of the list: ";
  96. cin >> x;
  97. froo.addstart(x);
  98.  
  99. cout << "Enter a value to add at the end of the list: ";
  100. cin >> x;
  101. froo.addend(x);
  102.  
  103. cout << "Enter a value to search in the list: ";
  104. cin >> x;
  105. if (froo.search(x)) {
  106. cout << "Value " << x << " is found in the list." << endl;
  107. } else {
  108. cout << "Value " << x << " is not found in the list." << endl;
  109. }
  110.  
  111. cout << "Enter a value to delete from the list: ";
  112. cin >> x;
  113. froo.Delete(x);
  114.  
  115. cout << "Updated list:" << endl;
  116. froo.display();
  117.  
  118. return 0;
  119. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter a value to add at the beginning of the list: Enter a value to add at the end of the list: Enter a value to search in the list: Value 0 is found in the list.
Enter a value to delete from the list: Item 0 deleted from the list.
Updated list:
0