Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define STACK_NULL -1
- typedef int nodeType;
- typedef struct nodeStackStruct {
- struct nodeStackStruct *next;
- nodeType data;
- } nodeStack;
- void push(nodeStack **top, nodeType data) {
- nodeStack *new;
- new = (nodeStack *)malloc(sizeof(nodeStack));
- new->data = data;
- new->next = *top;
- *top = new;
- }
- nodeType pop(nodeStack **top) {
- nodeType data;
- nodeStack *tmp;
- if (*top == NULL)
- return STACK_NULL;
- data = (*top)->data;
- tmp = *top;
- *top = tmp->next;
- free(tmp);
- return data;
- }
- nodeType top(nodeStack *top) {
- if (top == NULL)
- return STACK_NULL;
- return top->data;
- }
- nodeType deleteStack(nodeStack *top) {
- if (top != NULL) {
- deleteStack(top->next);
- free(top);
- }
- }
- void printStack(nodeStack *top) {
- if (top != NULL) {
- printf("%d\n", top->data);
- printStack(top->next);
- }
- }
- int main() {
- nodeStack *top;
- top = NULL;
- push(&top, 10);
- push(&top, 20);
- printStack(top);
- deleteStack(top);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement