Untitled
unknown
c_cpp
2 years ago
7.5 kB
6
Indexable
template <typename T>
T* cyclic_double_queue<T>::resize(size_t new_size)
{
/*copying old array into new array */
T* newArray = new T[new_size]();
m_backIndex = 0;
for (int i=0; i < m_size; i++) {
newArray[i] = m_data[m_frontIndex];
m_frontIndex++;
m_backIndex++;
if (m_frontIndex >= m_capacity) {
m_frontIndex = 0;
}
}
m_frontIndex = 0;
std :: cout << "I resized the array\n";
for(int i =0; i<new_size; i++){
std::cout << newArray[i] << " ";
}
std :: cout << "\n";
std :: cout << "Front = " << m_frontIndex << " and Back = " << m_backIndex << "\n";
return newArray;
}
template <typename T>
void cyclic_double_queue<T>::enqueue_front(const T& item)
{
//TODO: Remove following line and add your implementation here.
if (m_size == m_capacity) {
m_data = resize(m_capacity * m_increaseFactor);
m_capacity = m_capacity * m_increaseFactor;
}
if (m_frontIndex == 0) {
m_frontIndex = m_capacity - 1;
} else {
m_frontIndex--;
}
m_data[m_frontIndex] = item;
m_size++;
std :: cout << "I Enqueued front\n";
for(int i =0; i<m_capacity; i++){
std::cout << m_data[i] << " ";
}
std :: cout << "\n";
}
template <typename T>
void cyclic_double_queue<T>::enqueue_back(const T& item)
{
//TODO: Remove following line and add your implementation here.
if (m_size == m_capacity) {
m_data = resize(m_capacity * m_increaseFactor);
m_capacity = m_capacity * m_increaseFactor;
}
m_data[m_backIndex] = item;
m_backIndex++;
if(m_backIndex == m_capacity) {
m_backIndex = 0;
}
m_size++;
std :: cout << "I Enqueued back\n";
for(int i =0; i<m_capacity; i++){
std::cout << m_data[i] << " ";
}
std :: cout << "\n";
}
template <typename T>
T cyclic_double_queue<T>::dequeue_front()
{
//TODO: Remove following line and add your implementation here.
if (empty()) {
throw std::out_of_range("cyclic_double_queue is empty!");
}
T item = m_data[m_frontIndex];
m_frontIndex = (m_frontIndex + 1) % m_capacity;
m_size--;
std :: cout << "I Dequeued front\nNow, my front = "<<m_frontIndex << "and back = "<<m_backIndex << "\n";
for(int i =0; i<m_capacity; i++){
std::cout << m_data[i] << " ";
}
std :: cout << "\n";
if (m_capacity > 4 * m_size) {
if((m_capacity/m_decreaseFactor) > m_initialCapacity) {
m_data = resize((m_capacity/m_decreaseFactor));
m_capacity = m_capacity/m_decreaseFactor;
}
else {
m_data = resize(m_initialCapacity);
m_capacity = m_initialCapacity;
}
}
return item;
}
template <typename T>
T cyclic_double_queue<T>::dequeue_back()
{
//TODO: Remove following line and add your implementation here.
if (empty()) {
throw std::out_of_range("cyclic_double_queue is empty!");
}
if (m_backIndex == 0) {
m_backIndex = m_capacity - 1;
} else {
m_backIndex--;
}
T item = m_data[m_backIndex];
m_size--;
std :: cout << "I Dequeued back\n";
for(int i =0; i<m_capacity; i++){
std::cout << m_data[i] << " ";
}
std :: cout << "\n";
if (m_capacity > 4 * m_size) {
if((m_capacity/m_decreaseFactor) > m_initialCapacity) {
m_data = resize((m_capacity/m_decreaseFactor));
m_capacity = m_capacity/m_decreaseFactor;
}
else{
m_data = resize(m_initialCapacity);
m_capacity = m_initialCapacity;
}
}
return item;
}
template <typename T>
void cyclic_double_queue<T>::pop_front()
{
//TODO: Remove following line and add your implementation here.
dequeue_front();
}
template <typename T>
void cyclic_double_queue<T>::pop_back()
{
//TODO: Remove following line and add your implementation here.
dequeue_back();
}
template <typename T>
bool cyclic_double_queue<T>::empty() const
{
//TODO: Remove following line and add your implementation here.
if (m_size == 0) {
return true;
}
else {
return false;
}
}
template <typename T>
T& cyclic_double_queue<T>::front()
{
//TODO: Remove following line and add your implementation here.
if (empty()) {
throw std::out_of_range("cyclic_double_queue is empty!");
}
return m_data[m_frontIndex];
}
template <typename T>
T& cyclic_double_queue<T>::back()
{
//TODO: Remove following line and add your implementation here.
if (empty()) {
throw std::out_of_range("cyclic_double_queue is empty!");
}
if (m_backIndex == 0) {
return m_data[m_capacity - 1];
}
return m_data[m_backIndex - 1];
}
template <typename T>
void cyclic_double_queue<T>::clear()
{
//TODO: Remove following line and add your implementation here.
delete[] m_data;
m_capacity = m_initialCapacity;
m_data = new T[m_capacity];
m_frontIndex = 0;
m_backIndex = 0;
m_size = 0;
}
template <typename T>
std::string cyclic_double_queue<T>::print_status() const
{
//TODO: Remove following line and add your implementation here.
std::string result;
if(m_size == 0){
for(size_t i = 0; i<m_capacity; ++i){
result += "[-]";
}
}
else if(m_size == m_capacity){
for(size_t i =0; i<m_capacity; i++){
result += "[+]";
}
}
else {
if(m_backIndex < m_frontIndex){
for (size_t i = 0; i < m_capacity; ++i) {
if(i < m_backIndex && i < m_frontIndex){
result += "[+]";
}
else if(i < m_frontIndex){
result += "[-]";
}
else{
result += "[+]";
}
}
}
else{
for(size_t i =0; i<m_capacity; i++){
if(i >= m_frontIndex && i < m_backIndex){
result += "[+]";
}
else{
result += "[-]";
}
}
}
}
return result;
}
template <typename T>
size_t cyclic_double_queue<T>::get_size() const
{
//TODO: Remove following line and add your implementation here.
return m_size;
}
template <typename T>
size_t cyclic_double_queue<T>::get_capacity() const
{
//TODO: Remove following line and add your implementation here.
return m_capacity;
}
Editor is loading...
Leave a Comment