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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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>
            亚洲免费一级电影| 亚洲精品乱码久久久久| 久久福利影视| 国产一区亚洲一区| 久久久综合免费视频| 欧美成人一区二区| 夜夜爽夜夜爽精品视频| 国产精品久久久久久久久久尿 | 久久精品九九| 亚洲精品日本| 国产日韩欧美精品| 欧美在线一二三四区| 欧美视频二区| 亚洲欧美另类在线| 欧美成人精品一区| 午夜精品福利一区二区三区av| 黄色一区二区三区四区| 欧美另类一区二区三区| 欧美一级电影久久| 亚洲精品美女在线| 欧美日韩国内| 性做久久久久久久免费看| 亚洲日本中文字幕区| 亚洲美女啪啪| 欧美成人午夜剧场免费观看| 亚洲国产视频一区| 裸体丰满少妇做受久久99精品| 一区二区免费看| 亚洲国产一区在线| 狠狠色2019综合网| 国产伊人精品| 亚洲视频1区| 一区二区三区免费看| 久久黄金**| 久久激情视频久久| 亚洲精品美女91| 久久精品国产在热久久| 国产精品激情电影| 国产精品素人视频| 国产精品成人一区二区三区吃奶| 激情欧美一区二区三区| 亚洲欧美一区二区三区极速播放| 亚洲电影天堂av| 亚洲日本成人| 久久综合九九| 久久青草欧美一区二区三区| 久久久亚洲国产天美传媒修理工| 欧美午夜视频在线| 国产精品一区二区你懂的| 国产精品成人一区二区三区吃奶 | 久久久精品国产免费观看同学| 欧美日韩久久| 老司机67194精品线观看| 国产伦精品一区二区三区高清| 9国产精品视频| 欧美激情第1页| 亚洲人成高清| 嫩草成人www欧美| 欧美视频二区36p| 国产精品99久久久久久有的能看| 亚洲国产一区二区a毛片| 亚洲国产精品一区| 亚洲精品免费看| 欧美va天堂| 男女av一区三区二区色多| 亚洲经典在线看| 亚洲国产你懂的| 欧美激情四色| 国产精品一区免费在线观看| 亚洲专区在线| 麻豆精品在线视频| 久久精品国产一区二区三区免费看| 国产综合久久久久影院| 久久午夜电影| 夜夜躁日日躁狠狠久久88av| 欧美亚洲一级片| 国内精品久久久久久久影视蜜臀 | 欧美色精品天天在线观看视频| 一区二区免费在线视频| 亚洲一区二区三区成人在线视频精品| 久久精品卡一| 亚洲电影自拍| 一区二区三区导航| 久久亚洲免费| 一区二区三区|亚洲午夜| 中文在线资源观看网站视频免费不卡| 久久久夜夜夜| 99在线精品视频| 在线一区二区三区四区五区| 国产视频一区三区| 亚洲大片一区二区三区| 欧美三级在线视频| 久久精品天堂| 欧美日韩第一区日日骚| 久久五月天婷婷| 国产精品电影网站| 欧美成人中文字幕| 国产精品色在线| 欧美激情免费观看| 国产麻豆精品在线观看| 亚洲黄色成人网| 国产日韩欧美中文在线播放| 亚洲激情电影在线| 久久综合电影一区| 亚洲欧美国产77777| 蜜臀久久99精品久久久画质超高清| 国产一区视频在线观看免费| 亚洲黄色一区| 一色屋精品亚洲香蕉网站| 久久久久网址| 欧美在线亚洲一区| 亚洲尤物精选| 欧美日韩国产天堂| 免费看av成人| 欧美成人免费全部| 亚洲精品中文在线| 亚洲美女毛片| 欧美乱妇高清无乱码| 免费成人性网站| 亚洲欧美综合国产精品一区| 欧美国产免费| 亚洲图片欧洲图片av| 免费不卡视频| 欧美成黄导航| 激情久久影院| 久久精品视频免费| 久久夜精品va视频免费观看| 国产亚洲精品一区二区| 免费观看成人www动漫视频| 国产精品日韩欧美大师| 一区二区日本视频| 亚洲在线观看| 国产精品久久久久久户外露出 | 亚洲精品国产无天堂网2021| 久久精品国产一区二区三| 久久丁香综合五月国产三级网站| 欧美日韩在线一二三| 亚洲精品久久久久中文字幕欢迎你 | 欧美一区二区三区免费观看视频| 亚洲网友自拍| 国产在线观看一区| 亚洲欧美中文在线视频| 尤物yw午夜国产精品视频| 久久精品亚洲一区二区三区浴池| 久久久久久久999| 欧美区一区二| 日韩视频不卡中文| 午夜视频在线观看一区| 国产一区二区三区四区hd| 久久人人97超碰精品888| 欧美激情久久久| 亚洲网友自拍| 国产日韩一区二区| 老牛影视一区二区三区| 亚洲国产成人av好男人在线观看| 亚洲美女91| 欧美午夜片在线观看| 亚洲午夜一区二区三区| 久久成人免费网| 亚洲二区视频在线| 欧美午夜视频| 久久久亚洲国产天美传媒修理工 | 亚洲一区二区毛片| 免播放器亚洲一区| 夜夜嗨av色综合久久久综合网| 香蕉免费一区二区三区在线观看| 欧美 日韩 国产一区二区在线视频 | 韩国女主播一区| 欧美91福利在线观看| 9色精品在线| 麻豆av一区二区三区久久| 欧美性生交xxxxx久久久| 亚洲欧美日产图| 亚洲激情社区| 久久深夜福利| 亚洲性视频网站| 国产一区清纯| 欧美午夜视频在线| 久久综合久久综合久久综合| 亚洲天堂av高清| 亚洲第一福利在线观看| 久久se精品一区精品二区| 99这里只有久久精品视频| 狠狠入ady亚洲精品经典电影| 欧美日韩国产精品一区| 久久婷婷国产综合国色天香| 亚洲欧美春色| 一区二区三区成人| 亚洲乱码一区二区| 亚洲国产激情| 美女视频一区免费观看| 久久精品一本| 久久疯狂做爰流白浆xx| 亚洲欧美日韩视频二区| 在线亚洲一区二区| 一区二区三区四区五区视频| 日韩视频在线永久播放| 欧美日韩精品伦理作品在线免费观看| 欧美一级黄色网| 亚洲视频你懂的|