Advertisement
Guest User

CS260 Assignment 2 Array

a guest
Feb 20th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. # include <iostream>
  2.  
  3. class Array_List
  4. {
  5.     int front = 0;
  6.     int back = 0;
  7.     int capacity;
  8.     int size = 0;
  9.     int *array;
  10.  
  11.     public:
  12.         Array_List(int new_cap)
  13.         {
  14.            
  15.             capacity = new_cap;
  16.             array = new int[capacity];
  17.         }
  18.         bool enqueue(int val)
  19.         {
  20.             if (size == capacity)
  21.             {
  22.                 return false;
  23.             }
  24.             array[back % capacity] = val;
  25.             back++;
  26.             size++;
  27.             return true;
  28.         }
  29.  
  30.         int dequeue()
  31.         {
  32.             if (size == 0)
  33.             {
  34.                 return NULL;
  35.             }
  36.             int result = array[front % capacity];
  37.             front++;
  38.             size--;
  39.             return result;
  40.         }
  41. };
  42.  
  43. using std::cout;
  44. using std::endl;
  45.  
  46. int main()
  47. {
  48.     Array_List test = Array_List(5);
  49.     int should_be_null = test.dequeue();
  50.     if (should_be_null != NULL)
  51.     {
  52.         cout << "This list broke!" << endl;
  53.     }
  54.     test.enqueue(1);
  55.     test.enqueue(2);
  56.     test.enqueue(3);
  57.     test.enqueue(4);
  58.     test.enqueue(5);
  59.     bool should_be_false = test.enqueue(6);
  60.     if (should_be_false != false)
  61.     {
  62.         cout << "This list broke!" << endl;
  63.     }
  64.     int should_be_one = test.dequeue();
  65.     if (should_be_one != 1)
  66.     {
  67.         cout << should_be_one << endl;
  68.         cout << "This list broke!" << endl;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement