In: Computer Science
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.
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.