Reputation: 321
The pop() function is the one with the return type error. The function is says it returns a Stack object but the data is in a Node struct. How do I make it return the proper data?
According to my display function it looks like no data gets in the stack.
Adding newNode->mData = data and checking via debugging it looks like things are getting in the stack. It appears the display function may be the problem.
I'm also unsure if my push and isExist funcitons are right...
/* Pre: The stack is initialized and searchKey is available
* Post: The function retuns true if the searchKey exists in the queue;
* otherwise return false
* Purpose: To determine if a given value exists in the stack or not
*************************************************************************/
template <class T>
bool Stack<T>::isExist(T searchKey)
{
bool exist = false;
Node<T> *temp = mHead;
if (mHead == NULL)
{
exist = false;
}
else if (temp->mData == searchKey)
{
exist = true;
}
else
{
while (temp->mNext != NULL)
{
if (temp->mData == searchKey)
{
exist = true;
break;
}
temp = temp->mNext;
}
}
return exist;
}
/* Pre: The stack is initialized
* Post: The first node in the stack is removed, and its content is
* returned. If the stack is empty return the default value
* for the given data type
* Purpose: To remove the first node in the stack
*************************************************************************/
template <class T>
T Stack<T>::pop()
{
//Temp holds the mem location of the data
//that will be popped/returned
Node<T> *temp = mHead;
if (mHead == NULL)
{
return NULL;
cout << "The stack is empty!";
}
//Makes the next node in the stack the top one
else if (mHead->mNext == NULL )
{
mHead = NULL;
mTail = NULL;
}
else
{
mHead = mHead->mNext;
}
//Increment the counter down for the popped node
mCount--;
return temp->mData;
}
/* Pre: The stack is initialized
* Post: The new node is added at the beginning of the stack.
* Duplication is allowed
* Purpose: To add a new node at the beginning of the stack
*************************************************************************/
template <class T>
void Stack<T>::push(T data)
{
Node<T> *newNode = new Node<T>;
newNode->mData = data;
//If the stack is empty
if (mHead == NULL && mTail == NULL)
{
mHead = newNode;
mTail = newNode;
}
//Otherwise mHead will be the new node's next node
//The new node becomes the head
else
{
newNode->mNext = mHead;
mHead = newNode;
}
//Increment the counter to reflect the new node
mCount++;
}
template <class T>
void Stack<T>::display()
{
Node<T> *tmp;
if (isEmpty())
cout << "Empty Stack\n";
else
{
tmp = mHead;
while (tmp != NULL)
{
cout << tmp->mData << " ";
tmp = tmp->mNext;
}
cout << endl;
}
}
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
template <class T>
class Stack {
private:
template <class T>
struct Node
{
T mData;
Node<T> *mNext;
/* Pre: None
* Post: This object is initialized using default values
* Purpose: To intialize date object
*************************************************************************/
Node()
{
mData = T();
mNext = NULL;
}
/* Pre: None
* Post: This object is initialized using specified data
* Purpose: To intialize date object
*************************************************************************/
Node(T data)
{
mData = data;
mNext = NULL;
}
};
Node<T> *mHead, *mTail;
int mCount;
public:
Stack();
~Stack();
int getCount();
void clear();
void display();
bool isEmpty();
bool isExist(T searchKey);
T pop();
void push(T data);
};
template <class T>
Stack<T>::Stack()
{
mHead = NULL;
mTail = NULL;
mCount = 0;
}
template <class T>
Stack<T>::~Stack()
{
while (!isEmpty())
pop();
}
template <class T>
int Stack<T>::getCount()
{
return mCount;
}
template <class T>
void Stack<T>::clear()
{
while (!isEmpty())
pop();
}
template <class T>
void Stack<T>::display()
{
Node<T> *tmp;
if (isEmpty())
cout << "Empty Stack\n";
else
{
tmp = mHead;
while (tmp != NULL)
{
cout << tmp->mData << " ";
tmp = tmp->mNext;
}
cout << endl;
}
}
template <class T>
bool Stack<T>::isEmpty()
{
return mCount == 0;
}
#endif
Upvotes: 0
Views: 107
Reputation: 41145
You're missing a step in your push to set the data!
template <class T>
void Stack<T>::push(T data)
{
Node<T> *newNode = new Node<T>;
//HERE! vv
newNode->mData = data;
//THERE! ^^
//If the stack is empty
if (mCount == 0)
{
mHead = newNode;
mTail = newNode;
}
//Otherwise mHead will be the new node's next node
//The new node becomes the head
else
{
newNode->mNext = mHead;
mHead = newNode;
}
//Increment the counter to reflect the new node
mCount++;
}
(User fixed strikethrough text in an edit)
Other things to think about: What is this mTail
variable, and what should be happening to it?
Look at your isExist
function... Ask yourself what happens if you push a number and then ask if that number exists? Will it work as intended? What happens if you try to pop from an empty stack? What happens if you call isExist on an empty stack?
Upvotes: 1