Advertisement
dimon-torchila

Untitled

Dec 15th, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. namespace lab1{
  6.     class Queue{
  7.     public:
  8.         Queue(int size) : length(size), arr(new int[size]), begin(0), count(0), end(-1){ }
  9.         ~Queue(){ delete []arr;}
  10.         void push(int el){
  11.             if(isFull())
  12.                 throw runtime_error("going beyond the boundaries of the array");
  13.             ++end%=length;
  14.             arr[end] = el;
  15.             count++;
  16.         }
  17.         void pop(){
  18.             if(!isEmpty()){
  19.                 arr[begin] = INT_MIN;
  20.                 ++begin%=length;
  21.                 count--;
  22.             } else {
  23.                 throw runtime_error("going beyond the boundaries of the array");
  24.             }
  25.         }
  26.         int front(){
  27.             return arr[begin];
  28.         }
  29.         bool isEmpty(){ return size() == 0; }
  30.         bool isFull() { return size() == count; }
  31.         int size() { return length; }
  32.  
  33.         void sort(){
  34.             for(int i = begin; i < begin + count - 1; ++i){
  35.                 int min = i%length;
  36.                 for(int j = i + 1; j < begin + count; ++j){
  37.                     if(arr[j%length] < arr[min])
  38.                         min = j%length;
  39.                 }
  40.                 swap(arr[i%length], arr[min]);
  41.             }
  42.         }
  43.  
  44.         void print(){
  45.             for(int i = begin; i < begin + count; ++i)
  46.                 cout << arr[i%length] << ' ';
  47.             cout << endl;
  48.         }
  49.  
  50.     private:
  51.         int *arr;
  52.         int length;
  53.         int begin;
  54.         int end;
  55.         int count;
  56.     };
  57. }
  58.  
  59. #ifndef UNTITLED1_LAB1_H
  60. #define UNTITLED1_LAB1_H
  61.  
  62. #endif //UNTITLED1_LAB1_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement