c++ Implementation of a combined queue/stack
For my latest homework assignment I'm supposed to implement a "quack" which is the combination of a circular queue and a stack. Right now, I'm trying to wrap my head around the first two functions, pushFront, and pushBack. Here's an example of how they work.
pushFront(a) [a-----]
pushFront(b) [a----b]
Conversely, if pushBack is called first, it needs to place the item in the first element of the array and then move back.
pushBack(a) [a-----]
pushBack(b) [ab----]
Here is where I'm confused:
1.) Using modulo arithmetic to wrap front from item[0] to item[max -1]. The only solution I can think of is creating an if statement that, upon reaching [0], moves front to [max-1].
2.) In order for the pushBack function to place the value in item[0], it has to start at item[max-1] (and then moves "backward" before placing the item). The problem is, if pushFront is called first, there's already an item in that location.
3.)I thought about using a while(item[back] != null) to move through it, but the pop functions I'll write later don't seem to actually remove items from the array. Instead, they just move the front and back locations to, in effect, shorten the quack. Help appreciated, and if you would like to see code for whatever reason please let me know. Because my issues are more conceptual, I thought that might be the best way to address the problem.
Your schema is somewhat confusing. I understand from your explanation that you have a fixed size bufer of max
elements, and that you have to add elements in the front and in the back.
For doing this in such a circular buffer, you need to know which is the first active element and which is the last. So you have to keep track of two indexes: start
and end
. The difficulty is that the contiguous active elements can span the borders of your buffer (ie the index of the start element is higher than the index of the end element.
empty:
+----+----+----+----+
! ! ! ! ! start=0, end=0
---------------------
pushback(a):
+----+----+----+----+
! a ! ! ! ! start=0, end=1
---------------------
pushback(b):
+----+----+----+----+
! a ! b ! ! ! start=0, end=2
---------------------
pushfront(c):
+----+----+----+----+
! a ! b ! ! c ! start=max-1, end=2
---------------------
after a log of push and pop front and back :
+----+----+----+----+
! ! x ! y ! z ! start=1, end=0
---------------------
1) to elegantly circumvent these situations, the modulto arithmetic helps:
(end + 1) % max
results in end+1
if it's smaller than max
or 0
if it's exactly max
. The modulo operator %
is very practical for managing a kind of circular arithmetic.
2) With this view in mind, your push back is (pseudo code):
check that buffer is not full
add the element at end index and increment the index as explained in 1.
The pushfront is similar in the sense that you have to add an element at start or before the start (depending if the structure is empy or if there are already elements) and decrement start (hence a tip: use unsigned integral type such as size_t for your indexes in order to be able to use modulo when decretementing from 0).
The fact that you have also stack operations, means that you
3) with this logic in mind, the 3 should no longer be a question.
4) But there is an open issue that you'll have to think about:
when following this logic, at the beginning you have end==start, both 0. When your structure will be full, end==start again, so you have to think how to handle this. The easiest way to make the difference is to keep a count of the elements.
I believe the function pushFront is supposed to push the next char into the front of the list. so instead of
pushFront(a) [a-----]
pushFront(b) [a----b]
you should get,
pushFront(a) [a-----]
pushFront(b) [ba----]
pushFront(c) [cba---]
pushBack(d) [cbad--]
Either way you shouldn't need to use a modulus operator until you need to find the front and the back.
链接地址: http://www.djcxy.com/p/79866.html下一篇: c ++实现组合队列/堆栈