Question

//Look for** to complete this program --C+ please --please add comments // using namespace std; #include <iostream> #include
//File type: ** queue.cpp using namespace std; #include <iostream> #include queue.h // constructor queue::queue () } //dest
queue::queue { } //destructor queue: queue() // nothing } // PURPOSE: returns true if queue is empty, otherwise false bool qu
// PARAMETER: provide a holder (removed Elem) for the element removed (pass by ref) void queue::remove (el_t& removed Elem) /
// PURPOSE: if empty, throws an exception Underflow //if queue has just 1 element, does nothing //if queue has more than 1 el
== == // Look for **and complete them File type: queue header file queue.h using namespace std; #include<string> /H-- Globall
queue (); /constructor to create an object queue); //destructor to destroy an object // PURPOSE: returns true if queue is emp
// if not empty, give back the front element (but does not remove it) // PARAMETER: provide a holder for the element (pass by
//Look for** to complete this program --C+ please --please add comments // using namespace std; #include #include #include #include "queue.h" //Purpose of the program: ** Algorithm: * int main() /** "A", "B", "C" in the queue //** while loop -- indefinitely { try {//** catches } //* end of loop } |/ II II II II II
//File type: ** queue.cpp using namespace std; #include #include "queue.h" // constructor queue::queue () } //destructor queue::~queue()
queue::queue { } //destructor queue: queue() // nothing } // PURPOSE: returns true if queue is empty, otherwise false bool queue::isEmpty() { } // PURPOSE: returns true if queue is full, otherwise false bool queue::isFull // PURPOSE: if full, throws an exception Overflow // if not full, enters an element at the rear // PAREMETER: provide the element (newElem) to be added void queue::add(el_t newElem) // PURPOSE: if empty, throw Underflow // if not empty, removes the front element to give it back // PARAMETER: provide a holder (removedElem) for the element removed (pass by ref)
// PARAMETER: provide a holder (removed Elem) for the element removed (pass by ref) void queue::remove (el_t& removed Elem) // PURPOSE: if empty, throws an exception Underflow // if not empty, give back the front element (but does not remove it) //PARAMETER: provide a holder (theElem) for the element (pass by ref) void queue::frontElem(el_t& theElem) / PURPOSE: returns the current size to make it // accessible to the client caller int queue::getSize () // PURPOSE: display everything in the queue horizontally //from front to rear enclosed in [ // if empty, displays [empty ] void queue::displayAll if (isEmpty) cout
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code for Queue operation using an array -->

#include <iostream>

#include<stdlib.h>

#include<string>

using namespace std;

typedef string el_t;

const int MAX_SIZE = 10;

// Purpose of this program : Implement the operations of queue using an array

// Algorithm :

class Queue {

private :

el_t el[MAX_SIZE];

int front, back;

public :

Queue();

~Queue();

bool isEmpty();

bool isFull();

void add(el_t);

void removeElem(el_t&);

void frontElem(el_t&);

int getSize();

void displayAll();

void goToBack();

};

// constructor

Queue::Queue()

{

front = back = 0;

}

// destructor

Queue::~Queue() { }

bool Queue::isEmpty()

{

return (front == back);

}

bool Queue::isFull()

{

return (back == MAX_SIZE);

}

void Queue::add(el_t newElement)

{

if (isFull()) {

throw "Overflow exception";

}

// insert element at the back

else {

el[back] = newElement;

back++;

}

}

void Queue::removeElem(el_t &removedElem)

{

if (isEmpty())

{

throw "Underflow exception";

}

else

{

removedElem = el[front];

for (int i = 0; i < back - 1; i++) {

el[i] = el[i + 1];

}

// decrement back of the queue

back--;

}

}

void Queue::frontElem(el_t& frontElem)

{

if (isEmpty())

{

throw "Underflow exception";

}

else

{

frontElem = el[front];

}

}

int Queue::getSize()

{

return back + 1;

}

void Queue::displayAll()

{

int i = front;

if (isEmpty()) {

cout<<"[ empty ]"<<endl;

}

else

{

cout<<"[";

for (i = front; i < back; i++) {

cout<<"<-- "<<el[i];

}

cout<<"]";

}

}

void Queue::goToBack()

{

if (isEmpty())

{

throw "Underflow exception";

}

else

{

int arraySize = sizeof(el)/sizeof(el[0]);

if (arraySize > 1)

{

el_t removedElement;

removeElem(removedElement);

add(removedElement);

}

}

}

int main() {

Queue q;

q.add("A");

q.add("B");

q.add("C");

// Intial printing

q.displayAll();

el_t removedElem;

try {

// removing element from front

q.removeElem(removedElem);

// adding element at back

q.add(removedElem);

// print queue after insertion and deletion

q.displayAll();

} catch (exception& e){

cout<<e.what();

} return 0;

}

Add a comment
Know the answer?
Add Answer to:
//Look for** to complete this program --C+ please --please add comments // using namespace std; #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT