Queue implementation using Array in C++ - c++

I have implemented an integer type Queue using an array in C++ as:
This is not a circular queue so we don't have to bother for the empty space after filling it once and then deleting some values.
class IntQueue
{
private:
int *queueArray;
int queueSize;
int rear, front;
public:
IntQueue(int); // Constructor
~IntQueue(void);
void Enqueue(int);
int Dequeue(void);
bool isFull(void);
bool isEmpty(void);
void Display(void);
};
// Constructor
IntQueue::IntQueue(int size)
{
queueArray = new int[size];
queueSize = size;
rear = -1;
front = -1;
}
// Destructor
IntQueue::~IntQueue(void)
{
delete [] queueArray;
}
// Enqueue function
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
rear++;
queueArray[rear] = num;
}
}
// Dequeue function
int IntQueue::Dequeue()
{
int num;
if (isEmpty())
{
cout << "The Queue is empty.\n";
}
else
{
front++;
num = queueArray[front];
}
return num;
}
//isFull function
bool IntQueue::isFull(void)
{
bool status;
if (rear == queueSize - 1)
status = true;
else
status = false;
return status;
}
//isEmpty function
bool IntQueue::isEmpty(void)
{
bool status;
if (front == rear)
status = true;
else
status = false;
return status;
}
// Display function
void IntQueue::Display()
{
if(isEmpty())
{
cout << "The Queue is empty.\n";
}
for(int i = front++; i <= rear; i++)
{
cout << "queueArray[" << i << "] = " << queueArray[i] << endl;
}
}
// Main function
int main()
{
int option, queueCapacity, value;
cout << endl << "Enter size of queue: ";
cin >> queueCapacity;
IntQueue *queue = new IntQueue(queueCapacity);
Menu:
cout << endl << "Queue implementation using Array" << endl;
cout << "--------------------------------" << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display Queue" << endl;
cout << "4. Display Menu" << endl;
cout << "5. Exit" << endl;
cout << "--------------------------------" << endl;
YourOption:
cout << endl << "Your option: ";
cin >> option;
switch(option)
{
case 1:
// Enqueue
cout << "Enter value to enqueue: ";
cin >> value;
queue->Enqueue(value);
goto YourOption;
case 2:
// Dequeue
int data;
data = queue->Dequeue();
cout << data << " is dequeued!";
goto YourOption;
case 3:
// Display
queue->Display();
goto YourOption;
case 4:
goto Menu;
case 5:
exit(0);
default:
cout << "Invalid option!" << endl;
break;
}
delete queue;
return 0;
}
The variable rear is initially -1. I have checked isFull() function as if rear is equal to the size of Queue, then the queue is full, not otherwise. However, to check queue emptiness, I compared if rear == front, then empty, not otherwise. But after executing it, by opting 2 from the menu, it displays some random value sometimes and the message i.e., "The Queue is empty".
What is the problem in this example?

void enqueue(int x)
{
//cout << "Enter the DAta " << endl;
//cin >> x;
if (front==(rear+1)%size)
{
cout << "Queue Is overflow::" << endl;
return;
}
else if (rear==-1&&front==-1)
{
//cout << "enter the To EnQueue :" << endl;
front = rear = 0;
queue[rear] = x;
}
else if (rear==0 && front==0)
{
rear = rear + 1 % size;
queue[rear] = x;
}
else
{
rear++;
queue[rear] = x;
}
}

Your queue is working properly but you have need to check some variable values like in the Enqueue(int num) function you have forgot to update the variable front=0
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
rear++;
queueArray[rear] = num;
}
}
because at the initial time when you display the values front=-1.
So, add the front=0 in the enqueue function like this,
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
front=0;
rear++;
queueArray[rear] = num;
}
}
Hope you got it. if you have any confusion comment it.

Related

problem with the Queue and int main program in c++?

#include<iostream>
#include<string>
using namespace std;
class Product
{
public:
int static BarCode;
int Quantity;
double Price;
friend class ProudectQueue;
friend class ProductNode;
friend class Customerr;
friend class customerQueue;
//public:
product() {
BarCode= 0, Quantity = 0, Price = 0;
}
product(int barCode, int Quantity, double price)
{
BarCode=barCode ,Quantity=Quantity , Price = price;
}
void DisplayList();
void setBarCode(int bc)
{BarCode=bc;}
int getBarCode()
{return BarCode;}
void setQuantity(int Q)
{Quantity=Q;}
int getQuantity()
{return Quantity;}
void setPrice(int r)
{Price=r;}
int getPrice()
{return Price;}
void printProductDetails()
{ cout<<getBarCode()<<"\n";
cout<<getQuantity()<<"\n";
cout<<getPrice()<<endl;
}
};
class ProductNode //class node
{
public:
Product data;
ProductNode* next;
ProductNode()
{
data.BarCode = 0, data.Quantity = 0, data.Price = 0;
}
ProductNode(int BarCode, int Quantity, double price)
{
data.BarCode = BarCode, data.Quantity=Quantity, data.Price = price;
}
};
class ProductQueue {
ProductNode* rear, * front;
int counter;
public:
ProductQueue() { rear = front = NULL; counter = 0; }
~ProductQueue() { Product Productdata; while (!isempty()) dequeue(Productdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Product ProductData) {
ProductNode* newnode = new ProductNode;
newnode->data = ProductData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Product& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
ProductNode* nextnode;
nextnode = front->next;
data = front->data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(char ProductName)
{
ProductNode* currNode = front;
int position = 1;
while (currNode->data.BarCode!= ProductName)
{
if (currNode != NULL)
currNode = currNode->next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
ProductNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << "Product BarCode: " << currNode->data.BarCode << "\t"
<< "Product Quantity: " << currNode->data.Quantity<< "\t"
<< "Product price : " << currNode->data.Price;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->next;
}
}
bool modify()
{
if(isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int BarCode1;
double Price1;
int Quantity1;
cout << "\nEnter BarCode : "; cin >> BarCode1;
cout << "\nEnter price : "; cin >> Price1;
cout << "\nEnter Quantity : "; cin >> Quantity1;
rear->data.BarCode = BarCode1;
rear->data.Price = Price1;
rear->data.Quantity = Quantity1;
return true;
}
}
};
class Seller //seller class
{
private:
string Name;
int ID, contact;
float Reating;
friend class SellerQueue;
friend class SellerNode;
public:
Seller() { Name = "unknown", ID= 0, contact = 0,Reating=0; }
Seller(string name, int id, int con,float reating) { Name = name, ID=id , contact = con,Reating=reating; }
void setName(string name) { Name = name; }
void setID(int id) { ID = id; }
void setcontact(int con) { contact = con; }
void setReating(float reating){Reating=reating;}
string getName() { return Name; }
int getID() { return ID; }
int getcontact() { return contact; }
float getReating(){ return Reating;}
void printSellerInfo()
{
cout << "\tName : " << Name << "\t";
cout << "\tSeller ID : " << ID << "\t";
cout << "\tcontact: " << contact<< "\t\n";
cout << "\tSeller Reating : " << Reating << "\t";
}
};
class SellerNode
{
public:
Seller Data;
SellerNode* Next;
SellerNode() { Data.Name = "unknown", Data.ID = 0, Data.contact= 0,Data.Reating=0; }
SellerNode(string name, int id, int con,float reating)
{ Data.Name = name, Data.ID = id, Data.contact=con ,Data.Reating=reating;}
};
class SellerQueue {
SellerNode* rear, * front;
int counter;
public:
SellerQueue() { rear = front = NULL; counter = 0; }
~SellerQueue() { Seller Sellerdata; while (!isempty()) dequeue(Sellerdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Seller SellerData) {
SellerNode* newnode = new SellerNode;
newnode->Data = SellerData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->Next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Seller& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
SellerNode* nextnode;
nextnode = front->Next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int SellerID)
{
SellerNode* currNode = front;
int position = 1;
while (currNode->Data.ID != SellerID)
{
if (currNode != NULL)
currNode = currNode->Next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
SellerNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << "Seller name : " << currNode->Data.Name << "\t" <<
"Seller ID:" << currNode->Data.ID << "\t"
<< "contact:" << currNode->Data.contact<<"\t"<<
"Seller Reating"<<currNode->Data.Reating;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->Next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
string name1;
int ID1;
int contact1;
float Reating1;
cout << "\nEnter seller name : "; cin >> name1;
cout << "\nEnter Seller ID : "; cin >> ID1;
cout << "\nEnter contact : "; cin >> contact1;
cout << "\nEnter seller Reating: "; cin >> Reating1;
rear->Data.Name = name1;
rear->Data.ID = ID1;
rear->Data.contact = contact1;
rear->Data.Reating = Reating1;
return true;
}
}
};
class Customer //custmer class
{
public:
int ID;
string Name;
float Point;
int Phone;
friend class Product;
friend class customerQueue;
friend class customerrNode;
Customer() {
ID = 0; Name = "Unknown"; Point = 0;Phone=0;
}
Customer(unsigned int id, string name, float point,int phone) {
ID = id; Name = name; Point = point; Phone=phone;
}
void setID(unsigned int id) { ID = id; }
void setName(string name) { Name = name; }
void setPoint(float point) { Point= point; }
void setPhone(int phone){ Phone= phone; }
void setProductforCustomer(int B, int q, double pr)
{
Product.getBarCode() = B;
Product.getQuantity() = q;
Product.getPrice() = pr; // erorr
}
int getBarCode() { return Product.BarCode; }
int Quantity() { return Product.Quantity; }
double getPrice() { return Product.Price; }
unsigned int getID() { return ID; }
string getName() { return Name; }
float getPoint() { return Point; }
int getPhone(){ return Phone; }
void printCustomerInfo()
{
cout << "\tID number : " << ID << "\t";
cout << "\tCustomer Name : " << Name << "\t";
cout << "\tPoint is : " << Point << "\t";
cout<<"\tphone Number is :"<< Phone<<"\t";
cout << "\tBarCode" << Product.BarCode;
cout << "\tQuantity"<<Product.Quantity;
cout << "\tprice : "<<Product.Price << endl;
}
};
class customerNode
{
public:
customerNode* Next;
Customer Data;
customerNode()
{
Data.ID = 0, Data.BarCode = 0, Data.Name = "unkown";
}
customerNode(int id, string Name, int BarCode)
{
Data.ID = id, Data.BarCode = BarCode, Data.Name = Name; //error
}
};
class customerQueue {
customerNode* rear, * front;
int counter;
public:
customerQueue() { rear = front = NULL; counter = 0; }
~customerQueue() { Customer customerdata; while (!isempty()) dequeue(customerdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Customer customerdata) {
customerNode* newnode = new customerNode;
newnode->Data = customerdata;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->Next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Customer& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
customerrNode* nextnode; //error
nextnode = front->Next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int id)
{
customerNode* CurrNode = front;
int position = 1;
while (CurrNode->Data.ID != id)
{
if (CurrNode != NULL)
CurrNode = CurrNode->Next;
position++;
}
if (CurrNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
customerNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t ";
cout << "Customer ID : " << currNode->Data.ID << " Custmer Name :"
<< currNode->Data.Name <<
" Custmer point: " << currNode->Data.Point<<
" Prouduct BarCode: " << currNode->Data.Product.BarCode <<
" Product Quantity :" << currNode->Data.Product.Quantity<<
" product price : " << currNode->Data.Product.Price;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->Next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int BarCode1;
int Quantity1;
double Price1;
unsigned int ID1;
string Name1;
cout << "\nEnter customer ID : "; cin >> ID1;
cout << "\nEnter customer Name : "; cin >> Name1;
cout << "\nEnter BarCode : "; cin >> BarCode1;
cout << "\nEnter Quantity : "; cin >>Quantity1;
cout << "\nEnter price : "; cin >> Price1;
rear->Data.Product.BarCode = BarCode1;
rear->Data.Product.Quantity = Quantity;
rear->Data.Product.Price = Price1;
return true;
}
}
};
class Action
{
int product,Time;
string Seller, Customer;
double E_bidding;
friend class ActionQueue;
friend class ActionNode;
public:
Action() { Product = 0, Time = 0,Seller="unknown",custmer="unknown",E_bidding=0; }
Action(int Product, int Time,string Seller,string Custmer,double E_bidding)
{ Product=product, Time = time,Seller=seller,Custmer=custmer,E-binding=ebinding; }
void setProduct(int product) { Product=product; }
int getProduct() { return Product; }
void setTime (int time){ Time=time;}
int getTime () { return Time; }
void setSeller (string seller){ Seller = seller; }
string getSeller () { return Seller; }
void setCustomer (string custmor) { Customer = custmor;}
string getCustomer () { return Customer; }
void setE_bidding (double ebidding) { E_bidding = ebidding; }
double getE_bidding () { return E_bidding; }
void printActionDetails()
{
cout<<"Auction Details: "<<"\n"<<getE_bidding()<<"\n"<<getCustomer()<<"\n"<<getProduct()
<<"\n"<<getSeller()<<"\n"<<getTime()<<endl;
}
};
class ActionNode
{
public:
ActionNode Data;
ActionPosition Data;
ActionNode* next;
ActionNode()
{
Data.Product = 0, Data.Time = 0,Data.Seller="unknown",Data.Customer="unknown",Data.E_bidding=0 ;
}
ActionNode(int product, int time,string seller,string customer,double ebidding)
{
Data.Product = product, Data.Time = time,Data.Seller=seller,Data.Customer=customer,Data.E_bidding=ebidding;
}
};
class ActionQueue
{
ActionQueue newnode;
ActionNode* rear, * front;
int counter;
public:
ActionQueue() { rear = front = NULL; counter = 0; }
~ActionQueue() { Action Actiondata; while (!isempty()) dequeue(Actiondata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Action ActionData) {
stockNode* newnode = new stockNode; //error
newnode->Data = ActionData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Action& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
ActionNode* nextnode;
nextnode = front->next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int Product)
{
ActionNode* currNode = front;
int position = 1;
while (currNode->Data.Product != Product)
{
if (currNode != NULL)
currNode = currNode->next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
ActionNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << " Product : " << currNode->Data.Product << "\t" <<
"Time :" << currNode->Data.Time<<"\t"<<
"Seller:"<< currNode->Data.Seller<<"\t"<<
"Customer :" << currNode->Data.Custmoer<<"\t"<<
"E_bidding :" << currNode->Data.E_bidding<<"\t";
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int product1,Time1;
string Seller1, Customer1;
double E_bidding1;
cout << "\nEnter Product : "; cin>>product1;
cout<< "\nEnter Time :" ;cin>>Time1;
cout<< "\nEnter Seller:";cin>>Seller1;
cout<< "\nEnter Customer :" ;cin>>Customer1;
cout<< "\nEnter E_bidding :" ;cin>>E_bidding1;
rear->Data.product = product1;
rear->Data.Time = Time1;
rear->Data.Seller = Seller1;
rear->Data.Customer = Customer1;
rear->Data.E_bidding = E_bidding1;
return true;
}
}
};
int main()
{
Seller sellerObject, valseller;
SellerQueue SellerQueue1;
Product productObject ,valPruduct;
ProductQueue productQueue1;
Customer customerObject, valCostumer;
customerQueue customerQueue1;
Action ActionObject, Action,valAction;
ActionQueue Actionqueue1;
string string1, string2, string3;
int int1, int2, int3,index, intChoice, customerloop, customerTotal;
double double1,double2;
char charChoice;
bool bool1;
float float1,float2,float3;
for (int i = 0;; i++)
{
cout << "Main menu:\n";
cout << "1.Product\n";
cout << "2.Seller\n";
cout << "3.Customer\n";
cout << "4.ACtion\n";
cin >> intChoice;
switch (intChoice)
{
case 1:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
ProductQueue.display();
break;
case 'b':
cout << "Enter product BarCode: "; cin >> int1;
cout << "Enter Quantity : "; cin >> int1;
cout << "Enter product price : "; cin >> double1;
productObject.setBarCode(int1);
productObject.setQuantity(int1);
productObject.setPrice(double1);
ProductQueue.enqueue(ProductObject);
break;
case'c':
cout << ProductQueue.dequeue(valPruduct) << endl;
valPruduct.printProductDetails();
break;
case 'd':
cout << "modify rear element : ";
ProductQueue.modify();
break;
case'e':
cout << "Enter Product BarCode to find it "; cin >> int1;
cout << "\nthe element index : "<<ProductQueue.find(int1) << endl;
break;
case'f':
break;
}
break;
case 2:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
SellerQueue1.display();
break;
case 'b':
cout << "Enter Seller name : "; cin >> string1;
cout << "Enter Seller ID : "; cin >> int2;
cout << "Enter contact : "; cin >> int1;
cout << "Enter Reating : "; cin >> float1;
sellerObject.setName(string1);
sellerObject.setID(string2); //error
sellerObject.setcontact(int1);
sellerObject.setReating(float1);
SellerQueue.enqueue(sellerObject);
break;
case'c':
SellerQueue.dequeue(valseller);
valseller.printSellerInfo();
break;
case 'd' :
cout << "modify rear element : ";
SellerQueue.modify();
break;
case'e':
cout << "Enter Seller ID to find him : "; cin >> int1;
cout << "\nthe element index : " << SellerQueue.find(int1)<<endl;
break;
case'f':
break;
}
break;
case 3:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
customerQueue1.display();
break;
case 'b':
// Customer = 0;
cout << "Enter customer Name : "; cin >> string3;
cout << "Enter customer id : "; cin >> int1;
cout << "Enter customer point : "; cin >> float2;
cout << "Enter customer number : "; cin >> int2;
cout << "Enter BarCode : "; cin >> int2;
cout << "Enter Quantity : "; cin >> double1;
cout << "\tprice : " ;cin >>double2 ;
customerObject.setID(int1);
customerObject.setName(string3);
customerObject.setPoint(flaot3);//??error
customerObject.setProductforCustomer(int1, int2, double1);
customerQueue1.enqueue(customerObject);
for (int j = 1;; i++)
{
cout << "Do you want add a new Prodect to this customer ? \n 1 ""YES"" \n 2 ""NO"" ";
cin >> customerloop;
if (customerloop == 1)
{
cout << "Enter Product BarCode : "; cin >> int1;
cout << "Enter Product Quantity : "; cin >> int2;
cout << "Enter Product price : "; cin >> double1;
// customerObject.setBarCode();
customerObject.setProductforCustomer(int1, int2, double1);
customerQueue1.enqueue(customerObject);
}
else break;
}
break;
case'c':
cout << customerQueue1.dequeue(valCostumer);
valCostumer.printCustomerInfo();
break;
case 'd':
cout << "modify rear element : ";
customerQueue1.modify();
break;
case'e':
cout << "Enter customer id to find him : "; cin >> int1;
cout << "\nthe element index : " << customerQueue1.find(int1)<<endl;
break;
case'f':
break;
}
break;
case 4:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cout << "f.Exit\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
ActionQueue.display();
break;
case 'b':
cout << "\nEnter Product : "; cin>>int1;
cout<< "\nEnter Time :" ;cin>>int2;
cout<< "\nEnter Seller:";cin>>string1;
cout<< "\nEnter Customer :" ;cin>>string2;
cout<< "\nEnter E_bidding :" ;cin>>double1;
ActionObject.setProduct(int1);
ActionObject.setTime(int2);
ActionObject.setSeller(string1);
ActionObject.setCustomer(string2);
ActionObject.setE_bidding(double1);
ActionQueue.enqueue(ActionObject);
break;
case'c':
cout << ActionQueue.dequeue(valAction);
valAction.printActionDetails();
break;
case 'd':
cout << "modify rear element : ";
ActionQueue.modify();
break;
case'e':
cout << "Enter the Prodect to find it :"; cin >> int1;
cout << "\nthe element index : " << ActionQueue.find(int1)<<endl;
break;
case'f':
break;
}
break;
}
}
}
we study a small part about queue so we don't have full knowledge about it. we want to know why in call error, the class customer and class Action have most of the problem also the call in the int main .
most error like this [Error] expected unqualified-id before '.' token.
the error in name or wasn't declared in the scope it's ok we can fix it
pleas help

I having problem with my searching function C++

i'am begginner in C++, i want to ask question about what's wrong about my coding, when i doing my work then found out problem i can't solve in searching function, there's no error notice or anything from visual studio but my function still didn't work,
sorry for my bad english.
here is my full code
#include <iostream>
#include <cstdlib>
#define MAX 5
using namespace std;
struct College {
int nim;
string name;
}mahasiswa;
struct queue {
College college[MAX];
int start,end = - 1;
}antrean;
void init() {
antrean.start= -1;
antrean.end = -1;
}
bool full() {
if (antrean.end== MAX - 1) {
return true;
}
else {
return false;
}
}
bool empty() {
if (antrean.end== -1) {
return true;
}
else {
return false;
}
}
void tampilData() {
int i;
if (!empty()) {
for (i = antrean.start; i < antrean.end; i++)
{
cout<<antrean.college[i].nim<<" | ";
cout << antrean.college[i].name << " | ";
cout << "\n";
}
}
cout<<"\n";
}
void inQueue() {
tampilData();
int elemennim;
string elemenname;
if (!full()) {
cout << "Input your NIM : ";
cin >> elemennim;
cout << "\n";
cout << "Input your name: ";
cin >> elemenname;
cout << "\n";
cout << "Succefully\n";
antrean.college[antrean.end].nim = elemennim;
antrean.college[antrean.end].name = elemenname;
antrean.end++;
}
else
{
cout << "Queue Penuh\n";
}
}
and this the error function i cant solve,
void searching(int key) {
for (int i = 0; i <= antrean.start; i++)
{
if (key == antrean.college[i].nim) {
cout << "Element found in index " << i;
}
else
{
cout << "Element not found\n";
}
cout << antrean.college[i].nim;
}
}
int main() {
int choice, elemen;
init();
cout << "Demo Queue dengan Linear Array" << endl;
do {
tampilData();
cout << "\nMenu Utama\n";
cout << "==============\n";
cout << "[1] Init \n[2] InQueue \n[3] Searching \n[4] out \n";
cout << "==============\n";
cout << "\nMasukkan pilihan: "; cin >> choice;
cout << "==============\n";
switch (choice) {
case 1: init(); break;
case 2: inQueue(); break;
case 3: cout << "masukkan NIM yang ingin dicari \n";
cin >> choice;
searching(choice); break;
} while (choice != 0);
return 0;
}
please help me, thank you very much

static stack function does not 1) take input of 1st element 2)recognise old element when new element is added

There is an array of nodes(a structure) and is used as a
stack.
it has 3 functions
to add new elements (push)
to delete elements(pop)
to display(display)
problems:
does not save the first input
when a new input is added, it replaces the previous node with the new input.
please help me identify where i have gone wrong
#include <iostream>
#include<conio.h>
#include<process.h>
struct node
{
int x, y;
};
int top = -1;
class stack
{
node s[30];
public:
void push();
void pop();
void display();
};
void stack::push()
{
if (top < 29)
{
cout << "enter elements" << endl;
int a, b;
cin >> a >> b;
top = top + 1;
s[top].x = a;
s[top].y = b;
}
else
{
cout << "OVERFLOW" << endl;
}
}
void stack::pop()
{
node temp;
temp = s[top];
top--;
cout << "element" << temp.x << "&" << temp.y << " has been deleted" << endl;
}
void stack::display()
{
for (int i = 0; i < top; i++)
{
cout << s[top].x << "&" << s[top].y << endl;
}
}
void main()
{
clrscr();
stack sup;
int choice = 1;
do
{
cout << "1.add" << endl << "2.delete" << endl << "3.display" << endl;
int c;
cin >> c;
switch (c)
{
case 1:
sup.push();
sup.display();
break;
case 2:
sup.pop();
sup.display();
break;
case 3:
sup.display();
break;
default:
cout << "error in switch case" << endl;
}
cout << "enter 1 to perform more operations" << endl;
cin >> choice;
} while (choice == 1);
getch();
}
You delete and recreate you stack (sup) every iteration of your loop. The declaration should be before the do.

How to implement priority queue in C++?

I am implementing a train boarding system using priority queue. I have the working code but I need to make the following changes..
The priority levels will be : High, Medium and Low. So the passenger should input his/her name and the priority level. The train can have up to 30 passengers.
In the end I will sort the passengers accordingly... Here what I have so far my problem is taking strings as arguments instead of the integers I have now.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
#define High 1
#define Medium 2
#define Low 3
/*
* Node Declaration
*/
struct node
{
int priority;
int info;
struct node *link;
};
/*
* Class Priority Queue
*/
class Priority_Queue
{
private:
node *front;
public:
Priority_Queue()
{
front = NULL;
}
/*
* Insert into Priority Queue
*/
void insert(int item, int priority)
{
node *tmp, *q;
tmp = new node;
tmp->info = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while (q->link != NULL && q->link->priority <= priority)
q = q->link;
tmp->link = q->link;
q->link = tmp;
}
}
/*
* Delete from Priority Queue
*/
void del()
{
node *tmp;
if (front == NULL)
cout << "Queue Underflow\n";
else
{
tmp = front;
cout << "Deleted item is: " << tmp->info << endl;
front = front->link;
free(tmp);
}
}
/*
* Print Priority Queue
*/
void display()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "Queue is empty\n";
else
{
cout << "Queue is :\n";
cout << "Priority Item\n";
while (ptr != NULL)
{
cout << ptr->priority << " " << ptr->info << endl;
ptr = ptr->link;
}
}
}
};
/*
* Main
*/
int main()
{
int choice, item, priority;
Priority_Queue pq;
do
{
cout << "1.Insert\n";
cout << "2.Delete\n";
cout << "3.Display\n";
cout << "4.Quit\n";
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
cin >> priority;
pq.insert(item, priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default:
cout << "Wrong choice\n";
}
} while (choice != 4);
return 0;
}
I would avoid using string values for the queue and just translate plain-text priorities to the respective integer value, think
int ParsePriority(string plainTextPriority)
{
switch(plainTextPriority) {
case "High": return 1;
case "Medium": return 2;
case "Low": return 3;
}
throw "Unknown priority class";
}
You'll then be able to use the priority values like you did before.
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
do {
string plainTextPriority;
cin >> plainTextPriority;
try {
priority = ParsePriority(plainTextPriority)
}
catch {
priority = 0;
cout << "Could not parse the priority, please enter one of High, Medium, Low" << endl;
}
} while (priority == 0);
pq.insert(item, priority);
break;
I added the loop to take the case that a value that does not represent a valid priority has been entered into account.
Here you can use mapping.
int main()
{
int choice, item;
string priority;
map<string,int>m;
m["High"]=1;
m["Low"]=3;
m["Medium"]=2;
Priority_Queue pq;
do
{
cout << "1.Insert\n";
cout << "2.Delete\n";
cout << "3.Display\n";
cout << "4.Quit\n";
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
cin >> priority;
pq.insert(item, m[priority]);//look change
//... rest of the code will same
and you need to include #include<map> also.
Thank you.

copying the front of a queue instance to the rear of another queue instance

I have been doing an assignment for my c++ data structures class and am really stuck.
i have most of the program complete but im struggling on the delete passengers function
the point of the function is to delete the front of [BOOKED] and take the front of [WAITING] and assign it to the rear of [BOOKED]
i cant seem to figure out how to assign the front of booked to the rear of waiting
any help or tips is greatly appreciated thanks
include <iostream>
include <string>
//#include "cqueue.h"
using namespace std;
//1. cqueue.h
const int MAX = 4; //To do: determine appropriate number
struct Passenger {
char name[80]; //the data will be an array of Passenger structures,
};
class CQueue {
private:
int front; //Index "before" the first element of array that holds a value of the queue
int rear; //Index of last element of array that holds a value of the queue
int temp;
Passenger passengers[MAX]; //Holds values being placed on the queue.
public:
CQueue(); // Initializes front and rear. Optionally, you may also want to initialize the values of the data array to some default value.
bool IsEmpty(void); //Returns False (zero) if there is at least one item of the queue in array, True (non-zero) if not.
bool IsFull(void); //Returns True (non-zero) if all elements of the array contain items of the queue, False (zero) if not.
void Enqueue(Passenger); /*Assigns the parameter (same data type as array) to the index following the existing rear element of the array and
changes the rear variable. */
Passenger Front(); //[]
void Dequeue(void); //Changes the front variable.
void tempdequeue();
void showfront();
void frontback();
char x(char[80]);
void assign(string b, Passenger);
};
CQueue:: CQueue ()
{
front = MAX - 1;
rear = MAX-1;
}
bool CQueue::IsEmpty(void)
{ //returns true if empty
return (front == rear);
}
bool CQueue::IsFull(void)
{
//returns true if the queue is full. flase otherwise
//
return(rear + 1) % MAX == front;
}
//2. test.cpp
enum choice { BOOKED, WAITING };
const int LINES = 2;
int showMenu(void);
void addPassenger(CQueue*);
void deletePassenger(CQueue*);
void showPassengers(CQueue*);
int main ()
{
CQueue qPassengers[LINES];
int x;
do{
x = showMenu();
switch (x)
{
case 1: addPassenger(qPassengers);
break;
case 2: deletePassenger(qPassengers);
break;
case 3: showPassengers(qPassengers);
break;
}
} while (x != 4);
return 0;
}
int showMenu()
{
int select;
cout << "Menu\n";
cout << "========\n";
cout << "1. Add Passenger\n";
cout << "2. Delete Passenger\n";
cout << "3. Show Passengers\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> select;
return select;
}
void addPassenger(CQueue* crack)
{
if (crack[BOOKED].IsEmpty())
{
cout << "the queue is empty" << endl;
}
if (crack[BOOKED].IsFull())
{
cout << "the queue is full." << endl;
crack[WAITING].Enqueue(Passenger());
}
else if (crack[BOOKED].IsFull() && crack[WAITING].IsFull())
{
cout << "the plane is full try again later" << endl;
}
else
{
crack[BOOKED].Enqueue(Passenger());
}
}
void CQueue::Enqueue(Passenger a)
{
rear = (rear + 1 ) % MAX;
cin >> a.name;
passengers[rear]= a;
cout<<"front: "<<front<<endl<<endl;
}
void deletePassenger(CQueue* crack)
{
char name [80];
if (crack[BOOKED].IsEmpty())
{
cout << "the queue is empty" << endl;
}
else
{
crack[BOOKED].Dequeue();
crack[WAITING].x(name);
crack[WAITING].Dequeue();
crack[BOOKED].assign(name, Passenger());
}
}
char CQueue::x(char a[80])
{
a = passengers[front].name;
return *a;
}
void CQueue::assign(string pie, Passenger a)
{
cout << pie;
a = pie
passengers[rear]= a;
}
void CQueue::Dequeue()
{
// if (IsEmpty())
// {
// cout << "the slot is empty" << endl;
// }
// else
{
front = (front + 1) % MAX;
}
}
void showPassengers(CQueue* crack)
{
// string x = crack[BOOKED].Front().name;
// cout << x;
crack->showfront();
if (crack[BOOKED].IsEmpty())
{
cout << "No Passengers" << endl;
}
else if (crack[BOOKED].IsEmpty() == false)
{
cout << "Booked Passengers" << endl
<< "===============" << endl;
//if (crack[BOOKED].IsEmpty() == false)
for ( int x = 0 ; x < 3 ; x++)
{
cout << crack[BOOKED].Front().name<< endl;
crack[BOOKED].tempdequeue();
}
// crack->frontback();
cout << "WAITING LIST" << endl;
cout << "===============" << endl;
for ( int x = 0 ; x < 3 ; x++)
{
cout << crack[WAITING].Front().name << endl;
crack[WAITING].tempdequeue();
}
}
crack->frontback();
}
Passenger CQueue::Front(){
return passengers[(front+1)%MAX];
//return passengers[front];
}//Front
void CQueue::showfront()
{
temp = front;
}
void CQueue::frontback()
{
front = temp;
}
void CQueue::tempdequeue()
{
front = (front + 1) % MAX;
}
// To do: implement addPassenger, deletePassenger and showPassengers
You should look at the standard queue from std::queue
You have CQueue::Front() which returns the first element in the queue and then remove it from that queue
Passenger firstElement = cQueue.Front();
cQueue.Dequeue();
Then your other queue should have
cQueue2.addPassenger(firstElement);