// 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 contents:\n"; for (int i = 1; i <= 10; i++) { intStack.push(i); } intStack.dump(); cout << "\nRead contents using top() and pop():\n"; while ( not intStack.empty() ) { cout << intStack.top() << endl; intStack.pop(); } cout << "\nAttempt pop() of empty stack:\n"; try { intStack.pop(); } catch (exception &e) { cout << "Caught exception:\n" << e.what() << endl; } return 0; }
// This is stack.h #ifndef _STACK_H #define _STACK_H #include <iostream> #include <exception> using std::ostream; using std::cout; using std::endl; using std::range_error; // Forward declarations template <class T> class Stack; template <class T> class Node; template <class T> ostream& operator<<(ostream&, const Node<T>&); // Node class for linked list template <class T> class Node { friend Stack<T>; public: Node(T data = T(), Node<T> *next = nullptr) { _data = data; _next = next; } friend ostream& operator<< <T>(ostream &sout, const Node<T> &x); private: T _data; Node *_next; }; // Overloaded insertion operator. Must be overloaded for the template // class T, or this won't work! template <class T> ostream& operator<<(ostream &sout, const Node<T> &x) { sout << "Data: " << x._data; return sout; } // Stack class. Linked-list implementation of a stack. Uses the Node // class. template <class T> class Stack { public: // Constructor Stack(); // Copy constructor, assignment operator, and destructor // DO NOT IMPLEMENT HERE. SEE BELOW. Stack(const Stack &rhs); const Stack& operator=(const Stack& rhs); ~Stack(); // Stack operations: push(), top(), and pop() void push(const T& data); const T& top() const; void pop(); // Helpful functions bool empty() const; // Returns 'true' if stack is empty void dump() const; // Dump contents of the linked list private: Node<T> *_head; // *************************************************** // Any private helper functions must be delared here! // *************************************************** }; template <class T> Stack<T>::Stack() { _head = nullptr; } template <class T> Stack<T>::Stack(const Stack<T>& rhs) { // ******************************** // Implement the copy constructor // ******************************** } template <class T> const Stack<T>& Stack<T>::operator=(const Stack<T>& rhs) { // ********************************** // Implement the assignment operator // ********************************** } template <class T> Stack<T>::~Stack() { // ************************* // Implement the destructor // ************************* } template <class T> void Stack<T>::push(const T& data) { Node<T> *tmpPtr = new Node<T>(data); tmpPtr->_next = _head; _head = tmpPtr; } template <class T> const T& Stack<T>::top() const { if ( empty() ) { throw range_error("Stack<T>::top(): attempt to read empty stack."); } return _head->_data; } template <class T> void Stack<T>::pop() { if ( empty() ) { throw range_error("Stack<T>::pop(): attempt to pop from an empty stack."); } Node<T> *tmpPtr = _head->_next; delete _head; _head = tmpPtr; } template <class T> bool Stack<T>::empty() const { return _head == nullptr; } template <class T> void Stack<T>::dump() const { Node<T> *nodePtr = _head; while ( nodePtr != nullptr ) { cout << nodePtr->_data << endl; nodePtr = nodePtr->_next; } } #endif
// stack.h
#ifndef _STACK_H
#define _STACK_H
#include <iostream>
#include <exception>
using std::ostream;
using std::cout;
using std::endl;
using std::range_error;
// Forward declarations
template <class T> class Stack;
template <class T> class Node;
template <class T> ostream&
operator<<(ostream&, const Node<T>&);
// Node class for linked list
template <class T>
class Node {
friend Stack<T>;
public:
Node(T data = T(), Node<T> *next = nullptr) {
_data = data;
_next = next;
}
friend ostream& operator<< <T>(ostream &sout, const Node<T> &x);
private:
T _data;
Node *_next;
};
// Overloaded insertion operator. Must be overloaded for the
template
// class T, or this won't work!
template <class T>
ostream& operator<<(ostream &sout, const
Node<T> &x) {
sout << "Data: " << x._data;
return sout;
}
// Stack class. Linked-list implementation of a stack. Uses the
Node
// class.
template <class T>
class Stack {
public:
// Constructor
Stack();
// Copy constructor, assignment operator, and destructor
// DO NOT IMPLEMENT HERE. SEE BELOW.
Stack(const Stack &rhs);
const Stack& operator=(const Stack& rhs);
~Stack();
// Stack operations: push(), top(), and pop()
void push(const T& data);
const T& top() const;
void pop();
// Helpful functions
bool empty() const; // Returns 'true' if stack is empty
void dump() const; // Dump contents of the linked list
private:
Node<T> *_head;
// ***************************************************
// Any private helper functions must be delared here!
// ***************************************************
void copyStack(const Stack &rhs); // helper function to copy
the contents of rhs
};
template <class T>
Stack<T>::Stack() {
_head = nullptr;
}
template <class T>
Stack<T>::Stack(const Stack<T>& rhs) {
_head = nullptr; // set head to null
copyStack(rhs); // copy contents of rhs to this
stack
}
template <class T>
const Stack<T>& Stack<T>::operator=(const
Stack<T>& rhs) {
if(this != &rhs) // check for self
assignment
{
// delete the existing contents of
the stack
while(_head != nullptr)
{
Node<T>
*tmp = _head;
_head =
_head->_next;
delete(tmp);
}
copyStack(rhs); // copy the
contents of rhs to this stack
}
return *this;
}
template <class T>
Stack<T>::~Stack() {
// loop to delete the contents of the stack
while(_head != nullptr)
{
Node<T> *tmp = _head;
_head = _head->_next;
delete(tmp);
}
}
// function to copy the contents of rhs
template <class T>
void Stack<T>:: copyStack(const Stack &rhs)
{
Node<T> *node = rhs._head; // set node to point
to head of rhs
Node<T> *tmp = _head; // set tmp to head of this
stack
// loop to copy the nodes of rhs to this
stack
while(node != nullptr)
{
// if empty stack
if(tmp == nullptr)
{
_head = new
Node<T>(node->_data); // create a new node for head
tmp = _head; //
set tmp to head
}
else // if non-empty stack
{
tmp->_next =
new Node<T>(node->_data); // create a new node for node
next to tmp
tmp =
tmp->_next; // make tmp point to the above created node
}
node = node->_next; // point
node to next node of rhs
}
}
template <class T>
void Stack<T>::push(const T& data) {
Node<T> *tmpPtr = new Node<T>(data);
tmpPtr->_next = _head;
_head = tmpPtr;
}
template <class T>
const T& Stack<T>::top() const {
if ( empty() ) {
throw range_error("Stack<T>::top(): attempt to read empty
stack.");
}
return _head->_data;
}
template <class T>
void Stack<T>::pop() {
if ( empty() ) {
throw range_error("Stack<T>::pop(): attempt to pop from an
empty stack.");
}
Node<T> *tmpPtr = _head->_next;
delete _head;
_head = tmpPtr;
}
template <class T>
bool Stack<T>::empty() const {
return _head == nullptr;
}
template <class T>
void Stack<T>::dump() const {
Node<T> *nodePtr = _head;
while ( nodePtr != nullptr ) {
cout << nodePtr->_data << endl;
nodePtr = nodePtr->_next;
}
}
#endif
//end of stack.h
// main.cpp : C++ program to test the Stack class
#include <iostream>
#include "stack.h"
using namespace std;
int main() {
Stack<int> intStack;
cout << "\nPush integers on stack and dump
contents:\n";
for (int i = 1; i <= 10; i++) {
intStack.push(i);
}
intStack.dump();
// test the copy constructor and assignment operator
Stack<int> cStack(intStack), aStack;
cout<<"Stack using copy constructor:"<<endl;
cStack.dump();
cout<<"Stack using assignment
operator:"<<endl;
aStack = cStack;
aStack.dump();
cout << "\nRead contents using top() and pop():\n";
while ( not intStack.empty() ) {
cout << intStack.top() << endl;
intStack.pop();
}
cout << "\nAttempt pop() of empty stack:\n";
try {
intStack.pop();
} catch (exception &e) {
cout << "Caught exception:\n" << e.what() <<
endl;
}
return 0;
}
//end of main.cpp
Output:
// thanks for helping // C++ homework // The homework is to complete below in the...
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...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
- 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 {...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
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 is a template class 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...
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...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
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;...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
// 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...