104 lines
2.2 KiB
C++
104 lines
2.2 KiB
C++
#include <iostream>
|
|
#include <cstdlib>
|
|
|
|
const int MAX_QUEUE_SIZE = 100;
|
|
|
|
class JobQueue {
|
|
private:
|
|
int queue[MAX_QUEUE_SIZE];
|
|
int front, rear;
|
|
|
|
public:
|
|
JobQueue() {
|
|
front = -1;
|
|
rear = -1;
|
|
}
|
|
|
|
bool isFull() {
|
|
return rear == MAX_QUEUE_SIZE - 1;
|
|
}
|
|
|
|
bool isEmpty() {
|
|
return front == -1;
|
|
}
|
|
|
|
void enqueue(int job) {
|
|
if (isFull()) {
|
|
std::cout << "Queue is full. Job cannot be added.\n";
|
|
} else {
|
|
if (isEmpty()) {
|
|
front = 0;
|
|
}
|
|
rear++;
|
|
queue[rear] = job;
|
|
std::cout << "Job " << job << " added to the queue.\n";
|
|
}
|
|
}
|
|
|
|
void dequeue() {
|
|
if (isEmpty()) {
|
|
std::cout << "Queue is empty. No job to delete.\n";
|
|
} else {
|
|
int job = queue[front];
|
|
if (front == rear) {
|
|
front = rear = -1;
|
|
} else {
|
|
front++;
|
|
}
|
|
std::cout << "Job " << job << " deleted from the queue.\n";
|
|
}
|
|
}
|
|
|
|
void display() {
|
|
if (isEmpty()) {
|
|
std::cout << "Queue is empty.\n";
|
|
} else {
|
|
std::cout << "Job Queue: ";
|
|
for (int i = front; i <= rear; i++) {
|
|
std::cout << queue[i] << " ";
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
JobQueue jobQueue;
|
|
int choice, job;
|
|
|
|
while (1) {
|
|
std::cout << "Job Queue Simulation Menu:\n";
|
|
std::cout << "1. Add a job to the queue\n";
|
|
std::cout << "2. Delete a job from the queue\n";
|
|
std::cout << "3. Display the queue\n";
|
|
std::cout << "4. Quit\n";
|
|
std::cout << "Enter your choice: ";
|
|
std::cin >> choice;
|
|
|
|
switch (choice) {
|
|
case 1:
|
|
std::cout << "Enter the job number: ";
|
|
std::cin >> job;
|
|
jobQueue.enqueue(job);
|
|
break;
|
|
|
|
case 2:
|
|
jobQueue.dequeue();
|
|
break;
|
|
|
|
case 3:
|
|
jobQueue.display();
|
|
break;
|
|
|
|
case 4:
|
|
exit(0);
|
|
|
|
default:
|
|
std::cout << "Invalid choice. Please try again.\n";
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|