Question

In: Computer Science

Please in C++ (Carrano) Use Exercises 3. Page 391. Chap 13 to. As the main program...

Please in C++
(Carrano) Use Exercises 3. Page 391. Chap 13 to. As the main program for the LinkedQueue structure p. 403 Chap 14.

the linkedQueue structure is added.

3. What is the output of the following pseudocode, where num1, num2, and num3 are integer variables?

num1 = 5
num2 = 1
num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue()

num2 = aQueue.peek()
aQueue.dequeue()
cout << num2 << " " << num1 << " " << num3 << endl

=============================================

#include "QueueInterface.h"

#include "Node.h"

#include "PrecondViolatedExcep.h"

template<class ItemType>

class LinkedQueue : public QueueInterface<ItemType> {

private:

   // The queue is implemented as a chain of linked nodes that has

// two external pointers, a head pointer for the front of the queue // and a tail pointer for the back of the queue.

Node<ItemType>* backPtr;

Node<ItemType>* frontPtr;

public:

LinkedQueue();

LinkedQueue(const LinkedQueue& aQueue); ~LinkedQueue();

bool isEmpty() const;

bool enqueue(const ItemType& newEntry); bool dequeue();

/** @throw PrecondViolatedExcep if the queue is empty */

ItemType peekFront() const throw(PrecondViolatedExcep); }; // end LinkedQueue

=======================

#pragma once

QueueInterface.cpp

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 13-1.

@file QueueInterface.h */

#ifndef _QUEUE_INTERFACE

#define _QUEUE_INTERFACE

template<class ItemType>

class QueueInterface

{ public:

/** Sees whether this queue is empty.

@return True if the queue is empty, or false if not. */

virtual bool isEmpty() const = 0;

/** Adds a new entry to the back of this queue.

@post If the operation was successful, newEntry is at the

back of the queue.

@param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */

virtual bool enqueue(const ItemType& newEntry) = 0;

/** Removes the front of this queue.

@post If the operation was successful, the front of the queue

has been removed.

@return True if the removal is successful or false if not. */

virtual bool dequeue() = 0;

/** Returns the front of this queue.

@pre The queue is not empty.

@post The front of the queue has been returned, and the

queue is unchanged.

@return The front of the queue. */

virtual ItemType peekFront() const = 0; }; // end QueueInterface

#endif

======================

#ifndef _NODE

#define _NODE

template<class ItemType>

class Node

{

private:

   ItemType item; // A data item

   Node<ItemType>* next; // Pointer to next node

  

public:

   Node();

   Node(const ItemType& anItem);

   Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);

   void setItem(const ItemType& anItem);

   void setNext(Node<ItemType>* nextNodePtr);

   ItemType getItem() const ;

   Node<ItemType>* getNext() const ;

}; // end Node

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.cpp

Listing 4-2 */

#include <cstddef>

template<class ItemType>

Node<ItemType>::Node() : next(nullptr)

{

} // end default constructor

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

} // end constructor

template<class ItemType>

Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :

item(anItem), next(nextNodePtr)

{

} // end constructor

template<class ItemType>

void Node<ItemType>::setItem(const ItemType& anItem)

{

   item = anItem;

} // end setItem

template<class ItemType>

void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)

{

   next = nextNodePtr;

} // end setNext

template<class ItemType>

ItemType Node<ItemType>::getItem() const

{

   return item;

} // end getItem

template<class ItemType>

Node<ItemType>* Node<ItemType>::getNext() const

{

   return next;

} // end getNext

#endif

=========================

PrecondViolatedExcep.h

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-5.

@file PrecondViolatedExcep.h */

#ifndef _PRECOND_VIOLATED_EXCEP

#define _PRECOND_VIOLATED_EXCEP

#include <stdexcept>

#include <string>

using namespace std;

class PrecondViolatedExcep : public logic_error

{

public:

   PrecondViolatedExcep(const string& message = "");

}; // end PrecondViolatedExcep

#endif

PrecondViolatedExcep.cpp

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-6.

@file PrecondViolatedExcep.cpp */

#include "PrecondViolatedExcep.h"

PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message)

{

} // end constructor

// End of implementation file.

Solutions

Expert Solution

you need to know queue and compling process.

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

here some method of queue which is working on that

enqueue(): Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
dequeue(): Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.

peek(): which is return value at top or front position.

According to given question info, It is doing this :

#include <bits/stdc++.h> 
using namespace std; 
  
struct QNode { 
    int data; 
    QNode* next; 
    QNode(int d) 
    { 
        data = d; 
        next = NULL; 
    } 
}; 
  
struct Queue { 
    QNode *front, *rear; 
    Queue() 
    { 
        front = rear = NULL; 
    } 
  
    void enqueue(int x) 
    { 
  
        // Create a new LL node 
        QNode* temp = new QNode(x); 
  
        // 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; 
  
        // 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); 
    } 
    
    int peek()
    {
        if (front==NULL)
        {
                cout << "UnderFlow\nProgram Terminated\n";
                exit(EXIT_FAILURE);
        }
        return front->data;
}
}; 
  
int main()
{
    
int num1 = 5; //Dealare integer variable from num1 and store value 5
int num2 = 1 ;//Dealare integer variable from num2 and store value 1
int  num3 = 4 ;//Dealere integer variable from num3 and store value 4
 cout<<num2<<" "<<num1<<" "<<num3<<endl;
Queue aQueue;
aQueue.enqueue(num2); // inserting value from num2 variable to queue  
cout<<num2<<" "<<num1<<" "<<num3<<endl;
aQueue.enqueue(num3); // inserting value from num3 variable to queue
cout<<num2<<" "<<num1<<" "<<num3<<endl;
aQueue.dequeue(); // remove value from last , that's num3 or 4
cout<<num2<<" "<<num1<<" "<<num3<<endl;
aQueue.enqueue(num1 - num2); //inserting value  which is substraction of num1 and num2 i.e(5-1=4), now 4 is inserting into queue
cout<<num2<<" "<<num1<<" "<<num3<<endl;
num1 = aQueue.peek(); // return value at initial position which is called top, and store into num1 that's num1=1 ,due to first time inserting value from num2   
cout<<num2<<" "<<num1<<" "<<num3<<endl;
aQueue.dequeue(); //remove element from last , that's is (num1-num2)
cout<<num2<<" "<<num1<<" "<<num3<<endl;
num2 = aQueue.peek(); // return top value(initial position) and assigned to num2 which is 1. 
cout<<num2<<" "<<num1<<" "<<num3<<endl;
aQueue.dequeue(); //again remove last element from queue which is only contains 1 element and now "queue is null"
cout<<num2<<" "<<num1<<" "<<num3<<endl;
 return 0;   
}

thank you.


Related Solutions

Review Exercises questions 1-4 page 13 REVIEW EXERCISES: 1. Why are you interested in international economics?...
Review Exercises questions 1-4 page 13 REVIEW EXERCISES: 1. Why are you interested in international economics? What is motivating you? How are your interests, major, or profession affected by the world economy? 2.   What are the four windows on the world economy? 3.   What is the difference between trade in goods and trade in services? 4. What is the difference between international trade and foreign direct investment?
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
Please use c++ Consider the code on the next page. It creates a vector of five...
Please use c++ Consider the code on the next page. It creates a vector of five strings of vegetable names and you want to make the vector contain only vegetable names that you like. In the spaces designated, perform the following: Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable. With the same iterator, use .begin() to point it to the beginning of the list, and...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT