dude2099

Sir sortat

Oct 16th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nod
  6. {
  7.     int h;
  8.     nod* adr;
  9. };
  10.  
  11. int main()
  12. {
  13.     int n,a;
  14.     nod* prim, *q,*p,*r;
  15.     prim=new nod;
  16.     cout<<"Nr de elevi: "; cin>>n;
  17.     prim->h=INT_MAX; prim->adr=NULL;
  18.     for(int i=1;i<=n;i++)
  19.     {
  20.         cout<<"Ce inaltime? "; cin>>a;
  21.         q=new nod;
  22.         q->h=a;
  23.         if(a<prim->h)
  24.         {
  25.             q->adr=prim;
  26.             prim=q;
  27.         } else {
  28.             p=prim;
  29.             while(a>=p->h)
  30.             {
  31.                 r=p;
  32.                 p=p->adr;
  33.             }
  34.             q->adr=p;
  35.             r->adr=q;
  36.         }
  37.  
  38.     }
  39.     cout<<"Sirul sortat este: ";
  40.     p=prim;
  41.     for(int i=1;i<=n;i++)
  42.     {
  43.         cout<<p->h<<" ";
  44.         p=p->adr;
  45.     }
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment