Guest User

Untitled

a guest
Jan 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include<iostream.h>
  2.  
  3. struct nod{
  4. int info;
  5. nod *urm;
  6. } ;
  7.  
  8. nod *p,*u,*c;
  9. int n;
  10.  
  11. void adauga(int *&p,int *&u, int x){
  12. nod * c;
  13. c = new nod;
  14. c->info=x;
  15. if(!p){
  16. p=new nod;
  17. p->info=x;
  18. p->urm=NULL;
  19. u=p;
  20. }else{
  21. c->urm=NULL;
  22. u->urm=c;
  23. u=c;
  24. }
  25. }
  26.  
  27. void afisare(nod *p){
  28. nod *c;
  29. cout << endl;
  30. for(c=p;c!=NULL;c=c->urm){
  31. cout << c->info << " ";
  32. }
  33. }
  34.  
  35. void main(){
  36. cin >> n;
  37. int i,x,gata=0;
  38. for(i=0;i<n;i++){
  39. cin>>x;
  40. adauga(p,u,x);
  41. }
  42.  
  43. nod *a,*b;
  44. int mod;
  45. while(gata==0){
  46. gata=1;
  47. mod=1;
  48. c=p;
  49. while(c->urm!=NULL){
  50. if(mod==0) c=c->urm;
  51. mod=0;
  52. if(p->urm==c)a=p;
  53. if(c->info > c->urm->info && c->urm!=NULL){
  54. if(c==p){
  55. b=c;
  56. p=c->urm;
  57. b->urm=p->urm;
  58. p->urm=b;
  59. c=b;
  60. a=p;
  61. }else{
  62. b=c;
  63. c=c->urm;
  64. a->urm=c;
  65. b->urm=c->urm;
  66. c->urm=b;
  67. c=b;
  68. if(c->urm==NULL) u=c;
  69. a=a->urm;
  70. }
  71.  
  72. gata=0;
  73. mod=1;
  74.  
  75. }else if(c!=p){
  76. a->urm=c;
  77. a=a->urm;
  78. }
  79. }
  80. }
  81.  
  82. afisare(p);
  83. }
Add Comment
Please, Sign In to add comment