Advertisement
NukeVsCity

Untitled

Apr 3rd, 2025 (edited)
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. Problema 8, 10p
  2.  
  3. #include <iostream>
  4.  
  5. class Deque {
  6. private:
  7.     int* data;
  8.     int capacity;
  9.     int front;
  10.     int back;
  11.     int count;
  12.  
  13.     void redimensioneaza() {
  14.         int new_capacity = capacity * 2;
  15.         int* new_data = new int[new_capacity];
  16.  
  17.         for (int i = 0; i < count; ++i) {
  18.             new_data[i] = data[(front + i) % capacity];
  19.         }
  20.  
  21.         delete[] data;
  22.         data = new_data;
  23.         capacity = new_capacity;
  24.         front = 0;
  25.         back = count;
  26.     }
  27.  
  28. public:
  29.     Deque(int initial_capacity = 4) {
  30.         capacity = initial_capacity;
  31.         data = new int[capacity];
  32.         front = 0;
  33.         back = 0;
  34.         count = 0;
  35.     }
  36.  
  37.     ~Deque() {
  38.         delete[] data;
  39.     }
  40.  
  41.     void adauga_sfarsit(int value) {
  42.         if (count == capacity) {
  43.             redimensioneaza();
  44.         }
  45.         data[back] = value;
  46.         back = (back + 1) % capacity;
  47.         count++;
  48.     }
  49.  
  50.     void adauga_inceput(int value) {
  51.         if (count == capacity) {
  52.             redimensioneaza();
  53.         }
  54.         front = (front - 1 + capacity) % capacity;
  55.         data[front] = value;
  56.         count++;
  57.     }
  58.  
  59.     void scoate_sfarsit() {
  60.         if (count == 0) return;
  61.         back = (back - 1 + capacity) % capacity;
  62.         count--;
  63.     }
  64.  
  65.     void scoate_inceput() {
  66.         if (count == 0) return;
  67.         front = (front + 1) % capacity;
  68.         count--;
  69.     }
  70.  
  71.     int marime() {
  72.         return count;
  73.     }
  74.  
  75.     bool e_gol() {
  76.         return count == 0;
  77.     }
  78.  
  79.     int la_pozitie(int index) {
  80.         if (index < 0 || index >= count) return -1;
  81.         return data[(front + index) % capacity];
  82.     }
  83.  
  84.     void insereaza(int index, int value) {
  85.         if (index < 0 || index > count) return;
  86.  
  87.         if (count == capacity) {
  88.             redimensioneaza();
  89.         }
  90.  
  91.         if (index < count / 2) {
  92.             front = (front - 1 + capacity) % capacity;
  93.             for (int i = 0; i < index; ++i) {
  94.                 data[(front + i) % capacity] = data[(front + i + 1) % capacity];
  95.             }
  96.             data[(front + index) % capacity] = value;
  97.         } else {
  98.             for (int i = count; i > index; --i) {
  99.                 data[(front + i) % capacity] = data[(front + i - 1) % capacity];
  100.             }
  101.             data[(front + index) % capacity] = value;
  102.             back = (back + 1) % capacity;
  103.         }
  104.  
  105.         count++;
  106.     }
  107.  
  108.     void sterge(int index) {
  109.         if (index < 0 || index >= count) return;
  110.  
  111.         if (index < count / 2) {
  112.             for (int i = index; i > 0; --i) {
  113.                 data[(front + i) % capacity] = data[(front + i - 1) % capacity];
  114.             }
  115.             front = (front + 1) % capacity;
  116.         } else {
  117.             for (int i = index; i < count - 1; ++i) {
  118.                 data[(front + i) % capacity] = data[(front + i + 1) % capacity];
  119.             }
  120.             back = (back - 1 + capacity) % capacity;
  121.         }
  122.  
  123.         count--;
  124.     }
  125. };
  126.  
  127. int main() {
  128.     Deque d;
  129.  
  130.     d.adauga_sfarsit(10);
  131.     d.adauga_inceput(20);
  132.     d.adauga_sfarsit(30);
  133.  
  134.     std::cout << "la_pozitie(0): " << d.la_pozitie(0) << "\n";
  135.     std::cout << "la_pozitie(1): " << d.la_pozitie(1) << "\n";
  136.     std::cout << "la_pozitie(2): " << d.la_pozitie(2) << "\n";
  137.  
  138.     d.insereaza(1, 99);
  139.     std::cout << "dupa insereaza la 1: " << d.la_pozitie(1) << "\n";
  140.  
  141.     d.sterge(2);
  142.     std::cout << "dupa sterge la 2: " << d.la_pozitie(2) << "\n";
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement