Advertisement
Josif_tepe

Untitled

Mar 5th, 2025
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int max_size = 1000;
  4. struct red {
  5.     int niza[max_size];
  6.     int front, rear;
  7.    
  8.     void init() {
  9.         front = 0;
  10.         rear = -1;
  11.     }
  12.    
  13.     int isEmpty() {
  14.         if(rear == -1) {
  15.             return 1;
  16.         }
  17.         else {
  18.             return 0;
  19.         }
  20.     }
  21.    
  22.     int isFull() {
  23.         if(rear == max_size - 1) {
  24.             return 1;
  25.         }
  26.         else {
  27.             return 0;
  28.         }
  29.     }
  30.    
  31.     void push(int x) {
  32.         if(isFull() == 1) {
  33.             cout << "Redot e poln!" << endl;
  34.             return;
  35.         }
  36.         rear++;
  37.         niza[rear] = x;
  38.     }
  39.    
  40.     int pop() {
  41.         if(isEmpty() == 1) {
  42.             cout << "Redot e prazen" << endl;
  43.             return -1;
  44.         }
  45.         int result = niza[front];
  46.         for(int i = front; i < rear; i++) {
  47.             niza[i] = niza[i + 1];
  48.         }
  49.         rear--;
  50.         return result;
  51.     }
  52.     int peek() {
  53.         if(isEmpty() == 1) {
  54.             cout << "Nema elementi vo redot" << endl;
  55.             return -1;
  56.         }
  57.         return niza[front];
  58.     }
  59. };
  60. int main() {
  61.  
  62.     red Q;
  63.     Q.init();
  64.    
  65.     Q.push(1);
  66.     Q.push(2);
  67.     Q.push(3);
  68.     Q.push(4);
  69.     Q.push(5);
  70.    
  71.    
  72.     while(Q.isEmpty() == 0) {
  73.         cout << Q.pop() << endl;
  74.     }
  75.    
  76.    
  77.     return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement