スタック操作用のヘルパー関数を含む別のファイルを書き込もうとしています。メインファイルからスタック操作への引数としてスタックトップを参照渡ししたい。
top が変更されているため、ポインター top を参照渡ししています。しかし、それでも機能していません。どこが間違っていますか?
PS: これが Stack を実装する最良の方法ではないことはわかっていますが、それが機能しない理由を理解したかっただけです。
//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*/
}