Professional Documents
Culture Documents
cpp
#include <iostream>
#include "linkedStack.h"
int main()
{
// create an empty stack to store characters
linkedStackType<char> myStack;
// push S to myStack2
myStack2.push('S');
return 0;
}
-----------------------------------------------------------------------------------
----------------------
linkedStack.h
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
#include "stackADT.h"
void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: The stack elements are removed;
// stackTop = nullptr;
void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
linkedStackType();
//Default constructor
//Postcondition: stackTop = nullptr;
~linkedStackType();
//Destructor
//Postcondition: All the elements of the stack are
// removed from the stack.
bool operator==(const linkedStackType<Type>& otherStack) const;
// operator ==
// Function that returns true if they have the same number of elements
// and their elements at the corresponding positions are the same.
private:
nodeType<Type> *stackTop; //pointer to the stack
//Default constructor
template <class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = nullptr;
}
if (stackTop != nullptr)
{
temp = stackTop; //set temp to point to the top node
if (otherStack.stackTop == nullptr)
stackTop = nullptr;
else
{
current = otherStack.stackTop; //set current to point
//to the stack to be copied
//copy constructor
template <class Type>
linkedStackType<Type>::linkedStackType(
const linkedStackType<Type>& otherStack)
{
stackTop = nullptr;
copyStack(otherStack);
}//end copy constructor
//destructor
template <class Type>
linkedStackType<Type>::~linkedStackType()
{
initializeStack();
}//end destructor
return *this;
}//end operator=
// overloaded equality operator that returns true if the 2 stacks have the same
number of elements
// and elements in corresponding positions are equal, else return false
template <class Type>
bool linkedStackType<Type>::operator==(const linkedStackType<Type>& otherStack)
const{
// set curr to top of this stack and otherCurr to top of other stack
nodeType<Type>* curr = stackTop;
nodeType<Type>* otherCurr = otherStack.stackTop;
// after the loop if curr or otherCurr have not reached the end, then number of
elements in both stack are not equal, hence return false
if(curr != nullptr || otherCurr != nullptr)
return false;
#endif
// end of linkedStack.h