简体   繁体   English

C ++组合队列/堆栈的实现

[英]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. 对于我最新的作业,我应该实现一个“ quack”,它是循环队列和堆栈的组合。 Right now, I'm trying to wrap my head around the first two functions, pushFront, and pushBack. 现在,我正在尝试将头两个功能pushFront和pushBack包裹起来。 Here's an example of how they work. 这是它们如何工作的示例。

pushFront(a) [a-----] pushFront(a)[a -----]

pushFront(b) [a----b] 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,则需要将该项放置在数组的第一个元素中,然后再移回。

pushBack(a) [a-----] pushBack(a)[a -----]

pushBack(b) [ab----] pushBack(b)[ab ----]

Here is where I'm confused: 这是我很困惑的地方:

1.) Using modulo arithmetic to wrap front from item[0] to item[max -1]. 1.)使用取模算法将前部从item [0]包装到item [max -1]。 The only solution I can think of is creating an if statement that, upon reaching [0], moves front to [max-1]. 我能想到的唯一解决方案是创建一个if语句,当到达[0]时将其移至[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). 2.)为了使pushBack函数将值放置在item [0]中,它必须从item [max-1]开始(然后在放置该项之前“向后”移动)。 The problem is, if pushFront is called first, there's already an item in that location. 问题是,如果首先调用pushFront,则该位置已经有一个项目。

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. 3.)我考虑过使用while(item [back]!= null)来移动它,但是我稍后要写的pop函数似乎并没有真正从数组中删除项目。 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. 因为我的问题更具概念性,所以我认为这可能是解决问题的最佳方法。

I believe the function pushFront is supposed to push the next char into the front of the list. 我相信函数pushFront应该将下一个字符推入列表的前面。 so instead of 所以代替

pushFront(a) [a-----] pushFront(a)[a -----]

pushFront(b) [a----b] pushFront(b)[a ---- b]

you should get, 你应该得到

pushFront(a) [a-----] pushFront(a)[a -----]

pushFront(b) [ba----] pushFront(b)[ba ----]

pushFront(c) [cba---] pushFront(c)[cba ---]

pushBack(d) [cbad--] pushBack(d)[cbad--]

Either way you shouldn't need to use a modulus operator until you need to find the front and the back. 无论哪种方式,您都不需要使用模运算符,直到需要找到正面和背面。

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. 从您的解释中我们了解到,您具有固定大小的max元素缓冲区,并且必须在前面和后面添加元素。

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 . 因此,您必须跟踪两个索引: startend 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: 1)为了巧妙地规避这些情况,模运算有助于:

(end + 1) % max results in end+1 if it's smaller than max or 0 if it's exactly max . (end + 1) % max效果在end+1 ,如果它小于max0 ,如果它是恰好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): 2)考虑到这种观点,您的后退操作是(伪代码):

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). 从某种意义上说,pushfront是类似的,您必须在开始时或在开始之前添加一个元素(取决于结构是否为empy或是否已经有元素),然后递减开始(因此,一个小技巧:使用无符号整数类型,例如size_t表示)您的索引,以便在从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. 3)考虑到这种逻辑,3不再是问题。

4) But there is an open issue that you'll have to think about: 4)但是,您需要考虑一个未解决的问题:

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. 遵循此逻辑时,开始时您将end == start都设为0。当您的结构已满时,请再次开始end == start,因此您必须考虑如何处理此问题。 The easiest way to make the difference is to keep a count of the elements. 产生差异的最简单方法是对元素进行计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM