日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

设计循环队列

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计循环队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、定義數據結構??tail head 頭尾指針,隊列buf queue ,隊列長度 size? ?,已存數據長度 len?

typedef?struct?myQueue{

????int*?queue;

????int?head;

????int?tail;

????int?size;

????int?len;

}?MyCircularQueue;

2.初始化 數據結構

/**?Initialize?your?data?structure?here.?Set?the?size?of?the?queue?to?be?k.?*/

?

MyCircularQueue*?myCircularQueueCreate(int?k)?{

????MyCircularQueue*?obj=malloc(sizeof(MyCircularQueue));

????obj->queue=malloc(k*sizeof(int));

????obj->head=0;

????obj->tail=0;

????obj->size=k;

????obj->len?=?0;

????return?obj;

}

3.如果已存在數據長度為0 則為空

/**?Checks?whether?the?circular?queue?is?empty?or?not.?*/

bool?myCircularQueueIsEmpty(MyCircularQueue*?obj)?{

????return?(obj->len==0);//&&((obj->queue)[obj->head]==-1);

}

4、如果已存在數據長度等于queue的長度則隊列為滿

/**?Checks?whether?the?circular?queue?is?full?or?not.?*/

bool?myCircularQueueIsFull(MyCircularQueue*?obj)?{

????return?(obj->len==obj->size);

}

5、向尾指針插入數據,尾指針總是指向當前有效數據的下個地址,已存在數據長度加1

/**?Insert?an?element?into?the?circular?queue.?Return?true?if?the?operation?is?successful.?*/

bool?myCircularQueueEnQueue(MyCircularQueue*?obj,?int?value)?{

????if(myCircularQueueIsFull(obj))?return?false;

????(obj->queue)[obj->tail]=value;

????obj->tail?=?(obj->tail?+?1?)%?obj->size;??

????obj->len?++;

????return?true;

}

6、從頭指針開始刪除隊列中元素,已存在數據長度減1,頭指針加一

/**?Delete?an?element?from?the?circular?queue.?Return?true?if?the?operation?is?successful.?*/

bool?myCircularQueueDeQueue(MyCircularQueue*?obj)?{

????if(myCircularQueueIsEmpty(obj))?return?false;

???????obj->head?=?(obj->head?+?1)?%?obj->size;

???????obj->len?--;

????return?true;

}

7、獲取有效數據第一個元素

/**?Get?the?front?item?from?the?queue.?*/

int?myCircularQueueFront(MyCircularQueue*?obj)?{

?????if(myCircularQueueIsEmpty(obj))?return?-1;

????return?(obj->queue)[obj->head];

}

8、獲取有效數據最后一個元素

/**?Get?the?last?item?from?the?queue.?*/

int?myCircularQueueRear(MyCircularQueue*?obj)?{

????if(myCircularQueueIsEmpty(obj))?return?-1;?

????if(obj->tail?==?0)

????return?(obj->queue)[obj->size?-?1];

????else

?????return?(obj->queue)[obj->tail?-?1];

}

9、釋放queue

void?myCircularQueueFree(MyCircularQueue*?obj)?{

????if(obj&&obj->queue)

????{

???????free(obj->queue);

???????free(obj);

????}??

}

總結

以上是生活随笔為你收集整理的设计循环队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。