Last active
February 19, 2017 12:34
-
-
Save ddai-dev/3f4e61d9c7d5dd2207041fd8ae22f08b to your computer and use it in GitHub Desktop.
stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| static int sz = 512; | |
| static char *stack;//数据栈 | |
| static int top = 0; | |
| void init_stack(int size) | |
| { | |
| if(size == 0) | |
| size = sz; | |
| else | |
| sz = size; | |
| stack =(char *)malloc(sz); | |
| } | |
| void destory_stack(void) | |
| { | |
| free(stack); | |
| } | |
| void push(char ch) | |
| { | |
| if(top == sz) | |
| { | |
| sz += sz; | |
| stack = (char *)realloc(stack, sz); | |
| } | |
| stack[top++] = ch; | |
| } | |
| char pop(void) | |
| { | |
| return stack[--top]; | |
| } | |
| int is_empty(void) | |
| { | |
| return top == 0; | |
| } | |
| int is_full(void) | |
| { | |
| return top == sz; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern void push(char ch); | |
| extern char pop(void); | |
| extern int is_empty(void); | |
| extern int is_full(void); | |
| extern void init_stack(int size); | |
| extern void destory_stack(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "stack.h" | |
| int main(void) | |
| { | |
| char *str = "abcdefghijklmn"; | |
| init_stack(3); | |
| while(*str != '\0'){ | |
| push(*str); | |
| str++; | |
| } | |
| while(is_empty() == 0 ) | |
| putchar(pop()); | |
| putchar('\n'); | |
| destory_stack(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment