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