fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node {
  5. string s;
  6. Node* next;
  7. };
  8.  
  9.  
  10. Node* InsertAtBegin(Node* root, string s) {
  11. Node* newnode = new Node();
  12. newnode->s = s;
  13. newnode->next = root;
  14. root = newnode;
  15. return root;
  16. }
  17. Node* InsertAtPos(Node* root, string s, int pos) {
  18.  
  19. if (pos == 0) {
  20. root=InsertAtBegin(root, s);
  21. }
  22.  
  23. Node* newnode = new Node();
  24. newnode->s = s;
  25. newnode->next = NULL;
  26.  
  27. Node* curr = root;
  28. for (int i = 1; i < pos; i++) {
  29.  
  30. curr = curr->next;
  31. }
  32. newnode->next = curr->next;
  33. curr->next = newnode;
  34.  
  35. return root;
  36. }
  37.  
  38.  
  39. Node* InsertAtEnd(Node* root, string s) {
  40. Node* newnode = new Node();
  41. newnode->s = s;
  42. newnode->next = NULL;
  43.  
  44. if (root == NULL) {
  45. return newnode;
  46. }
  47.  
  48. Node* curr = root;
  49. while (curr->next != NULL) {
  50. curr = curr->next;
  51. }
  52. curr->next = newnode;
  53.  
  54. return root;
  55. }
  56.  
  57.  
  58. Node* SortedInsert(Node* root, string s)
  59. {
  60. Node* newnode = new Node();
  61. newnode->s = s;
  62. newnode->next = NULL;
  63.  
  64. Node* curr = root;
  65. Node* prev = NULL;
  66. if(root==NULL)
  67. {
  68. root=newnode;
  69. return root;
  70. }
  71. if(s<root->s)
  72. {
  73. newnode->next=root;
  74. root=newnode;
  75. return root;
  76. }
  77.  
  78. while (curr != NULL) {
  79. if(curr->s<s)
  80. {
  81. prev = curr;
  82. curr = curr->next;
  83. }
  84. else
  85. {
  86.  
  87. prev->next = newnode;
  88. newnode->next = curr;
  89.  
  90. return root;
  91. }
  92. }
  93. prev->next=newnode;
  94. newnode->next=NULL;
  95. return root;
  96. }
  97.  
  98. int Search(Node* root, string s) {
  99. int pos = 0;
  100. Node* curr = root;
  101.  
  102. while (curr != NULL) {
  103. if (curr->s == s) {
  104. return pos;
  105. }
  106. curr = curr->next;
  107. pos++;
  108. }
  109.  
  110. return -1;
  111. }
  112.  
  113.  
  114. Node* Delete(Node* root, string s)
  115. {
  116. Node* curr;
  117. Node* prev;
  118.  
  119.  
  120. curr=root;
  121. prev=NULL;
  122. while(curr!=NULL)
  123. {
  124. if(curr->s!=s)
  125. {
  126. prev=curr;
  127. curr=curr->next;
  128. }
  129. else
  130. {
  131. if (curr == root) {
  132. root = root->next;
  133. delete(curr);
  134. }
  135. else
  136. {
  137. prev->next = curr->next;
  138.  
  139. delete (curr);
  140. }
  141. break;
  142. }
  143. }
  144. return root;
  145. }
  146.  
  147.  
  148. void Print(Node* root) {
  149. Node* curr = root;
  150. while (curr != NULL) {
  151. cout << curr->s << " ";
  152. curr = curr->next;
  153. }
  154. cout << endl;
  155. }
  156.  
  157. int main() {
  158. Node* root = NULL;
  159.  
  160.  
  161. root = InsertAtBegin(root, "data");
  162. root = InsertAtBegin(root, "cse");
  163. root = InsertAtBegin(root, "Math");
  164. Print(root);
  165.  
  166. root = InsertAtPos(root, "hello", 2);
  167. Print(root);
  168.  
  169. root = InsertAtEnd(root, "end");
  170. Print(root);
  171.  
  172. root = SortedInsert(root, "alpha");
  173. Print(root);
  174.  
  175. cout<<"positions of cse:"<<Search(root,"cse")<<endl;
  176. cout<<"positions of I:"<<Search(root,"I")<<endl;
  177.  
  178. root = Delete(root, "hello");
  179. root=Delete(root,"yoU");
  180. Print(root);
  181.  
  182.  
  183.  
  184. return 0;
  185. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Math cse data 
Math cse hello data 
Math cse hello data end 
Math alpha cse hello data end 
positions of cse:2
positions of I:-1
Math alpha cse data end