fork download
  1. //
  2. // main.cpp
  3. // Linked List
  4. //
  5. // Created by Himanshu on 12/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. struct node {
  12. int info = 0;
  13. struct node *next;
  14. };
  15. typedef struct node Node;
  16.  
  17. node* newNode(int data)
  18. {
  19. node* Node = new node();
  20. Node->info = data;
  21. Node->next = NULL;
  22.  
  23. return(Node);
  24. }
  25.  
  26. void printLinkedList (Node* head) {
  27. Node *x = head;
  28.  
  29. while (x != NULL) {
  30. cout<<(x->info)<<" ";
  31. x = x->next;
  32. }
  33. cout<<endl;
  34. }
  35.  
  36. Node* insertNode (Node *head, Node* x) {
  37. x->next = head;
  38. head = x;
  39. return head;
  40. }
  41.  
  42. Node* searchNode (Node *head, int k) {
  43. Node* x = head;
  44.  
  45. while (x != NULL && x->info != k) {
  46. x = x->next;
  47. }
  48.  
  49. if (x != NULL && x->info == k) {
  50. return x;
  51. } else {
  52. return NULL;
  53. }
  54. }
  55.  
  56. Node* insertAtPosition (Node *head, Node* x, int k) {
  57. Node* node = head;
  58.  
  59. while (node != NULL && node->info != k) {
  60. node = node->next;
  61. }
  62.  
  63. if (node != NULL) {
  64. x->next = node->next;
  65. node->next = x;
  66. }
  67. return head;
  68. }
  69.  
  70.  
  71. Node* deleteNode (Node *head, int k) {
  72. Node* x = head, *prev = NULL;
  73.  
  74. if (x != NULL && x->info == k) {
  75. head = x->next;
  76.  
  77. //To prevent Memory Leak
  78. delete (x);
  79. return head;
  80. }
  81.  
  82. while (x != NULL && x->info != k) {
  83. prev = x;
  84. x = x->next;
  85. }
  86.  
  87. // Deleting a node, requires the node
  88. // previous to the node to be deleted.
  89. if (x != NULL) {
  90. prev->next = x->next;
  91.  
  92. //To prevent Memory Leak
  93. delete (x);
  94. }
  95. return head;
  96. }
  97.  
  98. int main() {
  99. Node *head = newNode(5);
  100. head = insertNode (head, newNode(4));
  101. head = insertNode (head, newNode(3));
  102. head = insertNode (head, newNode(2));
  103. head = insertNode (head, newNode(1));
  104.  
  105. cout<<"Linked List:"<<endl;
  106. printLinkedList(head);
  107.  
  108. cout<<"Searching Node 3"<<endl;
  109. if (searchNode(head, 3)) {
  110. cout<<"Node 3 is present"<<endl;
  111. }
  112.  
  113. cout<<"Searching Node 6 "<<endl;
  114. if (!searchNode(head, 6)) {
  115. cout<<"Node 6 is not present"<<endl;
  116. }
  117.  
  118. cout<<"Inserting Node 6 after 5 "<<endl;
  119. head = insertAtPosition(head, newNode(6), 5);
  120.  
  121. cout<<"Linked List:"<<endl;
  122. printLinkedList(head);
  123.  
  124. cout<<"Deleting Node 1"<<endl;
  125. head = deleteNode(head, 1);
  126.  
  127. cout<<"Linked List:"<<endl;
  128. printLinkedList(head);
  129.  
  130. cout<<"Deleting Node 6"<<endl;
  131. head = deleteNode(head, 6);
  132.  
  133. cout<<"Linked List:"<<endl;
  134. printLinkedList(head);
  135.  
  136. return 0;
  137. }
  138.  
  139.  
Success #stdin #stdout 0.01s 5464KB
stdin
Standard input is empty
stdout
Linked List:
1 2 3 4 5 
Searching Node 3
Node 3 is present
Searching Node 6 
Node 6 is not present
Inserting Node 6 after 5 
Linked List:
1 2 3 4 5 6 
Deleting Node 1
Linked List:
2 3 4 5 6 
Deleting Node 6
Linked List:
2 3 4 5