Im making a program to convert a decimal int to binary using a dynamic stack. It crashes on the last pop. ex. num: 4 output: 10crash
#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());
}
}