큐는 요소 모음을 포함하는 추상 데이터 구조입니다. 큐는 FIFO 메커니즘을 구현합니다. 즉, 먼저 삽입된 요소도 먼저 삭제됩니다. 즉, 가장 최근에 추가된 요소가 대기열에서 먼저 제거됩니다.
배열을 사용하여 큐를 구현하는 프로그램은 다음과 같습니다 -
예시
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
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() 함수는 요소를 큐에 삽입합니다. 후면이 n-1과 같으면 대기열이 가득 차서 오버플로가 표시됩니다. front가 -1이면 1씩 증가하고, thenrear는 1만큼 증가하고 요소는 후방의 인덱스에 삽입됩니다. 이것은 아래에 표시됩니다 -
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
} Delete() 함수에서 큐에 요소가 없으면 언더플로 상태입니다. 그렇지 않으면 앞쪽에 있는 요소가 표시되고 앞쪽이 1씩 증가합니다. 이것은 아래에 나와 있습니다 -
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
}
else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
} display() 함수에서 front가 -1이면 큐는 비어 있습니다. 그렇지 않으면 모든 대기열 요소가 for 루프를 사용하여 표시됩니다. 이것은 아래에 표시됩니다 -
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
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;
}