very new to C. creating a stack and implementing the create(), push(), pop(), empty() routines into another class. my stack class looks like: Code: struct list_item { char data; struct list_item *next; } typedef struct list_item stackItem; extern stackItem *header; void create(); void push(); void pop(); int empty (); not sure if this is all ok. but then my operator class follows: #include <stdio.h> #include <alloc.h> #include "stackhead.h" stackItem *header; void create() { stackItem *newStack; newStack = (stackItem *) malloc( sizeof(stackItem) ); newStack->data=NULL; newStack->next=NULL; }; void push(stackItem **head, char newData) { stackItem *tempItem; tempItem = (stackItem *) malloc( sizeof(stackItem) ); tempItem->data = newData; tempItem->next = *head; *head = tempItem; }; void pop(stackItem head) { *head->next = header; }; int empty(stackItem **head) { if (*head == NULL) { return 1; } else { return 0; } }; Not sure how correct any of these routines are. I am having the most trouble with pop, will not even compile. The dereference operators are still new to me so I am still struggling with those too. Would just like some feedback on what I have so far.
Where is your main function? Are you trying to write all the code at once? If so, don't do that. Write one bit, get it working, then write the next bit and so on. The "obvious" place to start is with the ability to (a) push onto the stack and (b) display the whole stack. Once you have those, you can then see what's going on in the rest of the program; for example print/pop/print will display the stack twice but without the top entry the second time - when it's working correctly. create() is wrong in that you keep no record of newStack. So you have no way to access the stack and this becomes a memory leak. Probably you need to assign this to something (header?) Is header defined in another file? Why is it defined "extern"? In fact why is it defined at all? It's a global variable, which equals VERY BAD, so don't use them unless you really need to. A program that uses stacks is likely to use a few of them not just one global stack. Also the other functions seem to suggest you'll want to pass the stack pointer in, so probably a global stackItem * isn't needed.