在我自己的堆栈中实现top的问题
Problems in implementing top in my own stack
我正在尝试实现我自己的通用堆栈
实现返回堆栈的最后一个元素的Top Function时出现了一些问题
但是当head==NULL时,就没有什么可返回的了
#include <iostream>
#include <string>
using namespace std;
template <class anyType>
class Stack
{
private: struct node
{
anyType data;
node* nextAddress;
};
private: node* head;
public: Stack()
{
head = NULL;
}
public: anyType Top()
{
if (head == NULL)
{
cout<<"Stack is empty"<<endl;
return NULL;
}
node* travel = head;
while(travel -> nextAddress != NULL)
{
travel = travel -> nextAddress;
}
return travel -> data;
}
};
struct Student
{
int id;
string name;
};
int main(int argc, char const *argv[])
{
Stack <Student> obj;
return 0;
}
所以,当我返回NULL时,就会出现问题,因为NULL!=anyType
有人能帮我怎么解决这个问题吗
如果您的函数被定义为返回一个实际对象(而不是指向对象的指针(,并且您需要指示这样的对象不存在,那么最好的方法可能只是抛出一个异常。
换句话说,类似于:
if (head == nullptr) {
throw std::runtime_error("stack is empty, no top");
}
然后,在调用它的代码中,你只需要考虑到这一点:
try {
processHeadSomehow(stack.Top());
} catch (std::runtime_error &e) {
std::cout << "No top element, couldn't processn";
}
关于更有用的示例,请参阅下面的代码。它是通用堆栈的基本操作,包括push
、pop
、top
、clear
(以及用于调试的dump
(。请注意,用于遍历列表到末尾的代码已被删除。这是因为我们把项目推到链表的开头,所以我们需要把它们取下(弹出(或查看(顶部(。
#include <iostream>
#include <string>
#include <exception>
using std::cout; using std::string; using std::runtime_error;
template <class AnyType> class Stack {
struct Node { AnyType data; Node *next; };
public:
Stack() { m_head = nullptr; }
~Stack() { Clear(); }
void Dump() {
cout << "n";
auto curr = m_head;
auto count = 0;
while (curr != nullptr) {
cout << "Item #" << ++count << " is " << curr->data << "n";
curr = curr->next;
}
cout << "-- Number of items was " << count << "n";
}
void Clear() {
auto curr = m_head;
m_head = nullptr;
while (curr != nullptr) {
Node *toFree = curr;
curr = curr->next;
delete toFree;
}
}
void Push(AnyType item) {
auto newItem = new Node;
newItem->data = item;
if (m_head == nullptr) {
newItem->next = nullptr;
m_head = newItem;
} else {
newItem->next = m_head;
m_head = newItem;
}
}
AnyType Pop() {
if (m_head == nullptr)
throw runtime_error("Stack empty");
Node *toFree = m_head;
m_head = m_head->next;
AnyType retVal = toFree->data;
delete toFree;
return retVal;
}
AnyType Top() {
if (m_head == nullptr)
throw runtime_error("Stack empty");
return m_head->data;
}
private:
Node *m_head;
};
int main(int argc, char const *argv[])
{
Stack<string> stack;
for (const string str: {"", "Pax", "Imran", "Joel"}) {
if (! str.empty())
stack.Push(str);
stack.Dump();
try {
cout << "Top of stack is " << stack.Top() << "n";
} catch (runtime_error &e) {
cout << "Exception getting top: " << e.what() << "n";
}
}
try {
auto popped = stack.Pop();
stack.Dump();
cout << "This is after we popped " << popped << "n";
cout << "Top of stack is " << stack.Top() << "n";
} catch (runtime_error &e) {
cout << "Exception getting top: " << e.what() << "n";
}
return 0;
}
当用测试main
运行它时,我们看到了预期的行为:
-- Number of items was 0
Exception getting top: Stack empty
Item #1 is Pax
-- Number of items was 1
Top of stack is Pax
Item #1 is Imran
Item #2 is Pax
-- Number of items was 2
Top of stack is Imran
Item #1 is Joel
Item #2 is Imran
Item #3 is Pax
-- Number of items was 3
Top of stack is Joel
Item #1 is Imran
Item #2 is Pax
-- Number of items was 2
This is after we popped Joel
Top of stack is Imran
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- 在我自己的堆栈中实现top的问题
- stl 堆栈和 top() 函数的问题