青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Circular Buffer

轉載自:http://www.vias.org/cppcourse/chap20_05.html

Another common implementation of a queue is a circular buffer. "Buffer" is a general name for a temporary storage location, although it often refers to an array, as it does in this case. What it means to say a buffer is "circular" should become clear in a minute.

The implementation of a circular buffer is similar to the array implementation of a stack, as in Section 19.7. The queue items are stored in an array, and we use indices to keep track of where we are in the array. In the stack implementation, there was a single index that pointed to the next available space. In the queue implementation, there are two indices:first points to the space in the array that contains the first customer in line and next points to the next available space.

The following figure shows a queue with two items (represented by dots).

There are two ways to think of the variables first and last. Literally, they are integers, and their values are shown in boxes on the right. Abstractly, though, they are indices of the array, and so they are often drawn as arrows pointing to locations in the array. The arrow representation is convenient, but you should remember that the indices are not references; they are just integers.

Here is an incomplete array implementation of a queue:

class Queue { 
    public

      pvector<Node> array; 

      int first, next; 

      Queue () { 
          array.resize (128); 
          first = 0; 
          next = 0; 
      } 

      bool empty () { 
        return first == next; 
      } 

The instance variables and the constructor are straightforward, although again we have the problem that we have to choose an arbitrary size for the array. Later we will solve that problem, as we did with the stack, by resizing the array if it gets full.

The implementation of empty is a little surprising. You might have thought that first == 0 would indicate an empty queue, but that neglects the fact that the head of the queue is not necessarily at the beginning of the array. Instead, we know that the queue is empty if head equals next, in which case there are no items left. Once we see the implementation of insert and remove, that situation will more more sense.

    void insert (Node node) { 
        array[next] = node; 
        next++; 
    } 

    Node remove () { 
        first++; 
        return array[first-1]; 
    } 

insert looks very much like push in Section 19.7; it puts the new item in the next available space and then increments the index.

remove is similar. It takes the first item from the queue and then increments first so it refers to the new head of the queue. The following figure shows what the queue looks like after both items have been removed.

It is always true that next points to an available space. If first catches up with next and points to the same space, thenfirst is referring to an "empty" location, and the queue is empty. I put "empty" in quotation marks because it is possible that the location that first points to actually contains a value (we do nothing to ensure that empty locations contain null); on the other hand, since we know the queue is empty, we will never read this location, so we can think of it, abstractly, as empty.

As an exercise, fix remove so that it returns null if the queue is empty.

The next problem with this implementation is that eventually it will run out of space. When we add an item we incrementnext and when we remove an item we increment first, but we never decrement either. What happens when we get to the end of the array?

The following figure shows the queue after we add four more items:

The array is now full. There is no "next available space," so there is nowhere for next to point. One possibility is that we could resize the array, as we did with the stack implementation. But in that case the array would keep getting bigger regardless of how many items were actually in queue. A better solution is to wrap around to the beginning of the array and reuse the spaces there. This "wrap around" is the reason this implementation is called a circular buffer.

One way to wrap the index around is to add a special case whenever we increment an index:

        next++; 
        if (next == array.length()) next = 0; 

A fancy alternative is to use the modulus operator:

        next = (next + 1) % array.length(); 

Either way, we have one last problem to solve. How do we know if the queue is really full, meaning that we cannot insert another item? The following figure shows what the queue looks like when it is "full."

There is still one empty space in the array, but the queue is full because if we insert another item, then we have to increment next such that next == first, and in that case it would appear that the queue was empty!

To avoid that, we sacrifice one space in the array. So how can we tell if the queue is full?

        if ((next + 1) % array.length == first) 

And what should we do if the array is full? In that case resizing the array is probably the only option.

As an exercise, put together all the code from this section and write an implementation of a queue using a circular buffer that resizes itself when necessary.


posted on 2009-10-27 11:49 楊粼波 閱讀(886) 評論(0)  編輯 收藏 引用

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            午夜天堂精品久久久久| 欧美成人r级一区二区三区| 欧美日韩福利在线观看| 欧美一区二区免费观在线| 亚洲综合999| 亚洲欧美制服中文字幕| 午夜精品视频一区| 久久精彩免费视频| 久久久午夜精品| 欧美激情第二页| 欧美日韩视频不卡| 国产精品午夜电影| 影音先锋另类| 一二三四社区欧美黄| 亚洲欧美一区二区在线观看| 羞羞漫画18久久大片| 久久一区亚洲| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美一区二区三区播放老司机| 亚洲欧美一区二区三区久久| 久久夜色精品一区| 欧美日韩在线不卡| 国产在线播放一区二区三区| 亚洲精品免费看| 久久精品国产精品亚洲| 亚洲黄网站在线观看| 一区二区成人精品| 久久亚洲一区二区三区四区| 国产精品vvv| 久久综合给合| 亚洲免费久久| 妖精视频成人观看www| 校园激情久久| 亚洲精品激情| 久久久久久夜精品精品免费| 国产精品久久久久9999高清| 在线成人黄色| 欧美一区二区三区免费观看| 亚洲日本中文| 欧美v日韩v国产v| 狠狠久久婷婷| 欧美在线三区| 在线一区二区日韩| 欧美精品电影| 亚洲另类自拍| 亚洲国产精品久久久久秋霞不卡| 欧美在线观看一二区| 国产精品区一区| 亚洲欧美日韩精品久久久| 亚洲激情啪啪| 欧美电影在线播放| 亚洲国产精品视频| 免播放器亚洲| 另类亚洲自拍| 亚洲国产一区二区三区青草影视| 久久成人18免费网站| 一区二区三区蜜桃网| 欧美韩日一区二区| 日韩一级精品视频在线观看| 亚洲高清视频一区二区| 男女视频一区二区| 亚洲三级影片| 日韩视频一区| 国产精品久久久久影院色老大 | 亚洲少妇自拍| 日韩亚洲视频在线| 国产精品高潮久久| 亚洲欧美日韩精品久久久| 宅男精品视频| 国产日韩欧美一区二区三区四区| 欧美在线一二三四区| 久久精品五月婷婷| 亚洲国产精品久久精品怡红院| 欧美成人精品一区二区| 免费成人小视频| 日韩一级视频免费观看在线| 亚洲精品综合| 国产精品亚洲一区| 久久一区亚洲| 欧美精品一区二区视频 | 亚洲高清在线精品| 欧美精品性视频| 亚洲一级片在线看| 午夜在线视频观看日韩17c| 国产真实乱偷精品视频免| 美女91精品| 欧美日本在线观看| 欧美在线电影| 国产精品日韩一区二区| 免费亚洲电影| 久久一区二区精品| 日韩亚洲视频在线| 亚洲综合色激情五月| 精品成人久久| 一区二区日韩伦理片| 激情偷拍久久| 中文av字幕一区| 亚洲成人在线视频播放| 99精品国产高清一区二区| 国户精品久久久久久久久久久不卡 | 亚洲国产精品va在线观看黑人| 亚洲国内自拍| 国产日韩欧美| 日韩一级免费| 亚洲福利小视频| 亚洲欧美国产精品va在线观看| 亚洲国产精品一区二区www在线| 在线一区二区三区四区| 亚洲电影在线| 午夜精品一区二区三区在线视| 亚洲国产日韩欧美综合久久| 一本色道久久综合亚洲精品不| 亚洲福利视频专区| 欧美一区二区性| 亚洲在线观看视频网站| 免费观看一级特黄欧美大片| 久久精品二区亚洲w码| 国产精品超碰97尤物18| 欧美国产成人在线| 狠狠色丁香久久综合频道 | 欧美高清视频| 老司机免费视频一区二区| 国产精品色婷婷久久58| 9色国产精品| 夜夜爽www精品| 欧美成人小视频| 欧美α欧美αv大片| 国内精品视频在线播放| 午夜国产精品影院在线观看 | 激情av一区二区| 欧美一区二区三区在线观看| 午夜精品久久久久久久99樱桃| 欧美日韩大片| 亚洲精品国精品久久99热一| 亚洲精品国产精品国自产观看| 久久精品亚洲乱码伦伦中文| 欧美一级午夜免费电影| 国产精品美女主播| 亚洲午夜视频在线| 在线一区亚洲| 欧美天天综合网| 中日韩在线视频| 午夜电影亚洲| 国户精品久久久久久久久久久不卡| 西瓜成人精品人成网站| 久久美女性网| 亚洲国产精选| 欧美巨乳波霸| 国产乱码精品一区二区三区不卡| 亚洲精品久久久久久久久久久久久 | 亚洲毛片在线看| 亚洲深夜激情| 国产美女精品在线| 久久精品二区亚洲w码| 久久综合网hezyo| 亚洲激情av| 欧美日韩综合不卡| 欧美一区二区三区啪啪| 欧美成人中文字幕| 亚洲四色影视在线观看| 国产日韩视频| 欧美国产日本| 午夜视频一区二区| 亚洲国产高清高潮精品美女| 亚洲一区尤物| 在线观看av不卡| 欧美视频在线视频| 久久国内精品视频| 亚洲国产视频一区| 香蕉国产精品偷在线观看不卡| 韩国在线一区| 欧美午夜精品电影| 久久精品国产一区二区电影| 亚洲国产日韩一区| 久久www成人_看片免费不卡| 亚洲精品日产精品乱码不卡| 国产精品一区久久久| 欧美成人免费小视频| 亚洲在线免费| 亚洲久久成人| 欧美成在线视频| 久久精品国产亚洲a| 亚洲美女在线一区| 国产在线观看91精品一区| 欧美日韩国产综合视频在线| 久久精品国产99精品国产亚洲性色| 亚洲人成在线播放网站岛国| 久久夜色精品国产欧美乱| 亚洲一区中文字幕在线观看| 亚洲三级电影全部在线观看高清| 国产一区二区三区自拍| 国产精品v日韩精品| 欧美激情无毛| 免费在线观看一区二区| 久久精品国亚洲| 欧美一区二区视频网站| 亚洲一二三四久久| 一区二区三区产品免费精品久久75| 欧美国产欧美亚州国产日韩mv天天看完整| 欧美一区二区|