Question

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 41 36

s3 (size 0):
s4 (size 4): 0 1477251200 1477251168 1477251136

Testing assignment to self

s3 (size 4): 1477251104 1477251200 1477251168 1477251136
s4 (size 0):

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Output I need

Testing default constructor

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): 4 6 2 17 

s3 (size 4): 28 75 41 36 

Testing assignment operator

s3 (size 4): 28 75 41 36 
s4 (size 4): 28 75 41 36 

s3 (size 0): 
s4 (size 4): 28 75 41 36 

Testing assignment to self

s3 (size 4): 28 75 41 36 
s4 (size 0): 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My Code

#ifndef STACK_H
#define STACK_H

#include <iostream>

template <class T>
struct Node
{
T data;
Node<T>* next;

Node(const T& = T(), Node<T>* next = NULL);
};

template <class T>
Node<T>::Node(const T& data, Node<T>* next)
{
this->next = next;
this->data = data;
}

template <class T>
class Stack;

template <class T>
std::ostream& operator<<(std::ostream&, const Stack<T>&);

template <class T>
class Stack
{
friend std::ostream& operator<< <>(std::ostream&, const Stack<T>&);

private:

Node<T>* stkTop;
size_t stackSize;

public:
Stack();
~Stack();

Stack(const Stack<T>&);
Stack<T> & operator =(const Stack &);

void clear();
size_t size();
bool empty();

const T& top()const;
void push(const T&);
void pop();
void copyList(const Stack<T> &);
};
#endif

template <class T>
Stack<T>::Stack()
{
stkTop = NULL;
stackSize = 0;
}

template <class T>
Stack<T>::~Stack()
{
clear();
}

template <class T>
void Stack<T>::clear()
{
while(stackSize != 0)
{
pop();
}
}

template<class T>
void Stack<T>::pop()
{
Node<T>* delNode = stkTop;
stkTop = stkTop->next;

delete delNode;
stackSize--;
}

template <class T>
Stack<T> & Stack<T>::operator = (const Stack<T> & other)
{
if(this != &other)
{
clear();
stkTop = other.stkTop;
stackSize = other.stackSize;
}

return *this;
}

template <class T>
std::ostream& operator<<(std::ostream &leftObj, const Stack<T> &rightObj)
{
Node<T>* ptr;
for (ptr = rightObj.stkTop; ptr != NULL; ptr = ptr->next)
{
leftObj << ptr->data << ' ';
}

return leftObj;
}

template <class T>
size_t Stack<T>::size()
{
return stackSize;
}

template <class T>
bool Stack<T>::empty()
{
if(stackSize == 0)
{
return true;
}
else
{
return false;
}
}

template <class T>
const T& Stack<T>::top()const
{
return stkTop->data;
}

template <class T>
void Stack<T>::push(const T &other)
{
Node<T>* newNode = new Node<T>(other, stkTop);
stkTop = newNode;
stackSize++;
}

template <class T>
Stack<T>::Stack(const Stack<T> &other)
{
stkTop = NULL;
stkTop = other.stkTop;
stackSize = other.stackSize;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TEST FILE

#include "Stack.h"
#include <iostream>

using std::cout;
using std::endl;

int main()
{
cout << "Testing default constructor\n\n";

Stack<int> s1;

cout << "s1 (size " << s1.size() << "): " << s1 << endl;
cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
cout << endl;

cout << "Testing push()\n\n";

s1.push(17);

cout << "s1 (size " << s1.size() << "): " << s1 << endl;
cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
cout << endl;

s1.push(2);
s1.push(6);
s1.push(4);

cout << "s1 (size " << s1.size() << "): " << s1 << endl;
cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
cout << endl;


cout << "Testing copy constructor\n\n";
Stack<int> s2(s1);

cout << "s1 (size " << s1.size() << "): " << s1 << endl;
cout << "s2 (size " << s2.size() << "): " << s2 << endl << endl;

cout << "Testing clear()\n\n";
s1.clear();

cout << "s1 (size " << s1.size() << "): " << s1 << endl;
cout << "s2 (size " << s2.size() << "): " << s2 << endl << endl;

Stack<int> s3;

s3.push(36);
s3.push(41);
s3.push(75);
s3.push(28);

cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;

cout << "Testing assignment operator\n\n";

Stack<int> s4;

s4 = s3;

cout << "s3 (size " << s3.size() << "): " << s3 << endl;
cout << "s4 (size " << s4.size() << "): " << s4 << endl << endl;

s3.clear();

cout << "s3 (size " << s3.size() << "): " << s3 << endl;
cout << "s4 (size " << s4.size() << "): " << s4 << endl << endl;

cout << "Testing assignment to self\n\n";

s4 = s4;
s3 = s4;

s4.clear();

cout << "s3 (size " << s3.size() << "): " << s3 << endl;
cout << "s4 (size " << s4.size() << "): " << s4 << endl << endl;

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

#ifndef STACK_H
#define STACK_H

#include <iostream>

template <class T> struct Node
{
   T data;
   Node<T>* next;

   Node(const T& = T(), Node<T>* next = NULL);
};

template <class T>Node<T>::Node(const T& data, Node<T>* next)
{
   this->next = next;
   this->data = data;
}

template <class T> class Stack;

template <class T>std::ostream& operator<<(std::ostream&, const Stack<T>&);

template <class T>class Stack
{
   friend std::ostream& operator<< <>(std::ostream&, const Stack<T>&);

   private:

   Node<T>* stkTop;
   size_t stackSize;

Highlighted lines of codes are added In stack.h: #ifndef STACK-H #define STACK-H #include <iostream> template lass T> struct Node T data; Node<T> next; Nodelconst T& T(), Node<T> next = NULL); template <class T>NodecT>:: Node(const T& data, Node<T> next this-next next; this->data data template <class T class Stack; template <class T std:ostream&operator<<(std:ostream&, const Stack<T>&); template class Teclass Stack friend std: ostream& operator<<(std: ostream&, const Stack<T>&) private: NodeT stkTop; size_t stackSize; public: Stack0: Stack0: Stack(const Stack<T-& Stack<T> & operator =(const Stack &); void clear0; size_t size0: bool empty0 const T& top0const; void push(const T&) void pop0: void copyList(const Stack<T &); #end if template <class T Stack<T: Stack) stkTop NULL; stackSize O;

Add a comment
Know the answer?
Add Answer to:
My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...
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...

  • C++ Create a static implementation of a set. Add the efficiency of each function to the...

    C++ Create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Use test_set.cpp as your test program. Header: /************************************ This class models a mathematical set. */ #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; set(); // postcondition: empty set has been created void insert (const value_type& entry); // precondition: if entry is not in...

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

  • (C++) Two stacks of the same type are the same if they have the same number...

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

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

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        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, false otherwise. Also, write the definition...

  • Write a program that opens a text file called input.txt and reads its contents into a...

    Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length. Input.txt This is a file is for test output.txt tset rof si elif...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

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

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