constructor: set capacity to 1 and used to 0.
enqueue: increment used.
memory: if(currentCapacity >= capacity)
used+1 == 1. >= trips on =.
error printed.
looks to me like it needs be > not >=. do you agree?
also for your own sanity I would have memory do this:
newCapacity = max((currentCapacity * 3) / 2 + 1, 100); //skip constant reallocation for small amounts of input. typically, when you make a new container, the first thing you do is bomb it with a bunch of data, and you want that to go quickly. 100 items is tiny, but in the first 100 or so items, you reallocated around 10 times! It starts to spread out fast after that. 100ish to 1000ish costs MUCH less than 1-100. Better than the max thing, just adjust your starting values.
You can easily remove line 105/6. It is not a problem when currentCapacity < capacity.
You can remove line 81 as well since the memory does not shrink.
However you have a problem when back exceeds the capacity. And When dequeue() is called back points to the wrong postition. I don't see the use of back at all. usedCapacity and capacity already do what you want.
Other comments:
- Your copy constructor leaves usedCapacity uninitialized.
- Your assignment operator doesn't set capacity.
- enqueue() should call memory(usedCapacity+1) before assigning to arr
- Shouldn't showFullQueue() show to usedCapacity? or maybe you should have a separate method that shows the used queue.
Finally, your implementation is REALLY inefficient because every time someone dequeues an item, you shift the entire queue down one position. In an implementation like this, the idea is that the front and back pointers always point to the first and last items, but the items wrap around so arr[usedCapacity-1] is followed by arr[0]. When you try to insert an item and back == front, you increase the capacity.
also, the function being public and having a parameter makes it similar to .resize(), but you don't allocate what the parameter ask for, but a 50% more...
if you intent to only using with .push(), then make it private and with no arguments.
> Here is the rest of the code
yay, it only took you a day
Go to the Forum main page. Click on the 'New Topic' button on the top right. Write the first draft of your post. You may need to subsequently edit the post to add formatting.
This will be a long answer. I hope you understand everything I am saying and I make no mistakes and mislead you. I won't write the correct code for you but I will guide you through the issues. You need to think and solve them
Issue Number 1
At end of second memory function call the Capacity=4 and usedcapacity=2 which is enqueue("B"). And when memory function is called third time which is for enqueue("C"), the current capacity=3 and capacity=4 so if codition gives allocation error. If instead of (currentcapacity*3)/2 +1 it should be just +1.
This will resolve a few allocation errors. Now You made the dequeue Function based on same logic.
If I change (currentcapacity*3)/2 +1 to currentcapacity + 1 and remove all dequeue function calls, than there are no errors upto first showfullqueue().
I dont know why you are using (currentcapacity*3)/2 +1 but if you can change it to just currentcapacity + 1 than using only enqueue will give no allocation error if you dont use a deqeue.
Your dequeue is based on your old memory function's logic. So changing (currentcapacity*3)/2 +1 to currentcapacity + 1 will not resolve three allocation errors because of three dequeue function calls
So please rethink your memory function based on what I said
Queue names, names2 will create these two queues.After the execution of this line their Default Constructors has been Called and They are created. Their Constructor Cannot be called again. Copy constructor and "=" operator are same but they are two different things. Copy constructor can only be used for creation of object and copying value of another object in it at the same time. "=" operator can be used after creation.
So do Queue names at start and Do Queue names2=names where you are doing names2=names.
Before changing this there was exception at names2=names.
After this The code will work till first enqeue for names2. at second enqueue now you have an exception.
Once you have changed your memory function. try the thing people commented above. Hope it solves the problem.
And There is much much much easier way to do this. Its using vector. Instead of creating a new array everytime. Its much more efficient and effective. Reply if you are interested in learning a new way. Vector will take only ten minutes to learn and you will no longer need all the usedcapacity ,capacity, shift and memory function etc.