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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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>
            在线观看欧美| 最新日韩在线| 欧美一区二区三区四区视频| 在线午夜精品| 国产欧美激情| 玖玖国产精品视频| 欧美成年人视频| 91久久中文字幕| 一本色道**综合亚洲精品蜜桃冫| 欧美伦理在线观看| 亚洲欧美资源在线| 久久精品国产欧美激情| 亚洲国产日韩欧美在线图片| 最新成人av在线| 欧美午夜寂寞影院| 久久精品二区三区| 男女av一区三区二区色多| 亚洲一二三四区| 久久精品亚洲一区| 亚洲午夜精品久久| 久久国产精品久久久| 亚洲美女91| 欧美一级理论片| 亚洲精品欧美极品| 欧美亚洲一级| 亚洲视频欧美在线| 久久久久久国产精品mv| av成人免费在线观看| 欧美一区在线直播| 一区二区三区波多野结衣在线观看| 午夜久久电影网| 9色精品在线| 久久久精品国产一区二区三区 | 欧美喷潮久久久xxxxx| 亚洲欧美另类国产| 欧美成年人在线观看| 久久国产日韩欧美| 欧美日韩成人网| 欧美国产日韩在线| 国产伦精品一区二区三区在线观看 | 一级成人国产| 久久字幕精品一区| 欧美影院在线播放| 欧美日韩一区在线观看视频| 欧美黄色aa电影| 国产综合精品一区| 亚洲午夜精品一区二区| 99riav久久精品riav| 老**午夜毛片一区二区三区| 欧美一区二区三区日韩| 欧美私人啪啪vps| 亚洲精华国产欧美| 亚洲国产另类 国产精品国产免费| 亚洲视频免费观看| 亚洲色图在线视频| 欧美激情精品久久久久久蜜臀| 久久综合九色九九| 国内精品久久久久久久97牛牛| 亚洲伊人观看| 羞羞答答国产精品www一本| 欧美日韩视频在线第一区| 亚洲高清一区二区三区| 亚洲国产精品一区在线观看不卡| 久久福利影视| 久久综合色8888| 亚洲成人在线观看视频| 久久噜噜噜精品国产亚洲综合| 久久久久久久一区二区三区| 国产精品亚洲激情 | 美女主播精品视频一二三四| 国产视频观看一区| 午夜精品偷拍| 久久久久一区二区三区| 国精产品99永久一区一区| 性欧美xxxx视频在线观看| 欧美在线视频播放| 国产午夜精品美女毛片视频| 欧美在线观看网址综合| 久久婷婷久久一区二区三区| 激情久久婷婷| 蜜桃久久av| 亚洲精品一区二区三区99| 亚洲一级高清| 国产女主播一区| 久久久久久网| 亚洲欧洲视频| 欧美一区不卡| 亚洲国产日韩欧美在线图片| 欧美日韩精品免费观看视频完整 | 99综合在线| 香蕉av777xxx色综合一区| 国产欧美精品久久| 久久在线免费观看| 亚洲另类视频| 久久爱另类一区二区小说| 精品不卡视频| 欧美日韩蜜桃| 欧美一区在线直播| 亚洲人成在线观看一区二区| 午夜精品国产| 亚洲国产精品久久久久秋霞影院| 欧美日韩国产不卡| 久久精品日产第一区二区三区 | 久久天天躁狠狠躁夜夜av| 亚洲国产精品视频| 国产精品日韩| 欧美激情一区二区三区| 亚洲欧美在线x视频| 欧美激情麻豆| 午夜精品久久久久影视| 亚洲国产高清自拍| 国产欧美视频一区二区三区| 欧美激情久久久久| 久久精品一区二区三区不卡牛牛| 亚洲美洲欧洲综合国产一区| 久久一区亚洲| 欧美在线视频一区| 亚洲天堂网站在线观看视频| 国产在线日韩| 国产伦一区二区三区色一情| 欧美精品成人91久久久久久久| 欧美综合77777色婷婷| 一本大道av伊人久久综合| 欧美大片18| 免费看亚洲片| 久久久免费av| 久久激情五月丁香伊人| 亚洲在线视频免费观看| 日韩视频在线一区二区三区| 亚洲国产成人av在线 | 欧美激情视频在线播放| 久久精品夜色噜噜亚洲aⅴ| 亚洲欧美精品在线| 亚洲小说区图片区| 亚洲午夜免费视频| 日韩一区二区免费看| 亚洲国产专区校园欧美| 亚洲国产精品v| 久久综合精品国产一区二区三区| 久久精品国内一区二区三区| 香蕉精品999视频一区二区 | 亚洲国产精品久久久久秋霞不卡| 激情小说另类小说亚洲欧美| 韩国精品在线观看| 激情五月综合色婷婷一区二区| 国产一区二区中文| 国产一区二三区| 狠狠色丁香婷婷综合| 尤物99国产成人精品视频| 精品91视频| 91久久精品一区二区三区| 最新中文字幕一区二区三区| 91久久夜色精品国产九色| 亚洲日本理论电影| 一本大道久久a久久精品综合| avtt综合网| 亚洲欧美美女| 久久一区二区三区超碰国产精品| 久久夜色精品国产欧美乱| 免费试看一区| 91久久在线播放| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲一区二区三区午夜| 久久国产精品久久久久久久久久 | 欧美在线3区| 久久精品男女| 欧美国产丝袜视频| 欧美三级网址| 国产综合色产| 亚洲精品久久| 欧美一区二区女人| 久久字幕精品一区| 亚洲国产导航| 亚洲欧美日韩一区在线| 久久久久久国产精品mv| 欧美精选一区| 国产日韩欧美| 亚洲精品中文字幕女同| 午夜在线视频观看日韩17c| 美女黄毛**国产精品啪啪| 日韩午夜电影av| 久久国产婷婷国产香蕉| 欧美日韩精品一区二区天天拍小说| 国产精品久久久久高潮| 亚洲黄色片网站| 欧美制服丝袜第一页| 亚洲第一网站| 欧美一区二区三区四区在线 | 欧美亚洲免费电影| 欧美精品一区二区三区很污很色的| 国产精品一区二区久久| 亚洲精品一区二区三区在线观看 | 亚洲国产日韩在线一区模特| 午夜精品久久久久久久99樱桃 | 久久精品国产99国产精品澳门| 欧美日韩免费高清| 最新日韩欧美| 久久久久国色av免费观看性色| 日韩亚洲视频在线| 老色批av在线精品|