gpt4 book ai didi

c++ - 在我自己的堆栈中实现 top 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:36 26 4
gpt4 key购买 nike

我正在尝试实现我自己的通用堆栈

实现返回堆栈最后一个元素的 Top 函数有一些问题

但是当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 process\n";
}

关于更实用的示例,请参见下面的代码。它是通用堆栈的基本操作,包括pushpoptopclear(和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

关于c++ - 在我自己的堆栈中实现 top 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51626891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com