I am trying to write a separate file with helper functions for stack operations. I want to pass the stack top by reference as an argument to stack operations from the main file.
Since top is getting modified, I am passing the pointer top by reference. But even then, it is not working. Where am I going wrong?
P.S.: I know that this is not the best way to implement Stack, but i just wanted to understand why it is not working.
//Stack.h
void print(stacknode **P)
{
stacknode *S;
S=*P;
printf("Printing stack from top to bottom...\n");
stacknode *temp=S;
while(temp != NULL)
{
printf("%d\t", temp->data);
temp=temp->next;
}
printf("\n");
}
void push(stacknode **P, int n)
{
stacknode *S;
S=*P;
stacknode *new=(stacknode *)malloc(sizeof(stacknode));
new->data=n;
new->next=S;
S=new;
print(&S);
}
//main.c
main()
{
printf("Creating new stack...\n");
stacknode *S=NULL;
printf("Pushing first number....\n");
push(&S, 2);
print(&S);/*Prints nothing*/
}