I have built a queue class that handles some songs.
Program specs:
Queue has data members
- pointer to an array
- size of array
- index of first and last element in the array
Member functions
Enter data last in the queue
Delete item from the beginning
When a queue object is created, it should be able to handle 5 songs, if there is a need for more songs, let the size increase by 5.
// in songQ.h
#ifndef SONGQ_H
#define SONGQ_H
#include <iostream>
#include "song.h"
#define ARRAY_SIZE 5
usingnamespace std;
typedef Song Item;
class SongQ
{
private:
Item *songArr;
int front; // index of queue's first element
int back; // index of queue's last element
int size;
int maxSize;
// member functions
public:
SongQ();
~SongQ();
void insertBack(Item &item);
void deleteFront();
bool isEmpty();
bool isFull();
int getSize() const;
const SongQ &operator=(const SongQ &q); // copy constructor
};
// in songQ.cpp
#include "songQ.h"
// Default constructor
SongQ::SongQ(): maxSize(5)
{
front = 0;
back = -1;
size = 0;
songArr = new Item[maxSize];
}
// Destructor
SongQ::~SongQ()
{
delete[] songArr;
songArr = nullptr;
}
// insert an item at the back of the queue
void SongQ::insertBack(Item & item)
{
songArr[++back] = item;
// THIS IS MY PROBLEM. When size is 5, program crasches. I need help here
if (isFull())
{
// create a new item, copy songArr to the item, delete songArr, point songArr to
// the new item with the new size
Item *tmpItem;
tmpItem = new Item[size];
for (int i = 0; i < size; i++)
{
songArr[i] = tmpItem[i];
}
delete[] songArr;
maxSize += 5;
tmpItem[maxSize] = *songArr;
}
}
// Delete an item from the front
void SongQ::deleteFront()
{
if (isEmpty())
{
cout << " Cannot delete from an empty queue!" << endl;
}
else
{
cout << songArr[front++] << endl; // i cout it because of specs
}
}
// Check if queue is empty
bool SongQ::isEmpty()
{
return (front > back);
}
// check that queue is full
bool SongQ::isFull()
{
return back ==maxSize;
}
// get size of array
int SongQ::getSize() const
{
return back - front + 1;
}
// copy constructor to copy one queue to the other
const SongQ & SongQ::operator=(const SongQ & q)
{
if (this != &q) // check that q is not the same as the other array
{
delete[]songArr; // delete current array
songArr = new Song[q.size]; // create space in the memory for the new array
size = q.size;
// copy elements for elements
for (int i = 0; i < q.size; i++)
songArr[i] = q.songArr[i];
}
return *this;
}
As commented, i need help with increase the array size to 5 when the size is five or when array becomes full.
But, i wonder why this function doen't get me the front value. It captures an empty space and prints it out
1 2 3 4 5 6 7 8 9 10 11 12 13
// Delete an item from the front
void Queue::deleteFront()
{
if (isEmpty())
{
cout << " Cannot delete from an empty queue!" << endl;
}
else
{
cout << songArr[front++] << endl;
}
}
// I get
Front | |0
// instead of
Jungele|B Gir|185
// songTitle(string), songArtist(string), songPlayTime(int)
In addition, the program runs successfully at first execution. When i try to re-run the program, it crashes and debugger sends me to check my isFull() function which is
1 2 3 4 5
// check that queue is full
bool Queue::isFull()
{
return back == maxSize;
}
I have tried
1 2 3 4 5 6
bool Queue::isFull()
{
return back % maxSize == 0;
}
// At least with this, the program can be re-run many times but maxSize is 10 from start
But after inserting 4 items in the queue, i get
Size of q 4
Max size of q now 10
And after adding 2 more items in the queue, size is now correctly reported as 6 but maxSize seems to have been 10 from the beginning