#ifndef H_StackType #define H_StackType #include #include using namespace std; template struct nodeType {     Type info;     nodeType *link; }; template class linkedStackType: public stackADT class linkedStackType { public:     const linkedStackType& operator=                               (const linkedStackType&);     bool isEmptyStack() const;     bool isFullStack() const;      void initializeStack();       //Function to initialize the stack to an empty state.        void push(const Type& newItem);     Type top() const;       //Function to return the top element of the stack.       //Precondition: The stack exists and is not empty.       //Postcondition: If the stack is empty, the program        //    terminates; otherwise, the top element of       //    the stack is returned.     void pop();     linkedStackType();        //Postcondition: stackTop = NULL;     linkedStackType(const linkedStackType& otherStack);        r     ~linkedStackType();     nodeType *stackTop; //pointer to the stack     void copyStack(const linkedStackType& otherStack);        //Function to make a copy of otherStack.       //Postcondition: A copy of otherStack is created and       //    assigned to this stack. };  template   linkedStackType::linkedStackType() {     stackTop = NULL; } template bool linkedStackType::isEmptyStack() const {     return(stackTop == NULL); } //end isEmptyStack template bool linkedStackType:: isFullStack() const {     return false; } //end isFullStack template void linkedStackType:: initializeStack() {     nodeType *temp; //pointer to delete the node     while (stackTop != NULL)                                {         temp = stackTop;    //set temp to point to the                              //current node         stackTop = stackTop->link;  //advance stackTop to the                                     //next node         delete temp;    //deallocate memory occupied by temp     } } //end initializeStack template void linkedStackType::push(const Type& newElement) {     nodeType *newNode;  //pointer to create the new node     newNode = new nodeType; //create the node     newNode->info = newElement; //store newElement in the node     newNode->link = stackTop; //insert newNode before stackTop     stackTop = newNode;       //set stackTop to point to the                                //top node } //end push template Type linkedStackType::top() const {     assert(stackTop != NULL); //if stack is empty,                               //terminate the program     return stackTop->info;    //return the top element  }/ template void linkedStackType::pop() {     nodeType *temp;   //pointer to deallocate memory     if (stackTop != NULL)     {         temp = stackTop;  //set temp to point to the top node         stackTop = stackTop->link;  //advance stackTop to the                                      //next node         delete temp;    //delete the top node     }     else         cout << "Cannot remove from an empty stack." << endl; } template   void linkedStackType::copyStack                      (const linkedStackType& otherStack) {     nodeType *newNode, *current, *last;     if (stackTop != NULL) //if stack is nonempty, make it empty         initializeStack();     if (otherStack.stackTop == NULL)         stackTop = NULL;     else     {         current = otherStack.stackTop;  //set current to point                                    //to the stack to be copied             //copy the stackTop element of the stack          stackTop = new nodeType;  //create the node         stackTop->info = current->info; //copy the info         stackTop->link = NULL;  //set the link field of the                                 //node to NULL         last = stackTop;        //set last to point to the node         current = current->link;    //set current to point to                                     //the next node             //copy the remaining stack         while (current != NULL)         {             newNode = new nodeType;             newNode->info = current->info;             newNode->link = NULL;             last->link = newNode;             last = newNode;             current = current->link;         }     } }  template    linkedStackType::linkedStackType(                       const linkedStackType& otherStack) {     stackTop = NULL;     copyStack(otherStack); } template   linkedStackType::~linkedStackType() {     initializeStack(); }//end destructor template    const linkedStackType& linkedStackType::operator=(const linkedStackType& otherStack) {      if (this != &otherStack) //avoid self-copy         copyStack(otherStack);     return *this;  }//end operator= #endif

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 


#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
template <class Type>
class linkedStackType: public stackADT<Type>
class linkedStackType
{
public:
    const linkedStackType<Type>& operator=
                              (const linkedStackType<Type>&);
    bool isEmptyStack() const;
    bool isFullStack() const; 
    void initializeStack();
      //Function to initialize the stack to an empty state. 
      void push(const Type& newItem);
    Type top() const;
      //Function to return the top element of the stack.
      //Precondition: The stack exists and is not empty.
      //Postcondition: If the stack is empty, the program 
      //    terminates; otherwise, the top element of
      //    the stack is returned.
    void pop();
    linkedStackType(); 
      //Postcondition: stackTop = NULL;
    linkedStackType(const linkedStackType<Type>& otherStack); 
      r
    ~linkedStackType();
    nodeType<Type> *stackTop; //pointer to the stack
    void copyStack(const linkedStackType<Type>& otherStack); 
      //Function to make a copy of otherStack.
      //Postcondition: A copy of otherStack is created and
      //    assigned to this stack.
}; 
template <class Type> 
linkedStackType<Type>::linkedStackType()
{
    stackTop = NULL;
}
template <class Type>
bool linkedStackType<Type>::isEmptyStack() const
{
    return(stackTop == NULL);
} //end isEmptyStack
template <class Type>
bool linkedStackType<Type>:: isFullStack() const
{
    return false;
} //end isFullStack
template <class Type>
void linkedStackType<Type>:: initializeStack()
{
    nodeType<Type> *temp; //pointer to delete the node

    while (stackTop != NULL)                           
    {
        temp = stackTop;    //set temp to point to the 
                            //current node
        stackTop = stackTop->link;  //advance stackTop to the
                                    //next node
        delete temp;    //deallocate memory occupied by temp
    }
} //end initializeStack
template <class Type>
void linkedStackType<Type>::push(const Type& newElement)
{
    nodeType<Type> *newNode;  //pointer to create the new node

    newNode = new nodeType<Type>; //create the node

    newNode->info = newElement; //store newElement in the node
    newNode->link = stackTop; //insert newNode before stackTop
    stackTop = newNode;       //set stackTop to point to the 
                              //top node
} //end push
template <class Type>
Type linkedStackType<Type>::top() const
{
    assert(stackTop != NULL); //if stack is empty,
                              //terminate the program
    return stackTop->info;    //return the top element 
}/
template <class Type>
void linkedStackType<Type>::pop()
{
    nodeType<Type> *temp;   //pointer to deallocate memory

    if (stackTop != NULL)
    {
        temp = stackTop;  //set temp to point to the top node

        stackTop = stackTop->link;  //advance stackTop to the 
                                    //next node
        delete temp;    //delete the top node
    }
    else
        cout << "Cannot remove from an empty stack." << endl;
}
template <class Type> 
void linkedStackType<Type>::copyStack
                     (const linkedStackType<Type>& otherStack)
{
    nodeType<Type> *newNode, *current, *last;

    if (stackTop != NULL) //if stack is nonempty, make it empty
        initializeStack();
    if (otherStack.stackTop == NULL)
        stackTop = NULL;
    else
    {
        current = otherStack.stackTop;  //set current to point
                                   //to the stack to be copied

            //copy the stackTop element of the stack 
        stackTop = new nodeType<Type>;  //create the node

        stackTop->info = current->info; //copy the info
        stackTop->link = NULL;  //set the link field of the
                                //node to NULL
        last = stackTop;        //set last to point to the node
        current = current->link;    //set current to point to
                                    //the next node

            //copy the remaining stack
        while (current != NULL)
        {
            newNode = new nodeType<Type>;

            newNode->info = current->info;
            newNode->link = NULL;
            last->link = newNode;
            last = newNode;
            current = current->link;
        }
    }

template <class Type>   
linkedStackType<Type>::linkedStackType(
                      const linkedStackType<Type>& otherStack)
{
    stackTop = NULL;
    copyStack(otherStack);
}
template <class Type> 
linkedStackType<Type>::~linkedStackType()
{
    initializeStack();
}//end destructor
template <class Type>   
const linkedStackType<Type>& linkedStackType<Type>::operator=(const linkedStackType<Type>& otherStack)

    if (this != &otherStack) //avoid self-copy
        copyStack(otherStack);
    return *this; 
}//end operator=
#endif

 

(A) When you create two object stacks of the same type, they are considered equal if they satisfy two conditions: they have
the same length of elements, and their corresponding positions are matched.
The relational overload operator == returns true if two object stacks are the same, false otherwise. Write a definition
of this operator into a function template using the following function declaration. Also, add this operator function to
the header file linkedStack.
bool operator==(const linkedStackType<Type>& otherStack) const;
(B) Write a function definition about reverseStack. This function copies elements of a stack in reverse order onto another
stack. Add the following function declaration to the class file (linkedStack.) and complete its function definition.
void
reverseStack (linkedStackType<Type> &otherStack)
Example: Consider the following statements when creating stack objects:
linkedStackType stack1;
linkedStackType stack2;
The statement stack1.reverseStack (stack2); copies the elements of stackl onto stack2 in reverse order. That
is, the top element of stackl is moved to the bottom of stack2, and so on. The old contents of stack2 are destroyed and
stackl is unchanged.
(C) Write a test program to satisfy the following requirements (see the output screenshot)
(a) Create two default stack objects, stackl and stack2 (object type must be integer).
(b) Prompt the user's to add inputs into the stackl providing an appropriate ending statement (use -999' to end a
user's input). The -999 does not count as an element of the stackl.
(c) Copy stackl to stack2 using = assign operator
(d) Print a statement indicating whether the stacks are the same or not.
(e) Copy the elements of stackl onto stack2 in reverse order using the reverse function (reverStack () ). Then, print
a statement indicating whether the stacks are the same or not.
(f) Print all elements of the stackl and the stack2.
Transcribed Image Text:(A) When you create two object stacks of the same type, they are considered equal if they satisfy two conditions: they have the same length of elements, and their corresponding positions are matched. The relational overload operator == returns true if two object stacks are the same, false otherwise. Write a definition of this operator into a function template using the following function declaration. Also, add this operator function to the header file linkedStack. bool operator==(const linkedStackType<Type>& otherStack) const; (B) Write a function definition about reverseStack. This function copies elements of a stack in reverse order onto another stack. Add the following function declaration to the class file (linkedStack.) and complete its function definition. void reverseStack (linkedStackType<Type> &otherStack) Example: Consider the following statements when creating stack objects: linkedStackType stack1; linkedStackType stack2; The statement stack1.reverseStack (stack2); copies the elements of stackl onto stack2 in reverse order. That is, the top element of stackl is moved to the bottom of stack2, and so on. The old contents of stack2 are destroyed and stackl is unchanged. (C) Write a test program to satisfy the following requirements (see the output screenshot) (a) Create two default stack objects, stackl and stack2 (object type must be integer). (b) Prompt the user's to add inputs into the stackl providing an appropriate ending statement (use -999' to end a user's input). The -999 does not count as an element of the stackl. (c) Copy stackl to stack2 using = assign operator (d) Print a statement indicating whether the stacks are the same or not. (e) Copy the elements of stackl onto stack2 in reverse order using the reverse function (reverStack () ). Then, print a statement indicating whether the stacks are the same or not. (f) Print all elements of the stackl and the stack2.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education