대기열
삽입이 한쪽 끝(뒤)에서 수행되고 삭제가 다른 끝(앞)에서 수행되는 FIFO로 구현되는 대기열입니다. 가장 먼저 입력된 요소가 먼저 삭제됩니다.
대기열 작업은 -
EnQueue(int 데이터) - 후단에 삽입
int DeQueue()- 프런트 엔드에서 삭제
배열을 이용하여 큐를 구현하는 C++ 프로그램입니다.
알고리즘
Begin function Enqueue() to insert elements in queue: If queue is completely filled up then print “Overflow”. Otherwise insert element at rear. Update the value of rear End Begin function Dequeue() to delete elements from queue: If queue is completely empty then print “Underflow”. Otherwise insert element from the front. Update the value of rear. End
예시 코드
#include <bits/stdc++.h>
using namespace std;
struct Q {
int f, r, capacity;
int* q;
Q(int c) {
f = r= 0;
capacity = c;
q = new int;
}
~Q() { delete[] q; }
void Enqueue(int d) {
if (capacity == r) { //check if queue is empty or not
printf("\nQueue is full\n");
return;
} else {
q[r] = d; //insert data
r++; //update rear
}
return;
}
void Dequeue() {
if (f == r) {
printf("\nQueue is empty\n");
return;
} else {
for (int i = 0; i < r - 1; i++) {
q[i] = q[i + 1];
}
r--; //update rear
}
return;
}
void Display() //display the queue {
int i;
if (f == r) {
printf("\nQueue is Empty\n");
return;
}
for (i = f; i < r; i++) {
printf(" %d <-- ", q[i]);
}
return;
}
void Front() {
if (f == r) {
printf("\nQueue is Empty\n");
return;
}
printf("\nFront Element is: %d", q[f]); //print front element of queue
return;
}
};
int main(void) {
Q qu(3);
qu.Display();
cout<<"after inserting elements"<<endl;
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Display();
qu.Dequeue();
qu.Dequeue();
printf("\n\nafter two node deletion\n\n");
qu.Display();
qu.Front();
return 0;
} 출력
Queue is Empty 10 <-- 20 <-- 30 <-- after two node deletion 30 <-- Front Element is: 30