【发布时间】:2016-11-18 16:15:04
【问题描述】:
我已经编写了一个将节点推入堆栈的代码,并且我已经使用单链表实现了它。但是每当我运行它时,它都会显示运行时错误。请帮帮我。
#include <iostream>
#include <string>
using namespace std;
struct node{
int key;
node *next;
}*head=NULL;
void push(node *n){
n->next=head->next;
head->key=n->key;
head->next=n;
cout<<head->key<<" ";
}
int main(){
node *x;
cin>>x->key;
push(x);
return 0;
}
我正在使用 C++ 4.9.2 (GCC-4.9.2) 请帮助我找出我哪里出错了
【问题讨论】:
-
UB,x 不指向任何东西。
标签: c++ pointers linked-list stack