Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474956
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 16:20:11


点击(此处)折叠或打开

  1. //stack.c

  2. //用结构体数组实现栈

  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <string.h>

  6. #define NUM 10

  7. typedef struct stack
  8. {
  9.     int top;
  10.     char stack[NUM];
  11. }stack_t;

  12. void init(stack_t *st)
  13. {
  14.     memset(st, 0, sizeof(stack_t));
  15. }

  16. void push(stack_t *st, char n)
  17. {
  18.     st->stack[st->top++] = n;
  19. }

  20. char pop(stack_t *st)
  21. {
  22.     return st->stack[-- st->top];
  23. }

  24. bool is_empty(stack_t *st)
  25. {
  26.     return st->top == 0;
  27. }

  28. bool is_full(stack_t *st)
  29. {
  30.    return st->top == NUM ;
  31. }

  32. int main(int argc, const char *argv[])
  33. {
  34.     char ch;
  35.     stack_t st;

  36.     init(&st);

  37.     while(! is_full(&st))
  38.     {
  39.         ch = getchar();

  40.         if(ch == '\n')
  41.             break;
  42.         push(&st, ch);
  43.     }

  44.     while(! is_empty(&st))
  45.         putchar(pop(&st));
  46.     putchar('\n');

  47.     return 0;
  48. }

点击(此处)折叠或打开

  1. //stack_link.c

  2. //用链表实现栈

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>

  6. #define MAX 128

  7. typedef struct node
  8. {
  9.     void *data;
  10.     struct node *next;
  11. }node_t;


  12. typedef struct
  13. {
  14.     node_t *top;
  15. }top_t;

  16. void init_top(top_t *t)
  17. {
  18.    t->top = NULL;
  19. }

  20. void push(top_t *st, void *data)
  21. {
  22.     node_t *cur;

  23.     cur = malloc(sizeof(node_t));
  24.     if(cur == NULL)
  25.     {
  26.         printf("malloc fail\n");
  27.         exit(1);
  28.     }

  29.     cur->data = data;
  30.     cur->next = NULL;

  31.     if(st->top == NULL)
  32.         st->top = cur;
  33.     else
  34.     {
  35.        cur->next = st->top;
  36.        st->top = cur;
  37.     }
  38. }

  39. node_t *pop(top_t *st)
  40. {
  41.     node_t *cur;
  42.     
  43.     cur = st->top;
  44.     st->top = cur->next;

  45.     return cur;
  46. }

  47. bool is_empty(top_t *st)
  48. {
  49.     return st->top == NULL;
  50. }



  51. int main(int argc, const char *argv[])
  52. {
  53.     char ch;
  54.     char *p;
  55.     top_t st;

  56.     init_top(&st);
  57.     
  58.     while((ch = getchar()) != '\n')
  59.     {
  60.         p = malloc(sizeof(char));
  61.         *p = ch;
  62.         push(&st, p);
  63.         //push(&st, &ch);
  64.         //此处压的是一个char类型的地址,push完成将该地址内的值压栈,ch最后存的是'\n'
  65.     }
  66.     while(! is_empty(&st))
  67.     {
  68.         node_t *cur = pop(&st);
  69.         printf("-%c-", *((char *)(cur->data)));
  70.         free(cur);
  71.     }

  72.     free(p);
  73.     putchar('\n');
  74.     return 0;
  75. }

阅读(1414) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~