c - Stack TOP pointer is always null -
i creating new stack using linked list. dont know why top pointer pointing null. think not setting top pointer correctly, or not visible outside function.
#include "stdio.h" #include "stdlib.h" typedef struct stackitem { int itemvalue; struct stackitem* nextitemptr; }stackitem; typedef struct stack { struct stackitem *top; }stack; int isstackempty(stackitem *top) { if(top==null) homecoming 1; } void pushitem(stackitem *headptr,int n) { stackitem* newnode; newnode=(stackitem*)malloc(sizeof(stackitem)); newnode->itemvalue=n; printf("checking head top %d\n\n",isstackempty(headptr)); //everytime giving 1 newnode->nextitemptr=isstackempty(headptr)?null:headptr; headptr=newnode; } int main() { stack* stackptr; stackptr=(stack*)malloc(sizeof(stack)); stackptr->top=null; pushitem(stackptr->top,3); pushitem(stackptr->top,6); homecoming 0; }
this statement nil caller:
headptr=newnode; you can:
pass double pointer , assign*headptr return newnode instead of assigning headptr this c faq explains subject.
c
No comments:
Post a Comment