0

I have been given an assignment and I'm struggling to figure out how I'm supposed to implement it.

I've pasted the parts of the assignment that puzzled me below

  1. Write a deque class to hold a list of integers which is implemented internally with a circular array The size of the array can be passed in the constructor, or you can decide on a default value. The class will maintain data members which hold the index position of the head and tail of the list The class should have member functions:
    • bool isEmpty();
    • bool isFull();
    • bool insertFront(int)
    • bool removeFront(int&)
    • bool insertBack(int)
    • bool removeBack(int&)

  2. prints all items in the array by removing them one at a time from the front.

So I've written all my function and have the deque working, the things I struggled with are:

  1. "The size of the array can be passed in the constructor" so to accomplish this I declared a pointer called array in my class and then array = new int[size] in my constructor, is this the only way to do this, I'm happy enough it works but not sure if there's a better solution. I was thinking vector, but think that would have been too easy. I also could have declared a const for the size and initialized the array in my class, but again to easy.

  2. The bool removeFront(int&) and bool removeBack(int&) functions really confused me, what reference am I supposed to be passing in? also the return type is bool, but later in the assignment I'm asked to "prints all items in the array by removing them one at a time from the front" how can I do this with a return type of bool, rather than int?

I have changed my functions to remove the reference and have a return type of int to get the code to work, but would like to know how to implement it the way the assignment asks for?

JTK
  • 1,469
  • 2
  • 22
  • 39

2 Answers2

1

Based on the requirements listed, the intent of the function arguments is unambiguous. Here is why:

Take

bool removeFront(int& );

This not only removes an element at the front of the buffer and stores it in the argument being passed by reference. But, the function returns a "bool" indicating whether it was able to successfully remove or not.

An example usage would be like this:

int elem;
while (removeFront(elem)) {
   printf("element : %d ", elem);
}

Here the variable "elem" is passed in by reference. Hence, upon a successful execution of removeFront() you will have elem filled in with the value of the element just removed.

The same reasoning applies to other similar methods. Please go back to using a reference mode parameter as given in the original specification.

The int& argument is not for a count of elements as other answer suggested.

Answer to Part-1:

Your solution is pretty decent. You could also

std::array for storing the elements. There is an advanced trick to do in-place allocation of a variable length array - but, that is beyond the scope of this question.

KalyanS
  • 527
  • 3
  • 8
  • Makes perfect sense now, I have adjusted my code accordingly. I'm happy enough to not delve into std::array, for now. – JTK Dec 06 '14 at 21:55
0

"The size of the array can be passed in the constructor"

Unless you were told otherwise, use a vector. Using old school arrays is just asking for trouble.

The "bool removeFront(int&)" and "bool removeBack(int&)" functions really confused me, what reference am I supposed to be passing in?

It's a matter of personal preference, but passing in a single int as a reference might be rather unnecessary, what the functions do (if I understood your problem correctly) is remove the element of the array that is at the position of the int you are passing as argument. If said element is correctly removed, you might want to return a true value, otherwise return a false one.

EDIT: Upon re reading the post, what the functions might do is simply remove the 'int' amount of elements from the front or back of the array. Return values should work as previously stated

but later in the assignment I'm asked to "prints all items in the array by removing them one at a time from the front" how can I do this with a return type of bool, rather than int?

The return type of the function has nothing to do with this (unless you were asked to do it recursively). Simply do a loop that starts at the beginning of the array and outputs its content, deletes that same element, then jumps to the next and repeats the process until its out of elements. Again, this is much safer to do with any of the STL containers since you can use iterators.

senex
  • 447
  • 3
  • 14
  • I believe, this suggestion is not going in the right direction. It is not supposed to be a count of elements to be removed from the queue at all.Please check the answer. – KalyanS Dec 06 '14 at 20:41
  • "prints all items in the array by removing them one at a time" – senex Dec 06 '14 at 20:44
  • I don't know you mean, but, printing can still be accomplished with the original interface. – KalyanS Dec 06 '14 at 20:46
  • Which you did not post in the original question. I recommend you post all the information relevant to the question from the begging. – senex Dec 06 '14 at 20:49