Hi. I am having trouble understanding and working with this ADT Stack implementation. For instance, how can you examine the top element in the stack when the function to do so accepts an argument? Same for the function to pop. It takes an argument. As you can see, about all I could do was push! Any ideas? Thanks, Steve
Code:#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> *next;
};
template<class T>
class Stack
{
private:
int count;
Node <T> *top;
public:
Stack();
~Stack();
bool pushStack(T dataIn);
bool popStack(T& dataOut);
bool stackTop(T& dataOut);
bool emptyStack();
bool fullStack();
int stackCount();
};
template<class T>
Stack<T>::Stack()
{
top=NULL;
count=0;
}
template<class T>
bool Stack<T>::pushStack(T dataIn)
{
bool success;
Node<T> *newPtr;
if(!(newPtr = new Node<T>))
success=false;
else
{
newPtr->data=dataIn;
newPtr->next=top;
top=newPtr;
count++;
success=true;
}
return success;
}
template<class T>
bool Stack<T>::popStack(T& dataOut)
{
Node<T> *dltPtr;
bool success;
if(count==0)
success=false;
else
{
dltPtr=top;
dataOut=top->data;
top=top->next;
count--;
success=true;
}
return success;
}
template<class T>
bool Stack<T>::stackTop(T& dataOut)
{
bool success;
if(count==0)
success=false;
else
{
dataOut=top->data;
success=true;
}
return success;
}
template<class T>
bool Stack<T>::emptyStack()
{
return (count==0);
}
template<class T>
bool Stack<T>::fullStack()
{
Node<T> *temp;
temp=new Node<T>;
if(temp != NULL)
{
delete temp;
return false;
}
return true;
}
template<class T>
Stack<T>::~Stack()
{
Node<T> *temp;
while(top != NULL)
{
temp=top;
top=top->next;
delete temp;
}
}
int main()
{
Stack<double> s;
s.pushStack(45.33);
return 0;
}