My program is supposed to read a postfix expression and convert it to infix and prefix using a tree implementation.
the pop() method always give the first element without errasing it and i can’t figure out why. Any help will be apreciate.
//tree structur
typedef struct asa {
enum { number_exp,sous_exp_op } type;
union {
int child;
struct {
struct asa* left;
struct asa* right;
char oper; } tree;
} op;
} asa;
//stack
typedef struct stack {
int size;
struct {
asa * element;
struct stack* link;
}e;
} stack;
struct stack *top;
(...)
asa * pop(){
asa* e ;
stack * temp;
if(top->size == 0 ){
printf("ERR0R : empty stack\n");
exit (EXIT_FAILURE);
}
else if (top->size >= 1){
temp = top->e.link;
e= top->e.element;
top = temp;
}
return e;
}
void push(asa* node ){
if(top->size == 0 ){
top->e.element = node;
top->e.link = NULL;
top->size++;
}
else if (top->size > 0){
pile * next = (pile*) malloc(sizeof(top));
next = top;
top->e.element = node;
top->e.link = next;
top->size++;
}
}
Logs snapshot:

Your immediate problems are that you are discarding
nextas soon as you allocate it whentop->size > 0and that you are allocating the size for a pointer rather than for the whole struct. To fix them, replacenext = topwithtop = nextat the end of the function and fix thesizeofinvocation:Also, this implementation of stack feels needlessly complex and error-prone. If you need the stack size, you should maintain the size independently of the nodes of the linked list, not in every individual node. The standard linked list idiom is to represent the empty list (stack) as NULL, so neither push nor pop need any extra code to check for empty stack: