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

Template Stack Class: No Data Appears in Stack

本文关键字:堆栈 显示 数据      更新时间:2023-10-16

pop()函数是具有返回类型错误的函数。据说该函数返回堆栈对象,但数据位于节点结构中。我该如何返回适当的数据?

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

添加newnode-> mdata =数据并通过调试检查检查看起来好像在堆栈中。看来显示功能可能是问题。

我也不确定我的推动和ISEXIST FUNCITON是否正确...

编辑:pop pusp and 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 Stackn";
   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 Stackn";
   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

您错过了推动数据的步骤!

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,会发生什么?