Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565184
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: C/C++

2012-03-11 17:53:30

1.什么是栈?
栈(stack)是仅限于在表尾进行插入或删除操作的线性表,表尾称为栈顶,表头称为栈底,不含元素的空表称为空栈。栈又被成为后进先出的线性表。
2. 栈的顺序实现

点击(此处)折叠或打开

  1. /*
  2.     时间:2012年3月10日 23:19:11
  3.     目的:学习c语言,学习数据结构
  4.     功能:栈的顺序实现
  5. */

  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #define TRUE 1
  9. #define FALSE 0
  10. #define OK 1
  11. #define ERROR 0

  12. #define STACK_INIT_SIZE 20
  13. #define STACKINCREMENT 5

  14. typedef int Status;

  15. typedef int SElemType;

  16. typedef struct Stack
  17. {
  18.     SElemType * base;    //栈底指针
  19.      SElemType * top;    //栈顶指针
  20.     int stacksize;        //当前已分配的存储空间,已元素为单位
  21. } SqStack;

  22. Status InitStack(SqStack *s);

  23. void DestroyStack(SqStack *s);

  24. void ClearStack(SqStack *s);

  25. Status StackEmpty(SqStack *s);

  26. int StackLength(SqStack *s);

  27. Status GetTop(SqStack *s, SElemType *e);

  28. Status Push(SqStack *s, SElemType *e);

  29. Status Pop(SqStack *s, SElemType *e);

  30. void StackTraverse1(SqStack *s);

  31. Status InitStack(SqStack *s)
  32. {
  33.     s->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
  34.     if (NULL == s->base)
  35.     {
  36.         printf("内存分配失败,程序退出\n");
  37.         exit(-1);
  38.     }
  39.     s->top = s->base; //空栈的标志:栈顶指针 = 栈底指针
  40.     s->stacksize = STACK_INIT_SIZE;

  41.     return OK;
  42. }

  43. //销毁顺序栈
  44. void DestroyStack(SqStack *s)
  45. {
  46.     free(s->base);
  47.     s->base = NULL;
  48.     s->top = NULL;
  49.     s->stacksize = 0;
  50. }

  51. //置空顺序栈
  52. void ClearStack(SqStack *s)
  53. {
  54.     s->top = s->base;
  55. }

  56. //判断栈是否为空
  57. Status StackEmpty(SqStack *s)
  58. {
  59.     if (s->top == s->base) //这里不要将==写成=号了。。。
  60.         return TRUE;
  61.     else
  62.         return FALSE;
  63. }

  64. //栈中元素个数
  65. int StackLength(SqStack *s)
  66. {
  67.     return (s->top - s->base);
  68. }

  69. //获取栈顶元素,用e返回其值
  70. Status GetTop(SqStack *s, SElemType *e)
  71. {
  72.     SElemType *p = s->top;
  73.     if (StackEmpty(s))
  74.     {
  75.         printf("栈为空\n");
  76.         return ERROR;
  77.     }
  78.     *e = *(--p); //这里不能写成 *e = *(p--);
  79.     return OK;
  80. }

  81. //将元素e压栈,成为新的栈顶元素
  82. Status Push(SqStack *s, SElemType e)
  83. {
  84.     //判断栈是否已满
  85.     if (s->top - s->base >= STACK_INIT_SIZE)
  86.     {
  87.         SElemType *newbase = (SElemType *)realloc(s->base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(SElemType));
  88.         if (NULL == newbase)
  89.         {
  90.             printf("内存增加失败,程序退出\n");
  91.             exit(-1);
  92.         }
  93.         s->base = newbase;
  94.         s->top = s->base + s->stacksize;
  95.         s->stacksize += STACKINCREMENT;
  96.     }
  97.     *(s->top) = e;
  98.     s->top++;

  99.     return OK;
  100. }

  101. //删除s的栈顶元素,并用e返回其值
  102. Status Pop(SqStack *s, SElemType *e)
  103. {
  104.     if (StackEmpty(s))
  105.     {
  106.         printf("栈为空\n");
  107.         return ERROR;
  108.     }

  109.     SElemType *p = s->top;
  110.     *e = *--p; ////这里不能写成 *e = *p--;
  111.     s->top--;
  112.     
  113.     return OK;
  114. }
  115. void StackTraverse1(SqStack *s)
  116. {
  117.     SElemType *q = s->top; //遍历操作后,不能改变top指针所指位置
  118.     while (q - s->base > 0)
  119.     {    
  120.         //q--;
  121.         printf("%d ", *--q); //--运算符优先级高于*,这里用前置--
  122.     }
  123.     printf("\n");
  124. }


  125. int main()
  126. {
  127.     SqStack s;
  128.     SElemType e;
  129.     //初始化栈
  130.     if (InitStack(&s))
  131.     {
  132.         printf("--->初始化栈完成\n");
  133.     }
  134.     else
  135.     {
  136.         printf("初始化栈失败,程序退出\n");
  137.         exit(-1);
  138.     }

  139.     //入栈
  140.     printf("--->创建栈\n");
  141.     Push(&s, 2);
  142.     Push(&s, 4);
  143.     Push(&s, 5);
  144.     Push(&s, 7);
  145.     Push(&s, 9);
  146.     printf("--->栈中元素个数为: %d\n", StackLength(&s));
  147.     StackTraverse1(&s);

  148.     printf("--->清空栈\n");
  149.     ClearStack(&s);
  150.     printf("--->栈中元素个数为: %d\n", StackLength(&s));

  151.     printf("--->构造新栈\n");
  152.     Push(&s, 1);
  153.     Push(&s, 3);
  154.     Push(&s, 5);
  155.     Push(&s, 7);
  156.     Push(&s, 9);
  157.     Push(&s, 11);
  158.     StackTraverse1(&s);
  159.     printf("--->栈中元素个数为: %d\n", StackLength(&s));

  160.     
  161.     //获取栈顶元素
  162.     if (GetTop(&s, &e))
  163.     {
  164.         printf("有栈顶,栈顶值为: %d\n", e);
  165.     }
  166.     else
  167.     {
  168.         printf("空栈,没有栈顶元素\n");
  169.     }
  170.     StackTraverse1(&s);

  171.     //出栈

  172.     while (s.top - s.base > 0)
  173.     {
  174.         if (Pop(&s, &e))
  175.         {
  176.             printf("栈顶元素出栈成功, 栈顶元素为: %d\n", e);
  177.         }
  178.         else
  179.         {
  180.             printf("空栈,没有栈顶元素\n");
  181.         }
  182.     }

  183.     StackTraverse1(&s);

  184.     DestroyStack(&s);

  185.     return 0;
  186. }


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