C C++ คิววงกลม : Circular Queue
Suttapak / September 08, 2022
4 min read • ––– views
คิววงกลม : Circular Queue
เป็นการออกแบบมาเพื่อใช้ในการแก้ปัญหาในกรณีที่ยังมีพื้นที่เหลือในการเก็บข้อมูล คือคิวที่เปรียบเสมือนกับ ลิเนียร์อาเรย์ แตกต่างเพียงแต่ว่า คิววงกลมสามารถกลับไปใช้คิวหน้าได้ในกรณีที่หมดคิวปกติแล้ว
ขอส่งงานที่สามนะครับ
การเพิ่มข้อมูลเข้าไปในคิว Enqueue
void enq(myQueue* q, int data){
if ((q->rear == MAXQUEUE -1 && q->front == 0 ) || q->rear +1 == q->front) {
std::cout << "\nQueue if full" << std::endl;
return;
}
if (q->rear== MAXQUEUE -1) {
q->rear = 0;
q->arr[q->rear] = data;
} else {
q->rear++;
q->arr[q->rear] = data;
if (q->front == -1) {
q->front = 0;
}
}
}
การนำข้อมูลออกจากคิว Dequeue
void deq(myQueue* q) {
if (q->front == -1 ) {
std::cout << "Q is empty" <<std::endl;
return ;
}
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
return;
}
if (q->front == MAXQUEUE -1) {
q->arr[q->front] =NULL;
q->front =0;
return;
}
q->arr[q->front] =NULL;
q->front ++;
return;
}
การแสดงผลข้อมูล display
void dp(myQueue* q) {
std::cout << "\nDisplay Q" << std::endl;
std::cout << "Rear -> " << q->rear <<" Front -> "<< q->front << std::endl;
if(q->front<= q->rear) {
for(int i = 0; i <= q->rear ; i++) {
std::cout << "["<< i+1 << "] "<< " -> " << q->arr[i] << ", ";
}
std::cout << "\n\n";
return;
}
for(int i = q->front; i != q->rear ; i =((i+1)%MAXQUEUE)) {
std::cout << "["<< i+1 << "] "<< " -> " << q->arr[i] << ", ";
}
std::cout << "["<< q->rear+1 << "] "<< " -> " << q->arr[q->rear] << ", ";
std::cout << "\n\n";
return;
}
code สำเร็จรูป
#include <iostream>
const int MAXQUEUE = 5;
struct myQueue {
int arr[5];
int rear = -1;
int front = -1;
};
void enq(myQueue* q, int data);
void deq(myQueue* q);
void dp(myQueue* q) ;
int main() {
myQueue myQ ;
int menu;
char ans = 'y';
while (ans == 'y' || ans == 'Y') {
std::cout << "Matee Suttapak \n " ;
std::cout << "Pls select menu [1]:EnQueue [2]:Dequeue [3]:Show data " ;
std::cin >> menu ;
switch (menu) {
case 1 :
int data;
std::cout << "\nInsert data : " ;
std::cin>>data;
enq(&myQ,data);
break;
case 2:
deq(&myQ);
break;
case 3:
dp(&myQ);
std::cout << "Do you want to continue press \'Y\' to continue press another to Exit programs : ";
std::cin >> ans ;
continue;
default:
std::cout << "Pls select 1 -> 2";
break;
}
std::cout << "\ndisplay Q" << std::endl;
if (myQ.rear == -1) {
std::cout << "\nQ is Empty." << std::endl;
}
dp(&myQ);
std::cout << "\n\n";
// std::cout << "Do you want to continue press \'Y\' to continue press another to Exit programs : ";
// std::cin >> ans ;
}
return 0;
}
void enq(myQueue* q, int data){
if ((q->rear == MAXQUEUE -1 && q->front == 0 ) || q->rear +1 == q->front) {
std::cout << "\nQueue if full" << std::endl;
return;
}
if (q->rear== MAXQUEUE -1) {
q->rear = 0;
q->arr[q->rear] = data;
} else {
q->rear++;
q->arr[q->rear] = data;
if (q->front == -1) {
q->front = 0;
}
}
}
void deq(myQueue* q) {
if (q->front == -1 ) {
std::cout << "Q is empty" <<std::endl;
return ;
}
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
return;
}
if (q->front == MAXQUEUE -1) {
q->arr[q->front] =NULL;
q->front =0;
return;
}
q->arr[q->front] =NULL;
q->front ++;
return;
}
void dp(myQueue* q) {
std::cout << "\nDisplay Q" << std::endl;
std::cout << "Rear -> " << q->rear <<" Front -> "<< q->front << std::endl;
if(q->front<= q->rear) {
for(int i = 0; i <= q->rear ; i++) {
std::cout << "["<< i+1 << "] "<< " -> " << q->arr[i] << ", ";
}
std::cout << "\n\n";
return;
}
for(int i = q->front; i != q->rear ; i =((i+1)%MAXQUEUE)) {
std::cout << "["<< i+1 << "] "<< " -> " << q->arr[i] << ", ";
}
std::cout << "["<< q->rear+1 << "] "<< " -> " << q->arr[q->rear] << ", ";
std::cout << "\n\n";
return;
}