为什么我在这段代码中出现运行时错误?它有什么问题?我是链表的新手我无法理解为什么以下代码会给出运行时错误代码有什么问题?我正在尝试动态实现堆栈。请帮忙!
#include <iostream>
using namespace std;
struct stack
{
int a ;
struct stack *n;
};
typedef struct stack st;
st *top = new st;
// 插入元素
void push(int a)
{
st *p;
p = new st;
p->n = top;
top = p;
}
//提取元素
int pop()
{
st *p;
p=top;
if(top == NULL)
{
cout<<"stack is already empty";
}
else
{
top = top->n;
return p->a;
delete p;
}
}
//DISPLAY FUNCTION
void display()
{
st *p;
p=top;
while(p!=NULL)
{
cout<<p->a;
p=p->n;
}
cout<<p->a;
}
int main()
{
push(1);
push(2);
push(3);
push(4);
push(5);
display();
int s=pop();
cout<<s;
display();
return 0;
}