我正在尝试使用cpp中的链表实现堆栈。我正在使用类制作链接列表,但出现以下错误,我不明白为什么
#include <bits/stdc++.h>
using namespace std;
class Stack{
public:
int top;
Stack* next;
Stack(int data){ //constructor
top = data;
next = NULL;
}
void push(Stack* &head, int data);
int pop(Stack* &head);
bool empty(Stack* &head);
};
void Stack::push(Stack* &head, int data){
Stack* s = new Stack(data);
s->next = head;
head = s;
}
int pop(Stack* &head){
int x = head->top;
head = head->next;
return x;
}
bool empty(Stack* &head){
if(head == NULL)
return true;
return false;
}
int main() {
class Stack* s = new Stack(1);
s.push(2);
s.push(3);
cout << s.empty(s) << endl;
cout << s.pop(s) << endl;
cout << s.pop(s) << endl;
cout << s.pop(s) << endl;
cout << s.empty(s) << endl;
}
错误:请求's'中的成员'push',这是指针类型'Stack *'(也许您打算使用'->'?)]
请求's'中的成员'empty',其指针类型为'Stack *'(也许您打算使用'->'?)]
请求's'中的成员'pop',其指针类型为'Stack *'(也许您打算使用'->'?)]
当我使用->而不是时。它表示对Stack :: empty(Stack *&)的未定义引用和对Stack :: pop(Stack *&)]的未定义引用
我正在尝试使用cpp中的链表实现堆栈。我正在使用类制作链接列表,但出现以下错误,并且我不理解为什么#include
您“偶然”声明了一个指针。像这样更改您的代码
如错误消息所述,当您有Stack*
时,您需要使用->
,而不是.
,如下所示: