Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How can I have my deleteFront return the value using passby Reference? #ifndef LQUEUE #define LQUEUE #include using namespace std; typedef int el_t; //create a
How can I have my deleteFront return the value using passby Reference?
#ifndef LQUEUE #define LQUEUE #includeusing namespace std; typedef int el_t; //create a struct that will have an operand, a operator, another operand struct node { el_t elem; node* next; }; class LQueue { // Data members private: node* front; // address to the front element node* rear; // address to the last element int count; // how many elements do we have right now? void queueError(string); // This displays an error messages passed to it and does exit(1); public: LQueue(); //Purpose to destroy //Parameters: nothing ~LQueue(); void addRear(el_t elem); // holds the function for adding an element to the queue //Purpose to delete elements from queue //Parameters: nothing el_t deleteFront(); //Purpose to displays the content of the queue //Parameters: nothing bool isEmpty(); void displayAll(); // holds the function for showing all elements in the stack //Purpose to display the queue in reverse order }; #endif
________________________________ #include#include "lqueue.h" //PURPOSE: Constructor which initilizes front, rear, and count LQueue::LQueue() { front = NULL; rear = NULL; count = 0; } //PURPOSE: When the program hits and endbrace '}', delete the LQueue //ALGORITHM: While the LQueue isn't empty, delete front LQueue::~LQueue() { while (!isEmpty()) { deleteFront(); } } void LQueue::addRear(el_t value) { if (isEmpty()) { front = new node; rear = front; front->elem = value; front->next = NULL; count++; } else { rear->next = new node; rear = rear->next; rear->elem = value; rear->next = NULL; count++; } } //PURPOSE:To delete the front pointer and display the element //ALGORITHM:while the lqueue isn't empty, set a temp element to front->next // and deletethe front. Set the temp to the front and return the element el_t LQueue::deleteFront() { if (isEmpty()) { queueError("UNABLE TO REMOVE FRONT. QUEUE UNDERFLOW."); return 0; } else { node* second = front->next; int sec = front->elem; delete front; front = second; count--; return sec; } } //PURPOSE: To return if a queue is empty or not (returns true or false) //ALGORITHM:if count is at the initilized state of 0, return true, else return false bool LQueue::isEmpty() { if (count == 0) return true; else return false; } //PURPOSE:To display all the elements in the lqueue //ALGORITHM: While the lqueue isn't empty, put a temp pointer to the front // and while the temp->next = NULL, keep displaying and moving // the element void LQueue::displayAll() { if (isEmpty()) { cout << "[EMPTY]" << endl; } else { node* p = front; while (p != NULL) { cout << "["; cout << p->elem; p = p->next; cout << "]" << " "; } cout << endl; } } //PURPOSE: (Private) To handle unexpected errors encountered by other // methods //ALGORITHM: 'cout' the message and exit program with error code 1 void LQueue::queueError(string msg) { cout << msg << endl; }
___________________________________
Client.cpp
#include#include "lqueue.h" int main() { LQueue L; if (L.isEmpty()) cout << "Linked list is empty" << endl; else cout << "Linked list is not empty" << endl; L.displayAll(); L.addRear(1); L.displayAll(); L.addRear(2); L.addRear(3); L.addRear(4); L.displayAll(); cout << "ELEMENT REMOVED: " << L.deleteFront() << endl; cout << "ELEMENT REMOVED: " << L.deleteFront() << endl; cout << "ELEMENT REMOVED: " << L.deleteFront() << endl; L.displayAll(); if (L.isEmpty()) cout << "Linked list is empty" << endl; else cout << "Linked list is not empty" << endl; cout << "ELEMENT REMOVED: " << L.deleteFront() << endl; if (L.isEmpty()) cout << "Linked list is empty" << endl; else cout << "Linked list is not empty" << endl; L.displayAll(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started