Question

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 the following private member variables

▪ A pointer to a NodeT that represents the front of the queue ▪ A pointer to a NodeT that represents the back of the queue

▪ An int that records the current size of the queue (i.e. the number of values in the queue) Queue Public Methods You must implement a QueueT template class to store data of any type. The queue must be implemented using a singly linked list of Nodes, where NodeT is a class that you also implement. Note that the enqeue and deqeue methods should be implemented in constant time. The QueueT class should implement the following public methods:

▪ constructor – creates an empty queue

▪ copy constructor – a constructor that creates a deep copy of its constant QueueT reference parameter

▪ destructor – deallocates dynamic memory allocated by the queue

▪ operator= – overloads the assignment operator for QueueT – (deep) copies its constant QueueT reference parameter into the calling object and returns a reference to the calling object after de-allocating any dynamic memory associated with the original contents of the calling object; if the calling object is equal to the parameter the operator should not copy it

▪ enqueue – inserts its template type parameter at the back of the queue ▪ dequeue – removes and returns the value at the front of the queue; if the queue is empty throws a runtime_error (this error class is defined in the stdexcept library file)

▪ print – prints the contents of the queue, one value per line, from front to back

▪ empty – returns true if the queue is empty, false otherwise ▪ size – returns the number of items stored in the queue

▪ concatenate – has two parameters, a QueueT reference and an integer (referred to as n in this description) adds the first n values stored in its QueueT parameter to the end of the calling object, the resulting queue therefore contains its original contents – order unchanged – followed by the first n values of the parameter queue in their original order; the values added to the calling object should be removed from the parameter; both queue's sizes should be adjusted as appropriate; if n is greater than the size of the parameter a runtime_error should be thrown and no changes made to either queue

▪ merge – returns a QueueT object that contains the contents of the calling object and its constant QueueT reference parameter, the resulting queue should start with the first value in the calling object, followed by the first value in the parameter, subsequent values should be copied – in order – from the two queues alternating between them; no changes should be made to either the calling object or the parameter; example: calling object = {a,b,c,d,e}, parameter = {r,s,t}, result = {a,r,b,s,c,t,d,e}

▪ getFront – returns a pointer to the node at the front of the queue. This method is provided to you here: NodeT* getFront() { return front; }; Its purpose is to allow us to directly access your queue's linked list for testing. It is not something a working class should include since allowing access to the internal structure of a class is not good design. Note that if you called your pointer to the node at the front of the queue something other than "front" you will need to change the method given above to return the correct attribute. Additional Notes ▪ The calling object should be made constant for any method where the calling object's attribute values should not change

▪ You may implement helper methods if you wish (for example, you might want to implement a deep copy method that can be used by the copy constructor and the overloaded assignment operator)

▪ Your class may include other private attributes that you deem necessary ▪ Method parameters and return values are noted (and highlighted) in the method descriptions – you must not add additional parameters to the methods; if the method description does not mention a parameter it does not have one, similarly if no return value is mentioned the method is void (or a constructor or destructor)

▪ The parameter type for enqeue and the return type of deqeue should be your template variable – see the implementation notes near the end of this document.

#include
using namespace std;
  
class QNode {
   public:
int data;
QNode* next;
QNode(int d)
{
data = d;
next = NULL;
}
};
  
class Queue {

   private:
QNode *front, *rear;
int size;
  
public:
  
void setFront(QNode* node){
   front = node;
}
void setRearNext(QNode* node){
   rear->next = node;
   rear = rear->next;
}
  
void setRear(QNode* node){
   rear = node;
}
  
QNode getFront(){
   return front;
}
  
QNode setFront(){
   return rear;
}
  
//Copy Constructor
Queue( const Queue &newQueue){
  
}
  
//destructor
~Queue(){}
  
  
//Create an empty queue, Constructor
Queue(){       
front = rear = NULL;
size=0;
}
  
   //Inserting a value
void enQueue(int x){      
  
// Create a new LL node
QNode* temp = new QNode(x);
       size++;
// If queue is empty, then
// new node is front and rear both
if (rear == NULL) {
front = rear = temp;
return;
}
  
// Add the new node at
// the end of queue and change rear
rear->next = temp;
rear = temp;
}
  
// Function to remove a key from given queue q
void deQueue(){
// If queue is empty, return NULL.
if (front == NULL)
return;
       size--;
// Store previous front and move front one node ahead
QNode* temp = front;
front = front->next;
  
// If front becomes NULL, then change rear also as NULL
if (front == NULL)
rear = NULL;
  
delete (temp);
}

   //get size of queue
   int getSize(){
       int size=0;
       QNode *current = front;
       while(current!=NULL){
           size++;
           current=current->next;
       }
       return size;
   }
  
   //Print contents of queue
   void printContents(){
       QNode *current = front;
       while(current!=NULL){
           cout<data<<" ";
           current=current->next;
       }
       cout<    }
  
   //adding content of given queue to its end
   void addQueue(QNode* start){
       //if first queue is empty
       if(front==NULL){
           front= start;
           while(start->next!=NULL){
               rear=start;
           }
           return;
       }
      
       while(start!=NULL){
           rear->next=start;
           start=start->next;
           rear=rear->next;
       }
   }
  
   //adding 2 queues to 3rd new queue
   //return a pointer to the Queue
   Queue add2queues(QNode* start){
       Queue third = new Queue();
      
       if(front!=NULL){
           third.setFront(front);
           third.setRear(rear);
           //second queue starts
           while(start!=NULL){
           setRearNext(start);
           start=start->next;
           }
          
       }else{
           third.setFront(start);
           third.setRear(start);
           while(start!=NULL){
           setRearNext(start);
           start=start->next;
           }
       }
}
};
  
// Driven Program
int main()
{
     
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

YOU CAN COMPILE THIS CODE

#include <bits/stdc++.h>
using namespace std;

  
class QNode {
public:
int data;
QNode* next;
QNode(int d)
{
data = d;
next = NULL;
}
};
  
class Queue {
private:
QNode *front, *rear;
int size;
  
public:
  
void setFront(QNode* node){
front = node;
}
void setRearNext(QNode* node){
rear->next = node;
rear = rear->next;
}
  
void setRear(QNode* node){
rear = node;
}
  
QNode*getFront(){
return front;
}
  
QNode* setFront(){
return rear;
}
  
//Copy Constructor
Queue( const Queue &newQueue){
  
}
  
//destructor
~Queue(){}
  
  
//Create an empty queue, Constructor
Queue(){   
front = rear = NULL;
size=0;
}
  
//Inserting a value
void enQueue(int x){
  
// Create a new LL node
QNode* temp = new QNode(x);
size++;
// If queue is empty, then
// new node is front and rear both
if (rear == NULL) {
front = rear = temp;
return;
}
  
// Add the new node at
// the end of queue and change rear
rear->next = temp;
rear = temp;
}
  
// Function to remove a key from given queue q
void deQueue(){
// If queue is empty, return NULL.
if (front == NULL)
return;
size--;
// Store previous front and move front one node ahead
QNode* temp = front;
front = front->next;
  
// If front becomes NULL, then change rear also as NULL
if (front == NULL)
rear = NULL;
  
delete (temp);
}
//get size of queue
int getSize(){
int size=0;
QNode *current = front;
while(current!=NULL){
size++;
current=current->next;
}
return size;
}
  
//Print contents of queue
void printContents(){
QNode *current = front;
while(current!=NULL){
cout<<current->data<<" ";
current=current->next;
}
}
  
//adding content of given queue to its end
void addQueue(QNode* start){
//if first queue is empty
if(front==NULL){
front= start;
while(start->next!=NULL){
rear=start;
}
return;
}
  
while(start!=NULL){
rear->next=start;
start=start->next;
rear=rear->next;
}
}
  
//adding 2 queues to 3rd new queue
//return a pointer to the Queue
Queue add2queues(QNode* start){
// Queue third = new Queue();
Queue third;
  
if(front!=NULL){
third.setFront(front);
third.setRear(rear);
//second queue starts
while(start!=NULL){
setRearNext(start);
start=start->next;
}
  
}else{
third.setFront(start);
third.setRear(start);
while(start!=NULL){
setRearNext(start);
start=start->next;
}
}
}
};
  
// Driven Program
int main()
{
Queue m;
cout<<"Queue :";
m.enQueue(10);
m.enQueue(20);
m.enQueue(30);
m.printContents();
cout<<"\nSize = "<<m.getSize()<<endl;
cout<<"\nAfter deletion";
cout<<"\n Queue :";
m.deQueue();
m.printContents();
cout<<"\nSize = "<<m.getSize()<<endl;
  
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Code in C++. Can someone make it so that the code below can be compiled? ▪...
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...

  • PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of...

    PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode {   string data;   qNode* next;   qNode* prev; }; class CBQueue {   public:     CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( );   private:     qNode* front;     qNode* rear;     int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...

  • PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”,...

    PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...

  • 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 C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

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

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