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->next=NULL;
  11. newnode->val=x;
  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->next=NULL;
  34. newnode->val=x;
  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*SortedInsert(Node*root,int x)
  47. {
  48. Node*newnode=new Node();
  49. newnode->next=NULL;
  50. newnode->val=x;
  51. Node*currnode,*prevnode;
  52. currnode=root;
  53. prevnode=NULL;
  54. if(root==NULL)
  55. {
  56. root=newnode;
  57. return root;
  58. }
  59. if(x<root->val)
  60. {
  61. newnode->next=root;
  62. root=newnode;
  63. return root;
  64. }
  65. while(currnode!=NULL)
  66. {
  67. if(currnode->val<x)
  68. {
  69. prevnode=currnode;
  70. currnode=currnode->next;
  71. }
  72. else
  73. {
  74. prevnode->next=newnode;
  75. newnode->next=currnode;
  76. return root;
  77. }
  78. }
  79. prevnode->next=newnode;
  80. newnode->next=NULL;
  81. return root;
  82. }
  83. int Search(Node*root,int x)
  84. {
  85. int pos=0;
  86. Node*currnode;
  87. currnode=root;
  88. while(currnode!=NULL)
  89. {
  90. if(currnode->val==x)
  91. {
  92. return pos;
  93. }
  94. else
  95. {
  96. currnode=currnode->next;
  97. pos++;
  98. }
  99. }
  100. return -1;
  101. }
  102.  
  103. Node*Delete(Node*root,int x)
  104. {
  105. Node*currnode,*prevnode;
  106. currnode=root;
  107. prevnode=NULL;
  108. while(currnode!=NULL)
  109. {
  110. if(currnode->val!=x)
  111. {
  112. prevnode=currnode;
  113. currnode=currnode->next;
  114. }
  115. else
  116. {
  117. if(currnode==root)
  118. {
  119. root=root->next;
  120. delete(currnode);
  121. }
  122. else
  123. {
  124. prevnode->next=currnode->next;
  125. delete(currnode);
  126. }
  127. break;
  128. }
  129. }
  130. return root;
  131. }
  132. void Print(Node*root)
  133. {
  134. Node*currnode;
  135. currnode=root;
  136. while(currnode!=NULL)
  137. {
  138. cout<<currnode->val<<" ";
  139. currnode=currnode->next;
  140. }
  141. cout<<endl;
  142. }
  143. int main()
  144. {
  145. Node*root=NULL;
  146. root=InsertAtBegin(root,4);
  147. root=InsertAtBegin(root,7);
  148. root=InsertAtBegin(root,2);
  149. Print(root);
  150. root=InsertAtPos(root,9,3);
  151. root=InsertAtPos(root,1,0);
  152. root=InsertAtPos(root,5,2);
  153. Print(root);
  154. root=SortedInsert(root,3);
  155. root=SortedInsert(root,8);
  156. root=SortedInsert(root,6);
  157. Print(root);
  158. cout<<"position of 1:"<<Search(root,1)<<endl;
  159. cout<<"positions of 12:"<<Search(root,12)<<endl;
  160. root=Delete(root,8);
  161. root=Delete(root,11);
  162. Print(root);
  163. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2 7 4 
1 2 5 7 4 9 
1 2 3 5 6 7 4 8 9 
position of 1:0
positions of 12:-1
1 2 3 5 6 7 4 9