// Ngăn_Đợ[Link] : This file contains the 'main' function.
Program execution begins
and ends there.
//
#include <iostream>
using namespace std;
int sz;
int front = -1; // vị trí ở đầu
int rear = -1; // vị trí sau
int Q[100]; //Q[sz]
void enqueue(int value); // Them phan tu vào cuoi
void dequeue();
bool empty(); //Kiem tra stack có rỗng không
bool full(); // Kiểm tra đã full chưa
void display();
int val_front();
int main()
{
int choice, val;
cout << "Nhap sz cua Stack : ";
cin >> sz;
while (1) {
cout << "-------------" << endl;
cout << "*** Menu *** \n [Link] Chuong trinh \n [Link] phan tu vao
stack \n [Link] phan tu o dau Stack \n [Link] thi gia tri dau tien trong Queue \n
[Link] thi stack hien tai \n [Link] tra queue full \n [Link] tra queue rong " <<
endl;
cout << "-------------" << endl;
cout << "Nhap lua chon cua ban : ";
cin >> choice;
switch (choice) {
case 0:
return 0;
case 1:
cout << "Nhap gia tri muon them vao Queue : ";
cin >> val;
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
cout << val_front();
break;
case 4:
display();
break;
case 5:
if (full()) {
cout << "Queue hien da full khong the them phan tu ";
}
else {
cout << "Queue hien chua full co the them phan tu ";
}break;
case 6 :
if (empty()) {
cout << "Queue hien rong ";
}
else {
cout << "Queue khong rong ";
}break;
}cout << endl;
}
void enqueue(int val) {
if (full()) {
cout << "Hang doi da day khong the them phan tu ";
}
else {
front = 0;
rear++;
Q[rear] = val;
}
}
bool full() {
if (rear == sz-1) {
return true;
}
else {
return false;
}
}
void dequeue() {
if (empty()) {
cout << "Ham da rong khong con phan tu de xoa";
}
else {
cout << "Da xoa phan tu ơ dau ";
for (int i = 0; i < rear;i++) {
Q[i] = Q[i + 1];
}rear--;
}
}
bool empty() {
if (front == rear && front == -1) {
return true;
}
else {
return false;
}
}
int val_front() {
if (empty()) {
cout << "Ham rong khong co phan tu o dau de hien thi ";
}
else {
int giatri = Q[0];
return giatri;
}
}
void display() {
cout << "Queue hien tai cua ban la : ";
for (int i = front; i <= rear; i++) {
cout << Q[i] << " ";
}cout << endl;
}