fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int roll;
  5. float cg;
  6. Node*next;
  7. };
  8. Node*InsertAtBegin(Node*root,int roll,float cg)
  9. {
  10. Node*newnode=new Node();
  11. newnode->next=NULL;
  12. newnode->roll=roll;
  13. newnode->cg=cg;
  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*InsertAtPos(Node*root,int roll,float cg,int pos)
  27. {
  28. if(pos==0)
  29. {
  30. root=InsertAtBegin(root, roll,cg);
  31. }
  32. else
  33. {
  34. Node*newnode=new Node();
  35. newnode->next=NULL;
  36. newnode->roll=roll;
  37. newnode->cg=cg;
  38. Node*currnode;
  39. currnode=root;
  40. for(int i=1;i<pos;i++)
  41. {
  42. currnode=currnode->next;
  43. }
  44. newnode->next=currnode->next;
  45. }
  46. return root;
  47. }
  48. int Search(Node*root,int roll,float cg)
  49. {
  50. int pos=0;
  51. Node*currnode;
  52. currnode=root;
  53. while(currnode!=NULL)
  54. {
  55. if(currnode->roll==roll && currnode->cg==cg)
  56. {
  57. return pos;
  58. }
  59. else
  60. {
  61. currnode=currnode->next;
  62. pos++;
  63. }
  64. }
  65. return -1;
  66. }
  67. void Print(Node*root)
  68. {
  69. Node*currnode;
  70. currnode=root;
  71. while(currnode!=NULL)
  72. {
  73. cout<<currnode->roll<<currnode->cg<<" ";
  74. currnode=currnode->next;
  75. }
  76. cout<<endl;
  77. }
  78. int main()
  79. {
  80. Node*root=NULL;
  81. root=InsertAtBegin(root,3,3.49);
  82. root=InsertAtBegin(root,1,4.00);
  83. root=InsertAtBegin(root,4,3.63);
  84. Print(root);
  85. root=InsertAtPos(root,1,4.00,0);
  86. root=InsertAtPos(root,4,3.63,2);
  87. root=InsertAtPos(root,3,3.49,1);
  88. Print(root);
  89. cout<<"positions of 1,4.00:"<<Search(root,1,4.00)<<endl;
  90. cout<<"positions of 2,4.3:"<<Search(root,2,4.3)<<endl;
  91. }
  92.  
  93.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
43.63 14 33.49 
14 43.63 14 33.49 
positions of 1,4.00:0
positions of 2,4.3:-1