【发布时间】:2013-10-14 07:09:25
【问题描述】:
这是我的功能,
template <class KeyType >
KeyType * Stack<KeyType>::Pop(KeyType& x) {
if (IsEmpty()) { //isempty is just a bool function
StackEmpty(); //just prints out that stack is empty
return 0; //bad coding breaking out of the function
}
x = stack[top--]; //stack is a pointer to an array, top is the top of the stack
return &x;
}
我的问题是: 我不确定如何在 main 中调用它。据我了解,pop 函数不应该真的可以选择从堆栈中弹出什么。后进先出对吗?主要问题是 Keytype& x 参数到底是什么,你将如何在 main 中调用它? (在这种情况下,KeyType 被初始化为 KeyType *stack an int 在这个特定的程序中)。
【问题讨论】:
-
实际上 Stack 的 Pop 方法不应该接受任何参数,因为它总是返回堆栈中最顶部的元素,因为它是一个后进先出数据结构。我认为您的 Pop 方法将返回指向 Stack 顶部元素的指针以及值本身;第一个作为返回值,后者通过引用传递。
-
它实际上不是一个堆栈,它是一个使用类的堆栈实现,可能应该把它放在那里的某个地方,但这是在一个教师给我们的头文件中,所以我假设它是可行的.我一直在争论是否只删除参数哈哈。