in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers
Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that
(2) Testing
With a reference to main.cpp provided, enhance it so that you test Stack class for int type and Student type that are attached with this assignment. The required details are as follows.
Please comment as well to help me understand what is happening
The following code below is to help
you get started :
dNode.cpp:
#include "dNode.h"
// constructor: create a new Node with d as data
dNode::dNode(double data) {
this->data = data;
next = 0;
}
dNode::~dNode() {
}
dStack.cpp:
#include <cstdlib> //for exit()
#include "dStack.h"
using namespace std;
// constructor: new stack is created. topNode is null.
dStack::dStack() {
topNode = 0;
}
//copy constructor
dStack::dStack(const dStack& copy)
{
dNode* tempNode = copy.topNode;
if(tempNode == NULL) { //same as !tempNode because NULL is 0 in
C++
topNode = NULL;
}
else {
dNode* newNode = topNode = new
dNode(tempNode->data);
while(tempNode->next) {
tempNode =
tempNode->next;
newNode->next
= new dNode(tempNode->data);
newNode =
newNode->next;
}
}
}
dStack& dStack::operator =(const dStack &original)
{
dStack localStack(original);
dNode* tempNode = this->topNode;
this->topNode = localStack.topNode;
localStack.topNode = tempNode;
return *this;
}
// destructor: free all the memory allocated for the stack
dStack::~dStack() {
while (!isEmpty())
pop();
}
// push a data onto the stack
void dStack::push(double data) {
try {
dNode* newNode = new dNode(data);
newNode->next = topNode;
topNode = newNode;
} catch (bad_alloc &e) {
//e.what(): returns a char sequence for exception
details
cout << "memory allocation exception: " << e.what()
<< endl;
exit(1);
}
}
// pop the data from the stack
void dStack::pop() {
if (isEmpty())
throw StackEmptyException();
dNode* discard = topNode;
topNode = topNode->next;
delete discard;
}
// read the data on the top of the stack
double dStack::top() {
if (isEmpty())
throw StackEmptyException();
return topNode->data;
}
// is stack empty?
bool dStack::isEmpty() {
return (topNode == 0);
}
// is stack full?
bool dStack::isFull() {
return false; // never, unless memory is full
}
main.cpp:
#include <iostream>
#include "dStack.h"
using namespace std;
void stack_test();
int main() {
stack_test();
return 0;
}
void stack_test() {
dStack s1;
// Stack empty test
if (s1.isEmpty()) {
cout << "s1 is empty at the beginning." << endl;
} else {
cout << "s1 must be empty. Something's wrong!" <<
endl;
}
for(int i = 0; i < 5; i++)
s1.push(1.1 * (i + 1));
dStack s2(s1);
cout << "S2: Expected: 5.5 4.4 3.3 2.2 1.1 -->" <<
endl;
for (int i = 0; i < 5; i++) {
cout << s2.top() << endl;
s2.pop();
}
// pop() test: reverses the items
cout << "S1: Expected: 5.5 4.4 3.3 2.2 1.1 -->" <<
endl;
for (int i = 0; i < 5; i++) {
cout << s1.top() << endl;
s1.pop();
}
// Stack empty test
if (s1.isEmpty()) {
cout << "s1 is empty after five pop() calls." <<
endl;
} else {
cout << "s1 must be full. Something's wrong!" <<
endl;
}
// StackEmptyException test
try {
cout << "One more pop when the stack is empty..." <<
endl;
s1.pop();
}
catch (dStack::StackEmptyException) {
cout << "Exception: cannot pop, stack is empty" <<
endl;
}
dStack s3;
s3 = s1;
for(int i = 0; i < 5; i++)
s3.push(1.1 * (i + 1));
}
dNote.h:
/*
* Programming II, Dr. Sung
*/
#ifndef DNODE_H
#define DNODE_H
class dNode {
private:
double data;
dNode* next;
public:
dNode(double);
virtual ~dNode(); //for later use of polymorphism, review the topic
again
friend class dStack; // allows dStack for private member
access
};
#endif
dStack.h:
/*
* Programming II, Dr. Sung
*/
#ifndef DSTACK_H
#define DSTACK_H
#include <iostream>
#include <new>
#include "dNode.h"
using namespace std;
class dStack {
private:
dNode* topNode;
public:
class StackEmptyException { };
dStack();
dStack(const dStack& copy);
dStack& operator=(const dStack&);
virtual ~dStack();
void push(double);
void pop();
double top();
bool isEmpty();
bool isFull();
};
#endif
// dNode.h
#ifndef DNODE_H
#define DNODE_H
template <typename T>
class dNode {
private:
T data;
dNode* next;
public:
dNode(T);
virtual ~dNode(); //for later use of polymorphism, review the topic again
template <typename U>
friend class dStack; // allows dStack for private member access
};
template <class T>
dNode<T>::dNode(T data)
{
this->data = data;
next = NULL;
}
template<class T>
dNode<T>::~dNode()
{}
#endif
//end of dNode.h
// dStack.h
#ifndef DSTACK_H
#define DSTACK_H
#include <cstdlib> //for exit()
#include "dNode.h"
#include <iostream>
#include <new>
using namespace std;
template <class T>
class dStack
{
private:
dNode<T>* topNode;
public:
class StackEmptyException { };
dStack();
dStack(const dStack<T>& copy);
dStack& operator=(const dStack<T>&);
virtual ~dStack();
void push(T);
void pop();
T top();
bool isEmpty();
bool isFull();
};
template <class T>
dStack<T>::dStack()
{
topNode = NULL;
}
template <class T>
dStack<T>::dStack(const dStack<T>& copy)
{
dNode<T>* tempNode = copy.topNode;
if(tempNode == NULL) { //same as !tempNode because NULL is 0 in C++
topNode = NULL;
}
else {
dNode<T>* newNode = topNode = new dNode<T>(tempNode->data);
while(tempNode->next) {
tempNode = tempNode->next;
newNode->next = new dNode<T>(tempNode->data);
newNode = newNode->next;
}
}
}
template <class T>
dStack<T>& dStack<T>::operator =(const dStack<T> &original)
{
dStack localStack(original);
dNode<T>* tempNode = this->topNode;
this->topNode = localStack.topNode;
localStack.topNode = tempNode;
return *this;
}
template <class T>
// destructor: free all the memory allocated for the stack
dStack<T>::~dStack() {
while (topNode != NULL)
{
dNode<T> *tempNode = topNode;
topNode = topNode->next;
cout<<"\n Address of node deleted : "<<tempNode;
delete(tempNode);
}
}
template <class T>
// push a data onto the stack
void dStack<T>::push(T data) {
try {
dNode<T>* newNode = new dNode<T>(data);
newNode->next = topNode;
topNode = newNode;
}catch (bad_alloc &e) {
//e.what(): returns a char sequence for exception details
cout << "memory allocation exception: " << e.what() << endl;
exit(1);
}
}
template <class T>
// pop the data from the stack
void dStack<T>::pop() {
if (isEmpty())
throw StackEmptyException();
dNode<T>* discard = topNode;
topNode = topNode->next;
delete discard;
}
template <class T>
// read the data on the top of the stack
T dStack<T>::top() {
if (isEmpty())
throw StackEmptyException();
return topNode->data;
}
template <class T>
// is stack empty?
bool dStack<T>::isEmpty() {
return (topNode == NULL);
}
template <class T>
// is stack full?
bool dStack<T>::isFull() {
return false; // never, unless memory is full
}
#endif
//end of dStack.h
// main.cpp
// C++ program to test the Dynamic Stack using int (similarly test for Student class)
#include <iostream>
#include "dStack.h"
using namespace std;
//function to test the stack for int type
void stack_test();
int main() {
stack_test();
return 0;
}
void stack_test() {
dStack<int> s1;
// Stack empty test
if (s1.isEmpty()) {
cout << "s1 is empty at the beginning." << endl;
} else {
cout << "s1 must be empty. Something's wrong!" << endl;
}
for(int i = 0; i < 5; i++)
s1.push( (i + 1));
dStack<int> s2(s1);
cout << "S2: Expected: 5 4 3 2 1 -->" << endl;
for (int i = 0; i < 5; i++) {
cout << s2.top() << endl;
s2.pop();
}
// pop() test: reverses the items
cout << "S1: Expected: 5 4 3 2 1 -->" << endl;
for (int i = 0; i < 5; i++) {
cout << s1.top() << endl;
s1.pop();
}
// Stack empty test
if (s1.isEmpty()) {
cout << "s1 is empty after five pop() calls." << endl;
} else {
cout << "s1 must be full. Something's wrong!" << endl;
}
// StackEmptyException test
try {
cout << "One more pop when the stack is empty..." << endl;
s1.pop();
}
catch (dStack<int>::StackEmptyException) {
cout << "Exception: cannot pop, stack is empty" << endl;
}
dStack<int> s3;
s3 = s1;
for(int i = 0; i < 5; i++)
s3.push( (i + 1));
}
//end of main.cpp
Output:

in c++ please include all of the following " template class, template function, singly linked list,...
// thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...
My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...
- implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...
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...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
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...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...