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

name:Nu
name:Mu
name:lu
name:pu
name:tu

name:Nu
name:Mu
name:lu
name:pu
name:tu
name:ku
name:yu

name:Nu
name:Mu
name:Xu
name:lu
name:pu
name:tu
name:ku
name:vu
name:yu

positions of pu:4
positions of du:-1
name:Mu
name:Xu
name:lu
name:pu
name:tu
name:ku
name:vu
name:yu