fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6. int data;
  7. struct Node *next;
  8. };
  9.  
  10. struct Node *addToEmpty(struct Node *last, int data)
  11. {
  12. // This function is only for empty list
  13. if (last != NULL)
  14. return last;
  15.  
  16. // Creating a node dynamically.
  17. struct Node *temp =
  18. (struct Node*)malloc(sizeof(struct Node));
  19.  
  20. // Assigning the data.
  21. temp -> data = data;
  22. last = temp;
  23.  
  24. // Creating the link.
  25. last -> next = last;
  26.  
  27. return last;
  28. }
  29.  
  30. struct Node *addBegin(struct Node *last, int data)
  31. {
  32. if (last == NULL)
  33. return addToEmpty(last, data);
  34.  
  35. struct Node *temp =
  36. (struct Node *)malloc(sizeof(struct Node));
  37.  
  38. temp -> data = data;
  39. temp -> next = last -> next;
  40. last -> next = temp;
  41.  
  42. return last;
  43. }
  44.  
  45. struct Node *addEnd(struct Node *last, int data)
  46. {
  47. if (last == NULL)
  48. return addToEmpty(last, data);
  49.  
  50. struct Node *temp =
  51. (struct Node *)malloc(sizeof(struct Node));
  52.  
  53. temp -> data = data;
  54. temp -> next = last -> next;
  55. last -> next = temp;
  56. last = temp;
  57.  
  58. return last;
  59. }
  60.  
  61. struct Node *addAfter(struct Node *last, int data, int item)
  62. {
  63. if (last == NULL)
  64. return NULL;
  65.  
  66. struct Node *temp, *p;
  67. p = last -> next;
  68. do
  69. {
  70. if (p ->data == item)
  71. {
  72. temp = (struct Node *)malloc(sizeof(struct Node));
  73. temp -> data = data;
  74. temp -> next = p -> next;
  75. p -> next = temp;
  76.  
  77. if (p == last)
  78. last = temp;
  79. return last;
  80. }
  81. p = p -> next;
  82. } while(p != last -> next);
  83.  
  84. cout << item << " not present in the list." << endl;
  85. return last;
  86.  
  87. }
  88.  
  89. void traverse(struct Node *last)
  90. {
  91. struct Node *p;
  92.  
  93. // If list is empty, return.
  94. if (last == NULL)
  95. {
  96. cout << "List is empty." << endl;
  97. return;
  98. }
  99.  
  100. // Pointing to first Node of the list.
  101. p = last -> next;
  102.  
  103. // Traversing the list.
  104. do
  105. {
  106. cout << p -> data << " ";
  107. p = p -> next;
  108.  
  109. }
  110. while(p != last->next);
  111.  
  112. }
  113.  
  114. // Driven Program
  115. int main()
  116. {
  117. struct Node *last = NULL;
  118.  
  119. last = addToEmpty(last, 6);
  120. last = addBegin(last, 4);
  121. last = addBegin(last, 2);
  122. last = addEnd(last, 8);
  123. last = addEnd(last, 12);
  124. last = addAfter(last, 10, 8);
  125.  
  126. traverse(last);
  127.  
  128. return 0;
  129. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:9: fatal error: bits/stdc++.h: No such file or directory
 #include<bits/stdc++.h>
         ^~~~~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty