/** * define struct of stack */ typedefstructNode { int data; structNode *next; } Node;
/** * init stack * @return the head pointer of stack */ Node *initStack() { Node *L = (Node *)malloc(sizeof(Node)); L->data = 0; L->next = NULL; return L; }
/** * push item in stack * @param L the head pointer of stack * @param data the data you want to push */ voidpush(Node *L, int data) { Node *node = (Node *)malloc(sizeof(Node)); node->data = data; node->next = L->next; L->next = node; L->data++; }
/** * pop item in stack * @param L the head pointer of stack * @return data */ intpop(Node *L) { if (L->data == 0) { return0; } else { Node *node = L->next; int data = node->data; L->next = node->next; free(node); L->data--; return data; } }
/** * judge stack is or not empty * @param L the head pointer of stack * @return empty flag */ intisEmpty(Node *L) { if (L->data == 0 || L->next == NULL) { return1; } else { return0; } }
/** * print all items in stack * @param stack the head pointer of stack */ voidprintStack(Node *stack) { Node *node = stack->next; while (node) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); }