Queue with dynamically created array of user-defined type

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.

This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  // in songQ.h
#ifndef SONGQ_H
#define SONGQ_H

#include <iostream>
#include "song.h"
#define ARRAY_SIZE 5
using namespace 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.

Thanks in advance
Last edited on
Consider this:
1
2
3
4
5
6
7
8
9
10
11
		Item *tmpItem = songArr;

		maxSize += 5;

		songArr = new Item[maxSize];

		for (int i = 0; i < size; i++)
		{
			songArr[i] = tmpItem[i];
		}
		delete[] tmpItem;
Thanks @coder777. That fixes the problem.

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

 Max size of q now 10
 Size of q 6

Last edited on
Please post the relevant code to reproduce the problem.

isFull() certainly is not the problem. Maybe the stack is corrupt.
Topic archived. No new replies allowed.