edutedu

sortare insertie cu liste

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