链表c++取栈顶元素

为什么这个链栈的栈顶元素取出来是一段数字?

#include
using namespace std;
template<class A>
struct stack
{
    A date;
    stack* next;
};
template<class A>
class stacklist
{
protected:stack* first = new stack;
public:
    stacklist() { first->next = NULL; }
    void in(A x);
    bool get(A&x);
    bool de();
    void out();
};
template<class A>
void stacklist::in(A x)
{
    stack* current = new stack;
    current->date = x;
    current->next = first->next;
    first->next = current;
}
template<class A>
bool stacklist::get(A &x)
{
    
    if (first->next = NULL)return false;
    if (first->next != NULL)
    {
        stack* current = new stack;
        x = first->next->date;    
        return true;
    }
}
template<class A>
bool stacklist::de()
{
    if (first->next == NULL)return false;
    else first->next = first->next->next;
    return true;
}
template<class A>
void stacklist::out()
{
    stack* prt = first->next;
    while (prt != NULL) {
        cout << prt->date << "  ";
        prt = prt->next;
    }
}
void main()
{
    stacklist<int>s, w, t;
    int i;
    for (i = 1;i <= 5;i++)
    {
        s.in(i);
    }
    s.out();cout << endl;
    int x;
    for (i = 1;i <= 5;i++)
    {
        s.get(x);
        w.in(x);
        s.de();
    }
    w.out();cout << endl;
    for (i = 1;i <= 5;i++)
    {
        w.get(x);t.in(x);
        w.de();
    }
    t.out();
}

img

因为你的get函数中,第一个if语句中,应该是==,你写成了=
如下:

img

if (first->next == NULL)return false;

那个这里我写的是通过一个链表来使栈s中的元素按照原序放到栈T。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632