Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7539641
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-02-28 08:56:47

  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. #define TRUE 1
  4. #define FALSE 0

  5. typedef struct node {
  6.   int data;
  7.   struct node *next;
  8. } Stack;


  9. /*栈初使化*/
  10. void StackInit(Stack *top)
  11. {
  12.     top->next = NULL;
  13. }


  14. /*进栈*/
  15. int Push(Stack *top, int x)
  16. {
  17.     Stack *p;
  18.     p = (Stack *)malloc(sizeof(Stack));
  19.     if(p == NULL)
  20.         return FALSE;
  21.     p->data = x;
  22.     p->next = top->next;
  23.     top->next = p;
  24.     return TRUE;    
  25. }


  26. /*栈顶指针是否为空*/
  27. int IsEmpty(Stack *top)
  28. {
  29.     if(top->next == NULL)
  30.         return TRUE;
  31.     return FALSE;
  32. }

  33. /*出栈*/
  34. int Pop(Stack *top, int *x)
  35. {
  36.     Stack *p;
  37.     if(IsEmpty(top))
  38.         return FALSE;
  39.     p = top->next;
  40.     *x = p->data;
  41.     top->next = p->next;
  42.     free(p);
  43.     return TRUE;
  44. }

  45. int main()
  46. {
  47.   int x;
  48.   Stack *top = (Stack *)malloc(sizeof(Stack));
  49.   StackInit(top);                        /* 栈初始化 */
  50.   printf("input some positive integers:\n");
  51.   scanf("%d", &x);
  52.   while(x > 0)
  53.   {
  54.     Push(top, x);                        /* 进栈 */
  55.     scanf("%d", &x);
  56.   }
  57.   while(Pop(top, &x))                    /* 出栈 */
  58.   {
  59.     printf("%d ", x);                         /* 输出到屏幕 */
  60.   }
  61.   printf("\n", x);
  62.   return 0;
  63. }
阅读(1361) | 评论(0) | 转发(2) |
0

上一篇:队列

下一篇:线性表

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