Advertisement
Josif_tepe

Untitled

Feb 26th, 2025
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int maxn = 1000;
  5.  
  6. struct stek {
  7.     int niza[maxn];
  8.     int idx;
  9.    
  10.     void init() {
  11.         idx = -1;
  12.     }
  13.    
  14.     void push(int x) {
  15.         if(idx + 1 >= maxn) {
  16.             cout << "NEMA DOVOLNO KAPACITET" << endl;
  17.             return;
  18.         }
  19.         idx++;
  20.         niza[idx] = x;
  21.     }
  22.    
  23.     int pop() {
  24.         if(idx == -1) {
  25.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  26.             return -1;
  27.         }
  28.         int result = niza[idx];
  29.         idx--;
  30.         return result;
  31.     }
  32.    
  33.     int top() {
  34.         if(idx == -1) {
  35.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  36.             return -1;
  37.         }
  38.         return niza[idx];
  39.     }
  40.    
  41.     int size() {
  42.         return idx + 1;
  43.     }
  44.    
  45.     int isEmpty() {
  46.         if(idx == -1) {
  47.             return 1;
  48.         }
  49.         else {
  50.             return 0;
  51.         }
  52.     }
  53. };
  54. int main() {
  55.     int n;
  56.     cin >> n;
  57.    
  58.     stek A;
  59.     A.init();
  60.    
  61.     stek B;
  62.     B.init();
  63.     for(int i = 0; i < n; i++) {
  64.         int pacient;
  65.         cin >> pacient;
  66.        
  67.         if(pacient == 2) {
  68.             A.push(2);
  69.         }
  70.         else {
  71.             B.push(pacient);
  72.         }
  73.     }
  74.     cout << "B: ";
  75.     while(B.isEmpty() == 0) {
  76.         int x = B.pop();
  77.        
  78.         if(x == 1) {
  79.             A.push(x);
  80.         }
  81.         else {
  82.             cout << x << " ";
  83.         }
  84.     }
  85.     cout << endl;
  86.    
  87.     cout << "A: " << endl;
  88.     while(A.isEmpty() == 0) {
  89.         int x = A.pop();
  90.         cout << x << " ";
  91.     }
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement