fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node
  4. {
  5. int val;
  6. Node*next;
  7. };
  8. Node*InsertAtBegin(Node*root,int x)
  9. {
  10. Node*newnode=new Node();
  11. newnode->next=NULL;
  12. newnode->val=x;
  13. if(root==NULL)
  14. {
  15. root=newnode;
  16. return root;
  17. }
  18. else
  19. {
  20. newnode->next=root;
  21. root=newnode;
  22. return root;
  23. }
  24. }
  25. Node*InsertAtPos(Node*root,int x,int pos)
  26. {
  27. if(pos==0)
  28. {
  29. root=InsertAtBegin(root,x);
  30. }
  31. else
  32. {
  33. Node*newnode=new Node();
  34. newnode->next=NULL;
  35. newnode->val=x;
  36. Node*currnode;
  37. currnode=root;
  38. for(int i=1;i<pos;i++)
  39. {
  40. currnode=currnode->next;
  41. }
  42. newnode->next=currnode->next;
  43. currnode->next=newnode;
  44. }
  45. return root;
  46. }
  47. Node*InsertAtEnd(Node*root,int x)
  48. {
  49. Node*newnode=new Node();
  50. newnode->next=NULL;
  51. newnode->val=x;
  52. if(root==NULL)
  53. {
  54. root=newnode;
  55. return root;
  56. }
  57. Node*currnode;
  58. currnode=root;
  59. while(currnode->next!=NULL)
  60. {
  61. currnode=currnode->next;
  62. }
  63. currnode->next=newnode;
  64. return root;
  65. }
  66. Node*SortedInsert(Node*root,int x)
  67. {
  68. Node*newnode=new Node();
  69. newnode->next=NULL;
  70. newnode->val=x;
  71. Node*currnode,*prevnode;
  72. currnode=root;
  73. prevnode=NULL;
  74. if(root==NULL)
  75. {
  76. root=newnode;
  77. return root;
  78. }
  79. if(x<root->val)
  80. {
  81. newnode->next=root;
  82. root=newnode;
  83. return root;
  84. }
  85. while(currnode!=NULL)
  86. {
  87. if(currnode->val<x)
  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,int x)
  104. {
  105. int pos=0;
  106. Node*currnode;
  107. currnode=root;
  108. while(currnode!=NULL)
  109. {
  110. if(currnode->val==x)
  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,int x)
  123. {
  124. Node*currnode,*prevnode;
  125. currnode=root;
  126. prevnode=NULL;
  127. while(currnode!=NULL)
  128. {
  129. if(currnode->val!=x)
  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<<currnode->val<<" ";
  158. currnode=currnode->next;
  159. }
  160. cout<<endl;
  161. }
  162. int main()
  163. {
  164. Node*root=NULL;
  165. root=InsertAtBegin(root,6);
  166. root=InsertAtBegin(root,2);
  167. root=InsertAtBegin(root,8);
  168. Print(root);
  169. root=InsertAtPos(root,9,0);
  170. root=InsertAtPos(root,5,3);
  171. root=InsertAtPos(root,1,4);
  172. Print(root);
  173. root=InsertAtEnd(root,7);
  174. root=InsertAtEnd(root,0);
  175. root=InsertAtEnd(root,3);
  176. Print(root);
  177. root=SortedInsert(root,4);
  178. root=SortedInsert(root,9);
  179. root=SortedInsert(root,2);
  180. Print(root);
  181. cout<<"Positions of 3:"<<Search(root,3)<<endl;
  182. cout<<"Positions of 13:"<<Search(root,13)<<endl;
  183. root=Delete(root,6);
  184. root=Delete(root,12);
  185. Print(root);
  186. }
  187.  
  188.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
8 2 6 
9 8 2 5 1 6 
9 8 2 5 1 6 7 0 3 
2 4 9 9 8 2 5 1 6 7 0 3 
Positions of 3:11
Positions of 13:-1
2 4 9 9 8 2 5 1 7 0 3