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

牽著老婆滿街逛

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

Circular Buffer

轉(zhuǎn)載自: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)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   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>
            99re热这里只有精品视频| 欧美激情一区二区三区在线视频| 欧美一区2区三区4区公司二百| 久久看片网站| 欧美激情一区二区| 免费视频最近日韩| 久久久成人精品| 麻豆国产va免费精品高清在线| 久久精品亚洲一区二区| 久久亚洲风情| 亚洲国产精品嫩草影院| 亚洲激情综合| 亚洲一级在线观看| 久久久噜噜噜久久久| 欧美久久电影| 国产日韩精品一区二区浪潮av| 伊大人香蕉综合8在线视| 亚洲另类自拍| 欧美在线综合| 亚洲激情啪啪| 欧美一级在线播放| 欧美高清视频免费观看| 国产欧美日韩视频一区二区三区 | 欧美a级一区二区| 久久蜜桃精品| 亚洲毛片在线观看.| 午夜精品久久久久久久99黑人| 久久久久久久久久久久久9999| 欧美日韩激情小视频| 国产噜噜噜噜噜久久久久久久久| 1000部国产精品成人观看| 一区二区高清视频在线观看| 欧美亚洲免费高清在线观看| 亚洲国产精品一区二区尤物区| 亚洲在线观看免费| 欧美激情一区二区三区不卡| 国产精品视频网| 日韩一区二区免费高清| 久久久久se| 亚洲色图综合久久| 欧美激情精品久久久久久蜜臀| 国产一区高清视频| 亚洲欧美在线另类| 亚洲电影免费| 久久精品国产免费看久久精品| 国产精品分类| 亚洲一区二区三区高清| 亚洲高清视频一区| 久久亚洲不卡| 一色屋精品视频在线看| 久久精品国产久精国产爱| 亚洲私人影院| 国产精品人人做人人爽| 亚洲一区二区在线播放| 日韩亚洲欧美高清| 欧美激情第五页| 亚洲欧洲精品一区二区三区不卡 | 久久精品中文字幕免费mv| 午夜精品福利视频| 欧美性猛交99久久久久99按摩| 尤物精品在线| 久久全国免费视频| 欧美专区中文字幕| 国内一区二区三区在线视频| 久久九九精品99国产精品| 亚洲欧美日韩国产成人| 国产精品欧美激情| 久久精品电影| 久久天天躁狠狠躁夜夜av| 亚洲国产精品成人综合| 亚洲电影第三页| 欧美精品一区二区在线观看| a91a精品视频在线观看| 在线亚洲免费| 国产综合自拍| 欧美激情第4页| 欧美日韩国产一级片| 亚洲男人的天堂在线aⅴ视频| 亚洲影院在线| 亚洲第一主播视频| 亚洲日本在线观看| 国产精品久久久久久久久免费桃花| 午夜精品免费视频| 久久久一本精品99久久精品66| 亚洲国产成人在线播放| 日韩视频免费在线| 国产日韩欧美电影在线观看| 美日韩精品免费| 欧美人与性动交cc0o| 欧美在线欧美在线| 老司机午夜免费精品视频| 一区二区三区免费观看| 亚洲一区二区三区精品在线| 一区在线免费| 国产精品99久久久久久人| 韩国av一区二区三区四区| 亚洲精品国产系列| 国产自产v一区二区三区c| 亚洲人成网站在线播| 国产日韩精品在线观看| 亚洲国产精品久久久| 国产精品一区二区你懂得| 欧美国产三级| 国产亚洲精品久久飘花| 亚洲激情一区二区| 国产综合在线看| 中文在线资源观看网站视频免费不卡 | 一区二区三区导航| 国产曰批免费观看久久久| 亚洲国产毛片完整版| 国产精品综合| 欧美三区视频| 中国女人久久久| 精品69视频一区二区三区| 国产精品99久久不卡二区| 亚洲人成高清| 久久久国产精品亚洲一区 | 翔田千里一区二区| 久久成人免费电影| 欧美日本在线| 亚洲福利在线观看| 国产综合欧美| 香港久久久电影| 亚洲欧美日韩国产成人| 欧美日本精品在线| 亚洲激情一区| 亚洲精品美女免费| 欧美成人免费大片| 欧美激情国产日韩| 亚洲国产婷婷香蕉久久久久久99| 久久国产黑丝| 久久久久免费视频| 国内精品久久久久久久97牛牛| 亚洲综合不卡| 久久成人国产精品| 好看的日韩av电影| 久久精品视频99| 老司机精品久久| 在线观看成人av电影| 久久黄色网页| 欧美ab在线视频| 亚洲另类自拍| 欧美日韩第一区| 在线亚洲自拍| 亚洲欧美综合精品久久成人| 国产精品日韩欧美| 亚洲欧美综合| 老牛嫩草一区二区三区日本| 在线观看福利一区| 欧美成人久久| 一区二区三区日韩精品| 欧美亚洲在线播放| 国语精品中文字幕| 欧美成人精品不卡视频在线观看| 亚洲精品免费在线| 午夜精品久久久久久99热| 国产欧美日韩一区| 麻豆国产精品va在线观看不卡| 亚洲狠狠婷婷| 欧美一级理论片| 亚洲大片精品永久免费| 欧美激情一区二区三区成人| 一本久久综合亚洲鲁鲁五月天| 欧美在线高清| 亚洲日本欧美日韩高观看| 国产精品99免视看9| 久久精品国产精品亚洲综合| 亚洲人人精品| 久久久综合免费视频| 亚洲美女av网站| 国产欧美视频一区二区| 免费在线观看日韩欧美| 亚洲一区激情| 亚洲激情一区| 久久精品国产精品| 中文成人激情娱乐网| 激情综合色综合久久综合| 欧美三级第一页| 亚洲美女在线看| 国产日韩欧美在线观看| 免费人成网站在线观看欧美高清| 一本色道久久综合亚洲精品婷婷 | 国产欧美一区二区三区另类精品 | 国产亚洲午夜高清国产拍精品| 欧美xxx在线观看| 欧美一级久久久久久久大片| 亚洲激情网址| 免费成人av在线看| 香蕉成人久久| 一本久道综合久久精品| 伊人久久大香线蕉综合热线 | 亚洲精品免费一区二区三区| 国产亚洲精品v| 国产精品第一页第二页第三页| 美女任你摸久久| 久久电影一区| 亚洲欧美区自拍先锋| 99伊人成综合| 亚洲人成在线观看| 欧美国产日本在线|