I try to implement a stack in C but I get a very strange error. For some reason my push function does not work..
typedef struct node
{
int v;
struct node* next;
}Node;
void push(Node *stack,int val)
{
Node *p = (Node *)calloc(1,sizeof(Node));
p->v = val;
Node *aux = stack;
if(aux == NULL)
{
stack = p;
return;
}
while(aux->next != NULL)
aux = aux->next;
aux->next = p;
}
I initialized my stack with NULL
Node *stack = NULL;
and I call the function something like this
push(stack,value)
L.E. I tried to create a pop function with parameter double pointer but the result is the same as for push.
void pop(Node **l)
{
if((*l) == NULL)
return;
else
{
Node *aux,*prev;
prev = *l;
aux = prev->next;
if(aux == NULL)
{
free(prev->v);
free(prev);
return;
}
while(aux != NULL)
{
prev = aux;
aux = aux->next;
}
prev->next = NULL;
free(aux->v);
free(aux);
}
}