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. return newnode;
  15.  
  16. }
  17. Node* InsertAtPos(Node* root, string s, int pos) {
  18.  
  19. if (pos == 0) {
  20. return 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 && curr!=NULL;i++) {
  29.  
  30. curr = curr->next;
  31. }
  32. if(curr==NULL)
  33. {
  34. cout<<"Position out of bounds"<<endl;
  35. delete newnode;
  36. return root;
  37. }
  38. newnode->next = curr->next;
  39. curr->next = newnode;
  40.  
  41. return root;
  42. }
  43.  
  44.  
  45. Node* InsertAtEnd(Node* root, string s) {
  46. Node* newnode = new Node();
  47. newnode->s = s;
  48. newnode->next = NULL;
  49.  
  50. if (root == NULL) {
  51. return newnode;
  52. }
  53.  
  54. Node* curr = root;
  55. while (curr->next != NULL) {
  56. curr = curr->next;
  57. }
  58. curr->next = newnode;
  59.  
  60. return root;
  61. }
  62.  
  63.  
  64. Node* SortedInsert(Node* root, string s)
  65. {
  66. Node* newnode = new Node();
  67. newnode->s = s;
  68. newnode->next = NULL;
  69.  
  70. if(root==NULL || s<root->s)
  71. {
  72. newnode->next=root;
  73. return newnode;
  74. }
  75. Node* curr=root;
  76. while(curr->next!=NULL && curr->next->s<s)
  77. {
  78. curr=curr->next;
  79. }
  80. newnode->next=curr->next;
  81. curr->next=newnode;
  82. return root;
  83. }
  84. int Search(Node* root, string s) {
  85. int pos = 0;
  86. Node* curr = root;
  87.  
  88. while (curr != NULL) {
  89. if (curr->s == s) {
  90. return pos;
  91. }
  92. curr = curr->next;
  93. pos++;
  94. }
  95.  
  96. return -1;
  97. }
  98. Node*Delete(Node*root,string s)
  99. {
  100. if(root==NULL)
  101. {
  102. cout<<"List is empty"<<endl;
  103. return root;
  104. }
  105. if(root->s==s)
  106. {
  107. Node*temp=root;
  108. root=root->next;
  109. delete temp;
  110. return root;
  111. }
  112. Node*curr=root;
  113. while(curr->next!=NULL && curr->next->s!=s)
  114. {
  115. curr=curr->next;
  116. }
  117. if(curr->next==NULL)
  118. {
  119. cout<<"Value is not found"<<endl;
  120. return root;
  121. }
  122. Node*temp=curr->next;
  123. curr->next=temp->next;
  124. delete temp;
  125. return root;
  126. }
  127.  
  128. void DeleteList(Node*&root)
  129. {
  130. while(root!=NULL)
  131. {
  132. Node*temp=root;
  133. root=root->next;
  134. delete temp;
  135. }
  136. }
  137.  
  138.  
  139.  
  140. void Print(Node* root) {
  141. Node* curr = root;
  142. while (curr != NULL) {
  143. cout << curr->s << " ";
  144. curr = curr->next;
  145. }
  146. cout << endl;
  147. }
  148.  
  149. int main() {
  150. Node* root = NULL;
  151.  
  152.  
  153. root = InsertAtBegin(root, "data");
  154. root = InsertAtBegin(root, "cse");
  155. root = InsertAtBegin(root, "Math");
  156. Print(root);
  157.  
  158. root = InsertAtPos(root, "hello", 2);
  159. Print(root);
  160.  
  161. root = InsertAtEnd(root, "end");
  162. Print(root);
  163.  
  164. root = SortedInsert(root, "alpha");
  165. Print(root);
  166.  
  167. cout<<"positions of cse:"<<Search(root,"cse")<<endl;
  168. cout<<"positions of I:"<<Search(root,"I")<<endl;
  169.  
  170. root = Delete(root, "hello");
  171. root=Delete(root,"yoU");
  172. Print(root);
  173.  
  174. DeleteList(root);
  175.  
  176. return 0;
  177. }
Success #stdin #stdout 0s 5292KB
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
Value is not found
Math alpha cse data end