Advertisement
hopingsteam

Stiva cu liste

Jul 11th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include    <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Nod {
  6.     int numar;
  7.     Nod* next;
  8. };
  9.  
  10. bool eGol(Nod* top) {
  11.     return top == NULL;
  12. }
  13.  
  14. Nod* creeazaNod(int valoare)
  15. {
  16.     Nod* nodNou = new Nod();
  17.     nodNou -> numar = valoare;
  18.     nodNou -> next = NULL;
  19.     return nodNou;
  20. }
  21.  
  22. void adaugaNod(Nod* &top, int valoare)
  23. {
  24.     Nod* nodNou = creeazaNod(valoare);
  25.     nodNou -> next = top;
  26.     top = nodNou;
  27. }
  28.  
  29. int varfulStivei(Nod* top)
  30. {
  31.     if(eGol(top))
  32.         return 0;
  33.     return top -> numar;
  34. }
  35.  
  36. void afiseazaStiva(Nod* top)
  37. {
  38.     while(top != NULL)
  39.     {
  40.         cout << top -> numar << " ";
  41.         top = top -> next;
  42.     }
  43. }
  44.  
  45. void construieste(int V[], int n)
  46. {
  47.     Nod* stive[100]; int nMax = 0;
  48.  
  49.     adaugaNod(stive[0], V[0]);
  50.     nMax += 1;
  51.  
  52.     for(int i = 1; i < n; i++)
  53.     {
  54.         bool plasat = false;
  55.         for(int j = 0; j < nMax; j++)
  56.         {
  57.             if(V[i] < varfulStivei(stive[j]))
  58.             {
  59.                 adaugaNod(stive[j], V[i]);
  60.                 plasat = true;
  61.                 break;
  62.             }
  63.         }
  64.         if(plasat == false)
  65.         {
  66.             adaugaNod(stive[nMax], V[i]);
  67.             nMax += 1;
  68.         }
  69.     }
  70.  
  71.     for(int i = 0; i < nMax; i++)
  72.     {
  73.         cout << "Stiva " << i << " : ";
  74.         afiseazaStiva(stive[i]);
  75.         cout << "\n";
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     int V[10] = {3, 1, 2, 6, 5, 7, 4}, n = 7;
  82.     construieste(V, n);
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement