模板堆栈类:堆栈中没有数据

时间:2014-01-29 21:38:33

标签: c++ templates stack push pop

pop()函数是返回类型错误的函数。该函数表示它返回一个Stack对象,但数据在Node结构中。如何让它返回正确的数据?

根据我的显示功能,看起来没有数据进入堆栈。

添加newNode-> mData =数据并通过调试检查它看起来像堆栈中的东西。看来显示功能可能是问题。

我也不确定我的推送和isExist功能是否正确......

已编辑:pop push和isExist

/*      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;
   }
}

Stack类的其余部分

    #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

1 个答案:

答案 0 :(得分:1)

你错过了设置数据的步骤!

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++;
}

(用户在编辑中修正了删除线文字)

需要考虑的其他事项:这个mTail变量是什么,应该发生什么?

查看您的isExist功能...问问自己,如果您推送一个号码然后询问该号码是否存在会发生什么?它会按预期工作吗?如果你尝试从空堆栈中弹出会发生什么?如果在空堆栈上调用isExist会发生什么?