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
  • 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...

  • 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...

  • 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...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • 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...

  • 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;...

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • 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....

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