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

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-05-01 11:38:30

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

  2. void create_stack(void);

  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. #define TRUE 0
  5. #define FALSE -1

  6. struct _node {
  7.         STACK_TYPE value;
  8.         struct _node *next;
  9. };
  10. typedef struct _node stackNode;

  11. static stackNode *stack;

  12. void
  13. create_stack(void)
  14. {
  15.         stack = NULL;
  16. }

  17. void
  18. push(STACK_TYPE value)
  19. {
  20.         stackNode *new_node;
  21.         new_node = malloc(sizeof(stackNode));
  22.         assert(NULL != new_node);
  23.         new_node->value = value;
  24.         new_node->next = stack;
  25.         stack = new_node;
  26. }

  27. void
  28. pop(void)
  29. {
  30.         assert(is_empty() != TRUE);
  31.         stackNode *tmpNode;
  32.         tmpNode = stack;
  33.         stack = stack->next;
  34.         free(tmpNode);
  35. }

  36. STACK_TYPE
  37. top(void)
  38. {
  39.         assert(is_empty() != TRUE);
  40.         return (stack->value);
  41. }

  42. int
  43. is_empty(void)
  44. {
  45.         if (stack == NULL)
  46.                 return TRUE;
  47.         else
  48.                 return FALSE;
  49. }


  50. int
  51. is_full(void)
  52. {
  53.         return FALSE;
  54. }

  55. void
  56. destory_stack(void)
  57. {
  58.         while (is_empty() != TRUE)
  59.                 pop();
  60. }

/*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();

  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: -1

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


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

上一篇:栈 II

下一篇:return 局部变量

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

onezeroone2011-05-01 11:41:34

链表