// I\'m having trouble figuring out my enqueue method. I\'m not sure if I\'m usi
ID: 3556789 • Letter: #
Question
// I'm having trouble figuring out my enqueue method. I'm not sure if I'm using the proper algorithm to copy the data from data[] into myArray[]. The header file is at the bottom if you need it.
'farmingdale::statusCode farmingdale::queue::enqueue(int addMe){
if (isFull()){
// Allocate a new piece of memory (the new buffer should be double the size of the old one)
int *myArray = new int[buffersize * 2];
// Copy the old memory into the new one
int j = 0;
for (int i = oldestIndex; i!= nextInsertIndex; i = advanceIndex(i)){
myArray[i] = data[i];
j++;
}
nextInsertIndex = j;
// Fix your counters and bufferSize
// Delete [] the old memory
delete[] data;
// Change your link to the new memory
data = myArray;
//return FAILURE;
}
data[nextInsertIndex] = addMe;
nextInsertIndex = advanceIndex(nextInsertIndex); // advance and roll over if end-of-array
return SUCCESS;
}
// header file
#include<new>
#ifndef H_CH11QUEUEBASE
#define H_CH11QUEUEBASE
namespace farmingdale {
enum statusCode { SUCCESS, FAILURE }; // you will remove these when you create exceptions
class queue;
}
class farmingdale::queue {
private:
int expandSpace(int size);
static const int INITIAL_QUEUE_SIZE = 16; // This should remain the initial size of the dynamic array
int *data = new int[INITIAL_QUEUE_SIZE]; // This should become a dynamic array
int buffersize = INITIAL_QUEUE_SIZE;
int oldestIndex; // index of the oldest element in the queue
int nextInsertIndex; // index of the first empty spot
inline int queueCapacity() const { return INITIAL_QUEUE_SIZE; }
inline int advanceIndex(int index) const { return((index + 1) % queueCapacity()); }
public:
queue();
memory
queue(const queue& copyMe);
friend bool operator==(const queue & lhs, const queue & rhs);
bool operator<(const queue &rhs) const;
inline int minQueueSize() { return queueCapacity() - 1; }
inline bool isEmpty(){ return (oldestIndex == nextInsertIndex); }
inline bool isFull(){ return (advanceIndex(nextInsertIndex) == oldestIndex); }
statusCode enqueue(int addMe);
statusCode dequeue(int &returnedElement);
statusCode peek(int &returnedElement);
friend std::ostream& operator<< (std::ostream& stream, const queue& rhs);
};
bool operator>(const farmingdale::queue & lhs,
const farmingdale::queue & rhs);
bool operator>=(const farmingdale::queue & lhs,
const farmingdale::queue & rhs);
bool operator!=(const farmingdale::queue & lhs,
const farmingdale::queue & rhs);
bool operator<=(const farmingdale::queue & lhs,
const farmingdale::queue & rhs);
#endif // H_CH11QUEUEBASE
Explanation / Answer
'farmingdale::statusCode farmingdale::queue::enqueue(int addMe){
if (isFull()){
// Allocate a new piece of memory (the new buffer should be double the size of the old one)
int *myArray;
myArray= new int[buffersize * 2];
// Copy the old memory into the new one
int j = 0;
for (int i = oldestIndex; i!= nextInsertIndex; i = advanceIndex(i))
{
myArray[i] = data[i];
j++;
}
nextInsertIndex = j;
// Fix your counters and bufferSize
// Delete [] the old memory
delete[] data;
// Change your link to the new memory
data = myArray;
//return FAILURE;
}
data[nextInsertIndex] = addMe;
nextInsertIndex = advanceIndex(nextInsertIndex); // advance and roll over if end-of-array
return SUCCESS;
}
everything is correct, but make sure that data[] is passed to enqueue unless its scope is available to the enqueue function.. make it global otherwise.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.