adding data to a stack implemented using 2 structs in C: moving pointers -
i'm struggling move pointer 1 element of stack next. have completed implementation. last element of stack given otherwise nothing try seems work. compiler gives error next undeclared (first use of function)
on line pnewnode->next=next
relevant code below:
void push( topstack *ts, int val) { if(ts->num==0) { stack *pnewnode; pnewnode=(stack*)malloc(sizeof(stack)); pnewnode->val=val; pnewnode->next=null; ts->top=pnewnode; } else if(ts->num!=0) { stack *pnewnode; pnewnode=(stack*)malloc(sizeof(stack)); pnewnode->val=val; pnewnode->next=next; ts->top=pnewnode; } }
the structs defined here:
typedef struct stack_elem { int val; struct stack_elem *next; } stack; //struct contains pointer top of stack typedef struct { int num; //num of elements in stack stack *top;; //top of stack } topstack;
i have relevant prototypes functions below, structs in header file. ease of reading have included think relevant code can provide more if needed.
please modify code in else part pnewnode->next=ts->top;
void push( topstack *ts, int val) { if(ts->num==0) { stack *pnewnode; pnewnode=(stack*)malloc(sizeof(stack)); pnewnode->val=val; pnewnode->next=null; ts->top=pnewnode; } else if(ts->num!=0) { stack *pnewnode; pnewnode=(stack*)malloc(sizeof(stack)); pnewnode->val=val; pnewnode->next=ts->top; ts->top=pnewnode; } }
Comments
Post a Comment