fork download
  1. #include<stdio.h>
  2. #define MAX 10
  3.  
  4. void push( char c, char* s, int* top )
  5. {
  6. if( *top == MAX ) {
  7. printf( "Can not push any more!\n" );
  8. }
  9. else {
  10. s[*top] = c;
  11. (*top)++;
  12. }
  13. }
  14. char pop( char* s, int* top )
  15. {
  16. if( *top == 0 ) {
  17. printf( "No stack!\n" );
  18. return 0;
  19. }
  20. else {
  21. (*top)--;
  22. return s[*top];
  23. }
  24. }
  25. void print_stack_ary( char *s, int top )
  26. {
  27. int i;
  28.  
  29. for( i = top; i > 0; i-- )
  30. printf( "%c\n", s[i-1] );
  31. }
  32. int main()
  33. {
  34.  
  35. char s[MAX];
  36. int top = 0;
  37. char c;
  38.  
  39. push( 'a', s, &top );
  40. push( 'b', s, &top );
  41. push( 'c', s, &top );
  42. push( 'd', s, &top );
  43. push( 'e', s, &top );
  44.  
  45. print_stack_ary( s, top );
  46. c = pop( s, &top );
  47. printf( "poped %c\n", c );
  48. print_stack_ary( s, top );
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
e
d
c
b
a
poped e
d
c
b
a