模板堆栈类:堆栈中没有显示数据
Template Stack Class: No Data Appears in Stack
添加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,会发生什么?
相关文章:
- 提升堆栈跟踪不显示函数名称和行号
- WinDbg 不显示某些小型转储文件的完整堆栈跟踪
- 如何使用方法覆盖在输出屏幕上显示堆栈整数值
- 调用堆栈显示 SIGBUS,这意味着什么
- 如何正确显示此堆栈的内容?
- 打开C++故障转储不会在调用堆栈中显示正确的行
- 如何在 c++ 中显示(或循环)堆栈
- 哪些更好的方法可以显示使用C STL创建的堆栈而不弹出每个元素的方法
- 堆栈在函数中弹出仍然显示在主函数中。调用不应该通过引用给定的向量吗
- GDB 显示奇怪的堆栈跟踪
- 我如何修改我的C 程序以显示用户输入的单词,并使用堆栈向后
- 模板堆栈类:堆栈中没有显示数据
- 本地变量的地址不在smaps显示的堆栈地址范围内
- 在C++中显示堆栈元素时的无限循环
- Visual Studio 2008 在堆栈溢出后显示完整的堆栈跟踪
- 可视化泄漏检测器(VLD)显示空的调用堆栈
- 堆芯转储的原因是什么?来自oraclelib的堆栈显示
- gdb回溯没有显示main()的完整堆栈跟踪
- 无法显示堆栈或推送
- 如何显示动态堆栈和队列(c++)中的所有元素