Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370694
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-05-01 11:14:43

/*stack.h*/
  1. #define STACK_TYPE int

  2. void create_stack(unsigned int size);

  3. void push(STACK_TYPE value);

  4. void pop(void);

  5. STACK_TYPE top(void);

  6. int is_empty(void);

  7. int is_full(void);

  8. void destory_stack(void);

/*stack.c*/
  1. #include "stack.h"
  2. #include <assert.h>
  3. #include <stdlib.h>

  4. static STACK_TYPE *stack;
  5. static size_t stack_size;
  6. static int top_element = -1;

  7. void
  8. create_stack(size_t size)
  9. {
  10.         assert(stack_size == 0);
  11.         stack_size = size;
  12.         stack = malloc(stack_size * sizeof(STACK_TYPE));
  13.         assert(stack != NULL);
  14. }

  15. void
  16. push(STACK_TYPE value)
  17. {
  18.         assert(is_full != 0);
  19.         top_element++;
  20.         stack[top_element] = value;
  21. }

  22. void
  23. pop(void)
  24. {
  25.         assert(is_empty != 0);
  26.         top_element--;
  27. }

  28. STACK_TYPE
  29. top(void)
  30. {
  31.         assert(is_empty != 0);
  32.         return (stack[top_element]);
  33. }

  34. int
  35. is_empty(void)
  36. {
  37.         if (-1 == top_element)
  38.                 return 0;
  39.         else
  40.                 return -1;
  41. }

  42. int
  43. is_full(void)
  44. {
  45.         if (stack_size - 1 == top_element)
  46.                 return 0;
  47.         else
  48.                 return -1;
  49. }


  50. void
  51. destory_stack(void)
  52. {
  53.         assert(stack_size > 0);
  54.         stack_size = 0;
  55.         free(stack);
  56.         stack = NULL;
  57. }

/*test.c*/
  1. #include <stdio.h>
  2. #include "stack.h"

  3. int
  4. main(void)
  5. {
  6.         int a[10] = {1,3,5,7,9,0,2,4,6,8};
  7.         int len = sizeof(a)/sizeof(a[0]);
  8.         int i;

  9.         create_stack(len);

  10.         printf("stack is_empty: %d\n", is_empty());
  11.         printf("stack is_full: %d\n", is_full());
  12.         printf("\n");

  13.         for (i = 0; i < len; i++) {
  14.                 push(a[i]);
  15.                 printf("%d,", top());
  16.         }
  17.         printf("\n");

  18.         printf("stack is_empty: %d\n", is_empty());
  19.         printf("stack is_full: %d\n", is_full());
  20.         printf("\n");

  21.         for (i = 0; i < len; i++) {
  22.                 printf("%d,", top());
  23.                 pop();
  24.         }
  25.         printf("\n");

  26.         printf("stack is_empty: %d\n", is_empty());
  27.         printf("stack is_full: %d\n", is_full());
  28.         printf("\n");

  29.         destory_stack();

  30.         return 0;
  31. }

/*output*/
  1. stack is_empty: 0
  2. stack is_full: -1

  3. 1,3,5,7,9,0,2,4,6,8,
  4. stack is_empty: -1
  5. stack is_full: 0

  6. 8,6,4,2,0,9,7,5,3,1,
  7. stack is_empty: 0
  8. stack is_full: -1






阅读(1230) | 评论(1) | 转发(0) |
0

上一篇:栈 I

下一篇:栈 III

给主人留下些什么吧!~~

onezeroone2011-05-01 11:18:33

动态数组