Chinaunix首页 | 论坛 | 博客
  • 博客访问: 444743
  • 博文数量: 89
  • 博客积分: 2713
  • 博客等级: 少校
  • 技术积分: 938
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 21:19
个人简介

为了成为自由自在的人而奋斗!

文章分类

全部博文(89)

文章存档

2016年(5)

2015年(9)

2014年(2)

2013年(10)

2012年(1)

2011年(30)

2010年(32)

分类: C/C++

2011-09-08 11:35:56

fw_stack.h:
  1. /* 栈先进后出,队列是先进先出,其它没什么区别了,可以认为都是线性表 */
  2. #ifndef _FW_STACK_H_
  3. #define _FW_STACK_H_
  4. #include <stdio.h>

  5. #define SUCC     1
  6. #define FAIL    0

  7. typedef struct fw_node_s {
  8.     struct fw_node_s *prev;
  9.     struct fw_node_s *next;
  10.     int n;
  11. }fw_node_t;

  12. typedef struct fw_stack_s {
  13.     fw_node_t *top;
  14.     fw_node_t *bottom;
  15. }fw_stack_t;

  16. #define INIT_STACK(stack) do {\
  17.     (stack)->top = NULL;\
  18.     (stack)->bottom = NULL;\
  19. } while(0)

  20. int push_stack(fw_stack_t *stack, fw_node_t *new_node);

  21. int pop_stack(fw_stack_t *stack, int *n);

  22. void destroy_stack(fw_stack_t *stack);

  23. void display_stack(fw_stack_t *stack);

  24. int get_stack_top(fw_stack_t *stack, int *top_n);

  25. #endif
 
fw_stack.c:
  1. #include <memory.h>
  2. #include
  3.     
  4. #include "fw_stack.h"
  5.     
  6. int push_stack(fw_stack_t *stack, fw_node_t *new_node)
  7. {
  8.     if (stack == NULL || new_node == NULL) {
  9.         printf("%s:bad paras\n", __FUNCTION__);
  10.         return FAIL;
  11.     }
  12.     
  13.     if (stack->bottom == NULL) {
  14.         stack->bottom = new_node;
  15.         stack->top = new_node;
  16.         return SUCC;
  17.     }
  18.     
  19.     new_node->prev = stack->top;
  20.     stack->top->next = new_node;
  21.     stack->top = new_node;
  22.     
  23.     return SUCC;
  24. }

  25. int pop_stack(fw_stack_t *stack, int *n)
  26. {    
  27.     fw_node_t *tmp;
  28.     
  29.     if (stack == NULL || n == NULL) {
  30.         printf("%s:bad paras\n", __FUNCTION__);
  31.         return FAIL;
  32.     }
  33.     
  34.     if (stack->bottom == NULL) {
  35.         return FAIL;
  36.     }
  37.     
  38.     /* 如果只有一个元素 */
  39.     if (stack->top == stack->bottom) {
  40.         *n = stack->top->n;
  41.         free(stack->top);
  42.         stack->top = NULL;
  43.         stack->bottom = NULL;
  44.         return SUCC;
  45.     }
  46.     
  47.     *n = stack->top->n;
  48.     stack->top->prev->next = NULL;
  49.     tmp = stack->top;
  50.     stack->top = stack->top->prev;
  51.     free(tmp);
  52.     
  53.     return SUCC;
  54. }

  55. void destroy_stack(fw_stack_t *stack)
  56. {    
  57.     int n;
  58.     
  59.     if (stack == NULL) {
  60.         printf("%s:bad paras\n", __FUNCTION__);
  61.         return;
  62.     }
  63.     
  64.     while (stack->bottom) {
  65.         pop_stack(stack, &n);
  66.     }
  67.     
  68.     return;
  69. }

  70. void display_stack(fw_stack_t *stack)
  71. {
  72.     fw_node_t *index;
  73.     
  74.     if (stack == NULL) {
  75.         printf("%s:bad paras\n", __FUNCTION__);
  76.         return;
  77.     }
  78.     
  79.     printf("\nstack:\n");
  80.     if (stack->bottom == NULL) {
  81.         printf("NULL stack!\n");
  82.         return;
  83.     }

  84.     printf(" == ");
  85.     index = stack->top;
  86.     while (index) {
  87.         printf("%d<-", index->n);
  88.         index = index->prev;
  89.     }
  90.     printf("\n");
  91.     
  92.     return;
  93. }

  94. int get_stack_top(fw_stack_t *stack, int *top_n)
  95. {
  96.     if (stack == NULL || top_n == NULL) {
  97.         printf("%s:bad paras\n", __FUNCTION__);
  98.         return FAIL;
  99.     }
  100.     
  101.     if (stack->top == NULL) {
  102.         printf("%s:top is NULL\n", __FUNCTION__);
  103.         return FAIL;
  104.     }
  105.     
  106.     *top_n = stack->top->n;
  107.     
  108.     return SUCC;
  109. }

  110. int main()
  111. {
  112.     fw_stack_t my_stack;
  113.     fw_node_t *new_node;
  114.     int op, n;
  115.     
  116.     INIT_STACK((&my_stack));
  117.     while (1) {
  118.         printf("====================================\n");
  119.         printf("\n1:push 2:pop 3:get top node 4:quit\n");
  120.         scanf("%d", &op);
  121.         switch(op) {
  122.         case 1:
  123.             printf("input the number:\n");
  124.             if (scanf("%d", &n) != 1) {
  125.                 while (getchar() != '\n') {
  126.                 }
  127.                 printf("input error!\n");
  128.                 break;
  129.             }
  130.             new_node = (fw_node_t *)malloc(sizeof(fw_node_t));
  131.             if (new_node == NULL) {
  132.                 printf("malloc failed!\n");
  133.                 goto _quit;
  134.             }
  135.             memset(new_node, 0, sizeof(fw_node_t));
  136.             new_node->n = n;
  137.             if (push_stack(&my_stack, new_node) == FAIL) {
  138.                 printf("push failed!\n");
  139.                 free(new_node);
  140.             }
  141.             display_stack(&my_stack);
  142.             break;
  143.         case 2:
  144.             if (pop_stack(&my_stack, &n) == FAIL) {
  145.                 printf("pop failed!\n");
  146.             } else {
  147.                 printf("pop %d\n", n);
  148.             }
  149.             display_stack(&my_stack);
  150.             break;
  151.         case 3:
  152.             if (get_stack_top(&my_stack, &n) == FAIL) {
  153.                 break;
  154.             }
  155.             printf("Get top %d\n", n);
  156.             display_stack(&my_stack);
  157.             break;
  158.         case 4:
  159.             display_stack(&my_stack);
  160.             goto _quit;
  161.         default:
  162.             printf("error code!\n\n");
  163.             while (getchar() != '\n');
  164.             break;
  165.         }
  166.     }
  167.     
  168. _quit:
  169.     destroy_stack(&my_stack);

  170.     return 0;
  171. }

 

阅读(717) | 评论(0) | 转发(0) |
0

上一篇:单向链表

下一篇:快速排序算法实现

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