Advertisement
cmiN

stack

Apr 2nd, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. const int nMax = 360;
  4. int stacks[nMax], index[nMax], nr, size;
  5.  
  6. void init()
  7. {
  8.     for (int i = 0; i < nr; i++) {
  9.         index[i] = -1;
  10.     }
  11. }
  12.  
  13. void insert(int k, int x)
  14. {
  15.     if (index[k] == size - 1) {
  16.         printf("Stiva %d e plina.", k); // daca indicele ultimului element adaugat in stiva k este
  17.     } else { // egal cu dimensiunea stivei minus 1 e clar ca e plina
  18.         index[k]++; // se actualizeaza indicele
  19.         stacks[k * size + index[k]] = x; // pe pozitia actualizata anterior + toate celelalte
  20.     } // elemente care au fost inaintea stivei curente se adauga elementul x
  21. }
  22.  
  23. int main()
  24. {
  25.     int k = 0; // o variabila initializata implicit ca sa nu dea eroare in expresia de la linia 32
  26.     do { // stam in bucla pana se citeste un numar corect de stive
  27.         printf("Numarul de stive (divide 360): ");
  28.         scanf("%d", &nr);
  29.     } while(nMax % nr);
  30.     size = nMax / nr; // se stabileste dimensiunea stivei
  31.     init(); // se atribuie valoarea -1 (stiva vida) lui index[k] unde k reprezinta stiva
  32.     bool vid = index[k] < 0 ? 1 : 0; // expresia
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement