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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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 楊粼波 閱讀(890) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            黑人极品videos精品欧美裸| 亚洲福利视频二区| 欧美日韩日日夜夜| 久久成人综合视频| 国产美女精品免费电影| 日韩亚洲欧美综合| 久久福利资源站| 亚洲精品美女在线| 国产三级精品三级| 欧美日韩黄色一区二区| 亚洲欧美另类综合偷拍| 亚洲黄网站在线观看| 午夜精品久久久99热福利| 影院欧美亚洲| 黄色成人在线免费| 国产亚洲一区在线播放| 欧美日韩精品免费观看| 免播放器亚洲一区| 久久精品亚洲一区二区| 亚洲精品久久久久中文字幕欢迎你| 久久精品国产91精品亚洲| 亚洲男同1069视频| 亚洲欧美激情一区| 性伦欧美刺激片在线观看| 久久久国产成人精品| 久久婷婷丁香| 亚洲电影成人| 日韩一二在线观看| 午夜日韩视频| 久久精品成人欧美大片古装| 欧美一区二区三区在线看| 久久精品国产视频| 欧美成人午夜影院| 欧美精品久久一区| 欧美日韩国产va另类| 久久网站免费| 欧美在线精品一区| 韩国av一区二区三区四区| 欧美中文字幕视频| 亚洲日本激情| 亚洲欧美精品伊人久久| 久久在线播放| 欧美激情bt| 国产日韩欧美另类| 怡红院精品视频在线观看极品| 亚洲国产精品女人久久久| 亚洲精品日韩精品| 久久深夜福利免费观看| 亚洲啪啪91| 久久精品亚洲国产奇米99| 国产精品你懂的在线欣赏| 狠狠久久婷婷| 亚洲伊人久久综合| 久久在线精品| 久久国产黑丝| 国产亚洲欧洲| 99精品免费视频| 欧美激情一二区| 欧美一区二区视频97| 国产热re99久久6国产精品| 99精品国产在热久久| 欧美激情精品久久久久久蜜臀| 午夜久久久久久| 国产伦精品一区二区三区照片91 | 午夜精品久久一牛影视| 欧美大胆人体视频| 1024亚洲| 免费亚洲视频| 欧美激情影音先锋| 亚洲精品欧洲| 日韩一区二区福利| 国产一区二区三区久久精品| 欧美日韩视频在线一区二区| 最近中文字幕日韩精品| 另类av一区二区| 久久狠狠婷婷| 亚洲国产成人一区| 亚洲精品久久久久久久久久久久久 | 怡红院av一区二区三区| 久久免费黄色| 美女免费视频一区| 亚洲狼人综合| 亚洲午夜精品久久| 亚洲激情在线观看| 99ri日韩精品视频| 狠狠入ady亚洲精品经典电影| 亚洲国产一区二区三区a毛片| 欧美va亚洲va日韩∨a综合色| 一本色道久久88综合日韩精品| 性18欧美另类| 亚洲精品在线观| 国产精品99久久久久久人| 国产女主播视频一区二区| 欧美大香线蕉线伊人久久国产精品| 免费观看成人| 性色av一区二区三区在线观看| 免费高清在线一区| 久久久久免费视频| 欧美大片在线影院| 久久久精品2019中文字幕神马| 欧美国产在线观看| 蜜臀av性久久久久蜜臀aⅴ| 欧美日韩一区在线观看视频| 西西人体一区二区| 欧美精品 国产精品| 久久蜜桃精品| 国产精品欧美久久| 一区二区三区四区蜜桃| 亚洲电影免费在线| 欧美主播一区二区三区| 欧美怡红院视频一区二区三区| 欧美三级欧美一级| 亚洲精品社区| 亚洲在线视频| 亚洲精品永久免费精品| 亚洲第一福利视频| 女仆av观看一区| 亚洲国产成人久久综合| 一区二区三区在线看| 欧美一区二区视频免费观看 | 最新中文字幕一区二区三区| 狠狠狠色丁香婷婷综合激情| 欧美一级理论性理论a| 久久国产高清| 亚洲人成人一区二区三区| 久久另类ts人妖一区二区| 欧美激情亚洲国产| 在线视频免费在线观看一区二区| 免费在线亚洲| 亚洲一区激情| 伊人狠狠色丁香综合尤物| 欧美不卡视频一区发布| 亚洲一区影音先锋| 免费亚洲一区二区| 日韩一区二区电影网| 国产精品美女xx| 久久综合伊人77777麻豆| 亚洲欧美另类国产| 蜜桃精品久久久久久久免费影院| 日韩一区二区久久| 国产欧美精品一区| 欧美va天堂| 久久久久网址| 一区二区三区高清在线| 麻豆国产va免费精品高清在线| 影音先锋亚洲电影| 欧美视频在线一区二区三区| 免费久久99精品国产| 亚洲一区欧美激情| 欧美成年视频| 久久久美女艺术照精彩视频福利播放| 亚洲最新视频在线| 最新日韩精品| 国产在线播放一区二区三区| 国产麻豆成人精品| 欧美吻胸吃奶大尺度电影| 麻豆freexxxx性91精品| 亚洲视频在线观看免费| 一本大道久久a久久精二百| 亚洲久久成人| 最近看过的日韩成人| 欧美激情精品久久久久久黑人 | 欧美日韩亚洲91| 久久久中精品2020中文| 久久er99精品| 一区二区国产在线观看| 亚洲欧洲久久| 亚洲国产影院| 夜夜嗨av一区二区三区四季av| 亚洲国产精品一区二区第一页| 免费在线观看成人av| 老色鬼精品视频在线观看播放| 亚洲国产日韩一级| 亚洲国产成人在线播放| 久久综合色影院| 亚洲国产成人久久综合| 亚洲福利视频二区| 亚洲一区在线免费观看| 午夜久久资源| 久久久免费观看视频| 欧美激情一区二区| 欧美三级视频在线| 亚洲第一网站| 亚洲一区二区三区三| 欧美在线观看视频一区二区三区| 美女精品国产| 亚洲午夜伦理| 美女999久久久精品视频| 欧美看片网站| 国产视频精品va久久久久久| 亚洲国产精品一区制服丝袜| 99国产麻豆精品| 欧美一区二区三区四区夜夜大片| 欧美成人黄色小视频| 欧美一区二区三区在线播放| 欧美国产日韩二区| 国产欧美一区二区三区在线老狼| 最新国产の精品合集bt伙计| 久久久久成人网| 亚洲欧美成人一区二区三区|