Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("date.in");
  6. ofstream g("date.out");
  7.  
  8. struct node{
  9. int inf;
  10. node *urm, *ant;
  11. };
  12.  
  13. node *prim, *ultim;
  14.  
  15. void creare(int x){
  16. prim = new node;
  17. prim -> inf = x;
  18. prim -> urm = NULL;
  19. prim -> ant = NULL;
  20. ultim = prim;
  21. }
  22.  
  23. void afisare(){
  24. node *p = prim;
  25. while(p != NULL){
  26. g << p -> inf << " ";
  27. p = p -> urm;
  28. }
  29. }
  30.  
  31. void inseraref(int x){
  32. node *p = new node;
  33. p -> inf = x;
  34. p -> ant = NULL;
  35. p -> urm = prim;
  36. prim -> ant = p;
  37. prim = p;
  38. }
  39.  
  40. void inserareb(int x){
  41. node *p = new node;
  42. p -> inf = x;
  43. p -> ant = ultim;
  44. p -> urm = NULL;
  45. ultim -> urm = p;
  46. ultim = p;
  47. }
  48.  
  49. void inserare(int x){
  50. node *p = prim;
  51. while(p != NULL){
  52. if(p -> inf <= x && p -> urm -> inf >= x){
  53. node *t = new node;
  54. t -> inf = x;
  55. t -> ant = p;
  56. t -> urm = p -> urm;
  57. p -> urm -> ant = t;
  58. p -> urm = t;
  59. break;
  60. }
  61. p = p -> urm;
  62. }
  63. }
  64.  
  65. void afisare2(){
  66. node *p = ultim;
  67.  
  68. while(p != NULL){
  69. g << p -> inf << " ";
  70. p = p -> ant;
  71. }
  72.  
  73. }
  74.  
  75. int main(){
  76. int x;
  77. f >> x;
  78. creare(x);
  79.  
  80. while(f >> x){
  81. if(x < prim -> inf)
  82. inseraref(x);
  83. else
  84. if(x > ultim -> inf)
  85. inserareb(x);
  86. else
  87. inserare(x);
  88. }
  89.  
  90. afisare2();
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement