fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int val;
  5. Node*next;
  6. };
  7. Node*InsertAtBegin(Node*root,int x)
  8. {
  9. Node*newnode=new Node();
  10. newnode->val=x;
  11. newnode->next=NULL;
  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,int x,int pos)
  25. {
  26. if(pos==0)
  27. {
  28. root=InsertAtBegin(root,x);
  29. }
  30. else
  31. {
  32. Node*newnode=new Node();
  33. newnode->val=x;
  34. newnode->next=NULL;
  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,int x)
  47. {
  48. Node*newnode=new Node();
  49. newnode->val=x;
  50. newnode->next=NULL;
  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. Node*SortedInsert(Node*root,int x)
  66. {
  67. Node*newnode=new Node();
  68. newnode->val=x;
  69. newnode->next=NULL;
  70. Node*currnode,*prevnode;
  71. currnode=root;
  72. prevnode=NULL;
  73. if(root==NULL)
  74. {
  75. root=newnode;
  76. return root;
  77. }
  78. if(x<root->val)
  79. {
  80. newnode->next=root;
  81. root=newnode;
  82. return root;
  83. }
  84. while(currnode!=NULL)
  85. {
  86. if(currnode->val<x)
  87. {
  88. prevnode=currnode;
  89. currnode=currnode->next;
  90. }
  91. else
  92. {
  93. prevnode->next=newnode;
  94. newnode->next=currnode;
  95. return root;
  96. }
  97. }
  98. prevnode->next=newnode;
  99. newnode->next=NULL;
  100. return root;
  101. }
  102. int Search(Node*root,int x)
  103. {
  104. int pos=0;
  105. Node*currnode;
  106. currnode=root;
  107. while(currnode!=NULL)
  108. {
  109. if(currnode->val==x)
  110. {
  111. return pos;
  112. }
  113. else
  114. {
  115. currnode=currnode->next;
  116. pos++;
  117. }
  118. }
  119. return -1;
  120. }
  121. Node*Delete(Node*root,int x)
  122. {
  123. Node*currnode,*prevnode;
  124. currnode=root;
  125. prevnode=NULL;
  126. while(currnode!=NULL)
  127. {
  128. if(currnode->val!=x)
  129. {
  130. prevnode=currnode;
  131. currnode=currnode->next;
  132. }
  133. if(currnode==root)
  134. {
  135. root=root->next;
  136. delete(currnode);
  137. }
  138. else
  139. {
  140. prevnode->next=currnode->next;
  141. delete(currnode);
  142. }
  143. break;
  144. }
  145. return root;
  146. }
  147. void Print(Node*root)
  148. {
  149. Node*currnode;
  150. currnode=root;
  151. while(currnode!=NULL)
  152. {
  153. cout<<currnode->val<<" ";
  154. currnode=currnode->next;
  155. }
  156. cout<<endl;
  157. }
  158. int main()
  159. {
  160. Node*root=NULL;
  161. root=InsertAtBegin(root,4);
  162. root=InsertAtBegin(root,6);
  163. root=InsertAtBegin(root,1);
  164. Print(root);
  165. root=InsertAtPos(root,9,0);
  166. root=InsertAtPos(root,3,3);
  167. Print(root);
  168. root=InsertAtEnd(root,10);
  169. root=InsertAtEnd(root,12);
  170. Print(root);
  171. cout<<"Position of 12:"<<Search(root,12)<<endl;
  172. root=Delete(root,9);
  173. root=Delete(root,15);
  174. Print(root);
  175. }
  176.  
  177.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
1 6 4 
9 1 6 3 4 
9 1 6 3 4 10 12 
Position of 12:6
1 3 4 10 12