【发布时间】:2015-04-07 18:27:23
【问题描述】:
我正在制作一个使用动态堆栈将十进制整数转换为二进制的程序。 它在最后一次弹出时崩溃。 前任。数量:4 输出:10 崩溃
#include <stdio.h>
struct stack {
struct stack *prev;
int val;
struct stack *next;
};
struct stack *first,*cur,*tmp;
struct stack *GETNODE(){
struct stack *pt = (struct stack *)malloc(sizeof(struct stack));
};
int counter=1;
void push(int val){
tmp=GETNODE();
tmp->prev=NULL;
tmp->val=val;
tmp->next=NULL;
if(first==NULL){
first=tmp;
cur=first;
}else{
tmp->prev=cur;
cur->next=tmp;
cur=tmp;
}
counter++;
};
int pop(){
int val=tmp->val;
cur=tmp->prev;
free(tmp);
tmp=cur;
tmp->next=NULL;
counter--;
return(val);
};
main(){
int num = 4;
while(num!=0){
push(num%2);
num/=2;
}
while(counter!=1){
printf("%d ",pop());
}
}
【问题讨论】:
-
MSVC 编译器警告 C4716:
'GETNODE' : must return a value. -
@JonathonReinhart 老师告诉我们这样做的,而且这似乎不是问题
-
也请告诉你的老师
#include <stdlib.h>。 -
如果您的老师真的认为演员表可以替代正确的声明,您需要一位新老师。告诉他们。