Problem Statement: Queues are frequently used in computer programming, and a typical example is the creation a job queue by an operating system. If the operating system does not use priorities, then jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add delete job from queue.
Note: Scroll Horizontally to see the full line of code.
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int x){
data = x;
next = NULL;
}
};
class queue{
public:
node* front;
node* rear;
queue(){
front = NULL;
rear = NULL;
}
void enqueue(){
int x;
cout<<"Enter JOB to be inserted in the Queue : A";
cin>>x;
node* n = new node(x);
if(front==NULL){
rear = n;
front = n;
return;
}
rear->next = n;
rear = n;
}
void dequeue(){
if(front==NULL){
cout<<"Queue is Empty!";
return;
}
node* todelete = front;
front = front->next;
delete todelete;
}
int frontElement(){
if(front==NULL){
cout<<"Queue is Empty!";
return -1;
}
return front->data;
}
int rearElement(){
if(front==NULL){
cout<<"Queue is Empty!";
return -1;
}
return rear->data;
}
int size(){
if(front==NULL){
cout<<"Queue is Empty!";
return 0;
}
int sum = 0;
node* temp = front;
while(temp!=NULL){
sum+=1;
temp = temp->next;
}
return sum;
}
void displayQueue(){
if(front==NULL){
cout<<"Queue is Empty!";
return;
}
node* temp = front;
while(temp!=NULL){
cout<<"A"<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
};
int main(){
queue q;
cout<<"Queue Representation:"<<endl;
char cont = 'y';
while(cont=='y'){
int option;
cout<<"<-----Menu----->"<<endl;
cout<<"1.Enter JOB in Queue"<<endl;
cout<<"2.Delete JOB from Queue"<<endl;
cout<<"3.Display First JOB of Queue"<<endl;
cout<<"4.Display Last JOB of Queue"<<endl;
cout<<"5.size of JOB Queue"<<endl;
cout<<"6.Display JOB Queue"<<endl;
cout<<"Enter a choice: ";
cin>>option;
switch(option){
case 1: q.enqueue(); break;
case 2: q.dequeue(); break;
case 3: cout<<"The First JOB of Queue is: A"<<q.frontElement()<<endl; break;
case 4: cout<<"The Last JOB of Queue is: A"<<q.rearElement()<<endl; break;
case 5: cout<<"The size of Queue is: "<<q.size()<<endl; break;
case 6: cout<<"The JOB queue is: ";q.displayQueue();cout<<endl; break;
default: cout<<"INVALID Choice!!!"<<endl;
}
cout<<"Do you want to continue?(y/n): ";
cin>>cont;
}
if(cont='n'){cout<<"Program Ended succesfully!"<<endl;}
return 0;
}
Comments
Post a Comment