We need to create a queue of items defined by the following structure. struct Qu
ID: 3848899 • Letter: W
Question
We need to create a queue of items defined by the following structure.
struct QueueItem
{
string fname;
string lname;
double salary;
QueueItem *next;
};
Use the following class definition to create a queue.
class Queue
{
private:
QueueItem *front;
QueueItem *back;
bool isempty;
public:
Queue()
{
front=NULL;
back=NULL;
isempty=true;
}
void enque(QueueItem *item);
void deque();
};
Write definition for the enque and deque function. Use the following main function to test your code.
int main()
{
ifstream fin;
fin.open("HW3Data.dat");
Queue my_queue;
string fname, lname;
double salary;
QueueItem *item=new QueueItem;
while(fin>>fname>>lname>>salary)
{
item->fname=fname;
item->lname=lname;
item->salary=salary;
my_queue.enque(item);
}
for (int i=0;i<9;++i)
my_queue.deque();
return 0;
}
You must get the following output.
John Harris 50000
Lisa Smith 75000.5
Adam Johnson 68500.1
Sheila Smith 150000
Tristen Major 75800.8
Yannic Lennart 58000.6
Lorena Emil 43000
Tereza Santeri 48000
Queue is empty. Nothing to remove.
Explanation / Answer
Queue::enque(QueueItem item) {
if (back == NULL) //inserting 1st item in the queue
front = back = item;
else //inserting item when the queue is not empty
back = back->next = item;
back->next = NULL; //this makes sure that 'back' node marks end of the queue
}
Queue::deque(){
if (front == NULL) { //if the queue is empty
cout << "Queue is empty. Nothing to remove." << endl; //print error msh
return;
}
cout << front->fname << " " << front->lname << " " << front->salary << endl; //print details of front node
front = front->next;
if (front == NULL) //if queue becomoes empty
back = NULL; //Nullify back also
}
These are the two functions you need. I have commented the code to make things easy, and I hope you loke them. Let me know if you need any guidance. I shall be glad to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.