Question

in c++ please include all of the following " template class, template function, singly linked list,...

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

  1. is a template class
  2. has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being deleted.

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

  1. Each member function of the Stack class must be clearly tested for each data type, int and Student (16 tests = 8 member functions * 2 data types)
  2. From the output, the instructor must be able to clearly see what test is being done and how it is done.
  3. Do not interact with the user to receive inputs so that program testing time can be reduced.
  4. When you fill the stack with many keys or elements, use a loop for efficiency.
  5. Provide a makefile and remove unused functions and files.

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

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

// 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:

Add a comment
Know the answer?
Add Answer to:
in c++ please include all of the following " template class, template function, singly linked list,...
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
  • // thanks for helping // C++ homework // The homework is to complete below in the...

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

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

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

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

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

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

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

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