This is the initial part of main function I used:
int main() {
int q;
struct ListNode* S = NULL;
scanf("%d\n",&q);
int i;
for (i=0;i<q;i++){
int queryType;
scanf("%d", &queryType);
struct NumOp* j;
struct NumOp k;
if(queryType == 1){
double num;
scanf("%lf", &num);
if(num>=1 && num<=100){
k.type='n';
k.num=num;
k.op='\0';
j=&k;
Push(&S,j);
}
}
Now the issue is this would create a stack consisting of the last pushed element in all the nodes pushed by this time. On making certain changes the following code seemed to work fine. Could anyone tell me why this didn't work in first place.
int main() {
int q;
struct ListNode* S = NULL;
scanf("%d\n",&q);
int i;
for (i=0;i<q;i++){
int queryType;
scanf("%d", &queryType);
struct NumOp* j = (struct NumOp*) malloc(sizeof(struct NumOp));
if(queryType == 1){
double num;
scanf("%lf", &num);
if(num>=1 && num<=100){
j->type='n';
j->num=num;
j->op='\0';
Push(&S,j);
}
The explanation is what I m looking for.