User: redempvfx

  • Public profile

User profile: redempvfx

User info
User name:redempvfx
Bio:#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;

bool perish(const string & msg);

class Queue
{
friend ostream& operator<<(ostream& os, Queue& obj);
public:
Queue(); //Constructor
Queue(const Queue & source); //Copy Constructor
~Queue();

void enqueue(int value);
int dequeue();
void showFullQueue();

Queue & operator=(const Queue & source); //Assignament operator
void memory(int currentCapacity); //Dynamic Memory Allocation
private:
void shift();
const static int front = 0;
int back;
int capacity;
int usedCapacity;
int * arr;
};


Queue::Queue()
{
back = -1;
capacity = 1;
usedCapacity = 0;
arr = new int[capacity]();
}

Queue::Queue(const Queue & source)
{
cout << "Invoking copy constructor" << endl;
this->back = source.back;
this->capacity = source.capacity;
this->usedCapacity = source.usedCapacity;
arr = new int[source.capacity]();
copy(source.arr, source.arr + source.capacity, this->arr);
}

Queue::~Queue()
{
delete[] arr;
}

void Queue::enqueue(int value)
{
back++;
usedCapacity++;
arr[back] = value;
memory(usedCapacity);
}

int Queue::dequeue()
{
if (back == front)
{
perish("can't remove from an empty queue");
}
int returnValue = arr[front];
shift();
usedCapacity--;
memory(usedCapacity);
return returnValue;

}

void Queue::showFullQueue()
{
cout << "Front: " << front << endl;
cout << "Back: " << back << endl;
for (int i = 0; i < usedCapacity; i++)
{
cout << arr[i] << " ";
}

}

Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
{
return *this;
}
cout << "Invoking Assignament Operator" << endl;
Queue temp(source);
swap(temp.back, back);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, usedCapacity);
swap(temp.arr, arr);
return *this;
}

void Queue::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3) / 2 + 1;
int * arr2;
try {
arr2 = new int[newCapacity];
}
catch (const bad_alloc &)
{
perish("Queue::add: allocation failure");
}
copy(arr, arr + capacity, arr2);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
}

void Queue::shift()
{
for (int i = front; i < back; i++)
{
arr[i] = arr[i + 1];
}
--back;
}


ostream & operator<<(ostream & os, Queue & obj)
{
os << "Back: " << obj.arr[obj.back] << " Front: " << obj.arr[obj.front] << " Capacity: " << obj.capacity
<< " Used Capacity: " << obj.usedCapacity << endl << "Inside of Queue: ";
for (int i = 0; i < obj.capacity; i++)
{
os << obj.arr[i] << " ";
}
os << endl;
return os;
}

bool perish(const string & msg)
{
cerr << endl << "Fatal Error: " << msg << endl;
exit(EXIT_FAILURE);
}
History
Joined:
Number of posts:6
Latest posts:

Assignament operator for a queue not working properly
well, I seem to have fixed my problem.

Assignament operator for a queue not working properly
[b]Queue.h [/b][code] #ifndef Queue_H #define Queue_H #include <iostream> #include <cstring> #incl...

Assignament operator for a queue not working properly
I've been playing around with dynamic allocation and queue recently. Keep in mind that I am fairly n...

What's wrong with this pointer?
So I'm breaking my head here. I've been reading up and down and I just can't figure out why my progr...

mingw Compiler: Getting an error when trying to compile this calculator program.
My mistake. I fixed it. I apoligize.

This user does not accept Private Messages