• <output id="aynwq"><form id="aynwq"><code id="aynwq"></code></form></output>

    <mark id="aynwq"><option id="aynwq"></option></mark>
  • <mark id="aynwq"><option id="aynwq"></option></mark><label id="aynwq"><dl id="aynwq"></dl></label>
  • 學習啦>學習英語>專業英語>計算機英語>

    c中queue的用法

    時間: 長思709 分享

      下面小編就跟你們詳細介紹下c中queue的用法的用法,希望對你們有用。

      c中queue的用法的用法如下:

      Model

      ------------------------------------------------------------------------------------------------------------------------

      隊列也是限制插入和刪除位置的表.

      主要操作是enqueue和dequeue操作.

      enqueue:入隊操作.在表的隊尾(rear)插入一個元素.

      dequeue:出隊操作.刪除表的隊首(front)元素.

      本文使用循環數組實現GenericQueue.需要指定capacity.缺點是超出容量,無法動態增長.當然,可以仿照list的方式克服這個問題.

      完整代碼詳見我的github(https://github.com/gnudennis/ds_c)(genric-queue.h generic-queue.c generic-queue-test.c)

      核心代碼

      ------------------------------------------------------------------------------------------------------------------------

      0. Generic Queue定義

      [cpp] view plain copy

      01.typedef void *ElementAddr;

      02.typedef void (*PfCbFree)(ElementAddr);

      03.

      04.typedef struct QueueRecord

      05.{

      06. ElementAddr *array;

      07. int capacity;

      08. int elemsize;

      09. int front;

      10. int rear;

      11. int size;

      12. PfCbFree freefn;

      13.} *Queue;

      1. API

      [cpp] view plain copy

      01./* Create a new queue */

      02.Queue queue_create(int elemsize, int capacity, PfCbFree freefn);

      03.

      04./* Dispose the queue */

      05.void queue_dispose(Queue que);

      06.

      07./* Make the give queue empty */

      08.void queue_make_empty(Queue que);

      09.

      10./* Return true if the queue is empty */

      11.int queue_is_empty(Queue que);

      12.

      13./* Return true if the queue is full */

      14.int queue_is_full(Queue que);

      15.

      16./* Insert a new element onto queue */

      17.void queue_enqueue(Queue que, ElementAddr elemaddr);

      18.

      19./* Delete the front element off the queue */

      20.void queue_dequeue(Queue que);

      21.

      22./* Fetch the front element from the queue */

      23.void queue_front(Queue que, ElementAddr elemaddr);

      24.

      25./* Fetch and Delete the front element from the queue */

      26.void queue_front_and_dequeue(Queue que, ElementAddr elemaddr);

      2.Implementation

      [cpp] view plain copy

      01./* Create a new queue with capacity */

      02.Queue

      03.queue_create(int elemsize, int capacity, PfCbFree freefn)

      04.{

      05. Queue que;

      06.

      07. que = malloc(sizeof(struct QueueRecord));

      08. if ( que == NULL ) {

      09. fprintf(stderr, "Out of memory\n");

      10. exit(1);

      11. }

      12.

      13. que->elemsize = elemsize;

      14. que->capacity = capacity > MIN_QUEUE_SIZE ? capacity : MIN_QUEUE_SIZE;

      15.

      16. que->array = malloc(elemsize * que->capacity);

      17. if ( que->array == NULL ) {

      18. fprintf(stderr, "Out of memory\n");

      19. exit(1);

      20. }

      21. que->front = 1;

      22. que->rear = 0;

      23. que->size = 0;

      24. que->freefn = freefn;

      25.

      26. return que;

      27.}

      28.

      29./* Dispose the queue */

      30.void

      31.queue_dispose(Queue que)

      32.{

      33. if (que != NULL) {

      34. queue_make_empty(que);

      35. free(que->array);

      36. free(que);

      37. }

      38.}

      39.

      40./* Make the give queue empty */

      41.void

      42.queue_make_empty(Queue que)

      43.{

      44. if ( que->freefn ) {

      45. int i;

      46. for ( i = 0; i < que->size; ++i) {

      47. free((char *)que->array +

      48. que->elemsize * i);

      49. }

      50. }

      51. que->size = 0;

      52. que->front = 1;

      53. que->rear = 0;

      54.}

      55.

      56./* Return true if the queue is empty */

      57.int

      58.queue_is_empty(Queue que)

      59.{

      60. return que->size == 0;

      61.}

      62.

      63./* Return true if the queue is full */

      64.int

      65.queue_is_full(Queue que)

      66.{

      67. return que->size == que->capacity;

      68.}

      69.

      70.static int

      71.successor(Queue que, int index)

      72.{

      73. if ( ++index == que->capacity)

      74. index = 0;

      75. return index;

      76.}

      77.

      78./* Insert a new element onto queue(rear) */

      79.void

      80.queue_enqueue(Queue que, ElementAddr elemaddr)

      81.{

      82. void *target;

      83.

      84. if ( queue_is_full(que) ) {

      85. fprintf(stderr, "Full queue\n");

      86. exit(1);

      87. }

      88. que->rear = successor(que, que->rear);

      89. target = (char *)que->array + que->elemsize * que->rear;

      90. memcpy(target, elemaddr, que->elemsize);

      91. que->size++;

      92.}

      93.

      94./* Delete the front element off the queue */

      95.void

      96.queue_dequeue(Queue que)

      97.{

      98. if ( queue_is_empty(que) ) {

      99. fprintf(stderr, "Empty queue\n");

      100. exit(1);

      101. }

      102. if ( que->freefn ) {

      103. void *target = (char *)que->array +

      104. que->front * que->elemsize;

      105. que->freefn(target);

      106. }

      107. que->size--;

      108. que->front = successor(que, que->front);

      109.}

      110.

      111./* Fetch the front element from the queue */

      112.void

      113.queue_front(Queue que, ElementAddr elemaddr)

      114.{

      115. void *target = (char *)que->array +

      116. que->front * que->elemsize;

      117. memcpy(elemaddr, target, que->elemsize);

      118.}

      119.

      120./* Fetch and Delete the front element from the queue */

      121.void

      122.queue_front_and_dequeue(Queue que, ElementAddr elemaddr)

      123.{

      124. void *target;

      125.

      126. if ( queue_is_empty(que) ) {

      127. fprintf(stderr, "Empty queue\n");

      128. exit(1);

      129. }

      130.

      131. target = (char *)que->array +

      132. que->front * que->elemsize;

      133. memcpy(elemaddr, target, que->elemsize);

      134.

      135. que->size--;

      136. que->front = successor(que, que->front);

      137.}

      分析

      ----------------

      本文使用循環數組實現GenericQueue.需要指定capacity.既然是循環數組,就是圍成一個圈.也就插入第一個元素沒有必要非要放在0處啦.

      初始狀態:

      {

      que->size = 0;

      que->front = 1;

      que->rear = 0;

      }

      說明這樣第一次enqueue操作放在array[1]處,當然:這不是必須的,取決于你想放在那里.

      #define mxx

      {

      que->size = 0;

      que->front =m+1;

      que->rear = m;

      }

      就放在array[m+1]處.

    c中queue的用法

    下面小編就跟你們詳細介紹下c中queue的用法的用法,希望對你們有用。 c中queue的用法的用法如下: Model ------------------------------------------------------------------------------------------------------------------------
    推薦度:
    點擊下載文檔文檔為doc格式

    上一篇:c中out的用法

    下一篇:c中random的用法

    精選文章

    • c中out的用法
      c中out的用法

      下面小編就跟你們詳細介紹下c中out的用法的用法,希望對你們有用。 c中out的用法的用法如下: 在C#這門高級語言中,你是否注意過ref與out的用法?你是否

    • c中new的用法
      c中new的用法

      下面小編就跟你們詳細介紹下c中new的用法的用法,希望對你們有用。 c中new的用法的用法如下: 一. 簡介 new有三種使用方式:plain new,nothrow new和placement

    • c中map的用法
      c中map的用法

      下面小編就跟你們詳細介紹下c中map的用法的用法,希望對你們有用。 c中map的用法的用法如下: Map是c++的一個標準容器,她提供了很好一對一的關系,在一

    • c中malloc的用法
      c中malloc的用法

      下面小編就跟你們詳細介紹下c中malloc的用法的用法,希望對你們有用。 c中malloc的用法的用法如下: 函數聲明(函數原型): void *malloc(int size); 說明:mallo

    537236 主站蜘蛛池模板: 久久精品国产亚洲AV香蕉| 免费又黄又爽1000禁片| 中文字幕yellow在线资源| 精品国精品自拍自在线| 奇米综合四色77777久久| 亚洲精品无码久久久久久| 2019中文字幕在线| 本道久久综合无码中文字幕| 国产在线资源站| 中文字幕乱码人妻无码久久| 91video国产一区| 村上凉子丰满禁断五十路| 国产亚洲Av综合人人澡精品| 一级白嫩美女毛片免费| 泰国午夜理伦三级| 国产特级毛片aaaaaa| 久久久久性色av毛片特级| 精品偷自拍另类在线观看| 国产综合无码一区二区色蜜蜜| 亚欧洲精品在线视频免费观看| 色婷婷激情综合| 天天想你电视剧| 亚洲一区中文字幕在线电影网 | 男人桶女人叽叽| 国产精品乱子乱xxxx| 久久久久99精品成人片欧美| 看亚洲a级一级毛片| 国产精品jizz在线观看直播| 中文字幕无线码免费人妻| 毛片网在线观看| 国产在线91区精品| youjizz亚洲| 欧美啪啪动态图| 国产91精品高清一区二区三区| gav男人天堂| 最近中文字幕免费mv视频| 啊啊啊好深视频| 1024毛片基地| 成人爽爽激情在线观看| 亚洲成在人线在线播放无码| 青青国产线免观看手机版精品|