คิวเป็นโครงสร้างข้อมูลนามธรรมที่มีคอลเลกชันขององค์ประกอบ คิวใช้กลไก FIFO กล่าวคือองค์ประกอบที่แทรกก่อนจะถูกลบก่อนเช่นกัน กล่าวอีกนัยหนึ่ง องค์ประกอบที่เพิ่มล่าสุดน้อยที่สุดจะถูกลบออกก่อนในคิว
โปรแกรมที่ใช้คิวโดยใช้รายการเชื่อมโยงจะได้รับดังนี้ -
ตัวอย่าง
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
} else {
temp=(struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void Delete() {
temp = front;
if (front == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL;
}
}
void Display() {
temp = front;
if ((front == NULL) && (rear == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
} ผลลัพธ์
ผลลัพธ์ของโปรแกรมข้างต้นมีดังนี้
1) Insert element to queue 2) Delete element from queue 3) Display all the elements of queue 4) Exit Enter your choice : 1 Insert the element in queue : 4 Enter your choice : 1 Insert the element in queue : 3 Enter your choice : 1 Insert the element in queue : 5 Enter your choice : 2 Element deleted from queue is : 4 Enter your choice : 3 Queue elements are : 3 5 Enter your choice : 7 Invalid choice Enter your choice : 4 Exit
ในโปรแกรมข้างต้น ฟังก์ชัน Insert() จะแทรกองค์ประกอบลงในคิว หากด้านหลังเป็น NULL แสดงว่าคิวว่างและมีการแทรกองค์ประกอบเดียว มิฉะนั้น โหนดจะถูกแทรกหลังด้านหลังด้วยองค์ประกอบที่จำเป็น จากนั้นโหนดนั้นจะถูกตั้งค่าเป็นด้านหลัง ด้านล่างนี้ −
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
} else {
temp=(struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
} ในฟังก์ชัน Delete() หากไม่มีองค์ประกอบในคิว แสดงว่าเป็นเงื่อนไขที่ไม่เพียงพอ หากมีเพียงหนึ่งองค์ประกอบในคิวที่ถูกลบและด้านหน้าและด้านหลังถูกตั้งค่าเป็น NULL มิฉะนั้น องค์ประกอบที่อยู่ด้านหน้าจะถูกลบออกและชี้ไปที่องค์ประกอบถัดไป ด้านล่างนี้ −
void Delete() {
temp = front;
if (front == NULL) {
cout<<"Underflow"<<endl;
return;
} else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL;
}
} ในฟังก์ชัน display() หากด้านหน้าและด้านหลังเป็น NULL แสดงว่าคิวว่างเปล่า มิฉะนั้น องค์ประกอบคิวทั้งหมดจะแสดงโดยใช้ลูป while โดยใช้ตัวแปร temp ด้านล่างนี้ −
void Display() {
temp = front;
if ((front == NULL) && (rear == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
} ฟังก์ชัน main() ให้ตัวเลือกแก่ผู้ใช้หากต้องการแทรก ลบ หรือแสดงคิว ตามการตอบสนองของผู้ใช้ ฟังก์ชันที่เหมาะสมเรียกว่าการใช้สวิตช์ หากผู้ใช้ป้อนคำตอบที่ไม่ถูกต้อง คำตอบนั้นจะถูกพิมพ์ออกมา ข้อมูลโค้ดสำหรับสิ่งนี้ได้รับด้านล่าง −
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}