第五周 建立顺序栈算法库

来源:互联网 发布:linux内核完全剖析 编辑:程序博客网 时间:2024/05/29 16:41
  1. *Copyright (c) 2017, 烟台大学计算机学院 
  2.  *All rights reserved.  
  3.  *作    者:李浩南 
  4.  *完成日期:2017年10月19日 

  1.  *版 本 号:v1.0 
  2.  *问题描述:定义顺序栈存储结构,实现其基本运算
  3.  *结果显示: (1)初始化栈s
                (2)栈为空
                (3)依次进栈元素a,b,c,d,e
                (4)栈为非空
                (5)栈长度:5
                (6)从栈顶到栈底元素:e d c b a
                (7)出栈序列:e d c b a
                (8)栈为空
  4.             (9)释放栈

  5.   
    [csharp] view plain copy
    1. <span style="color:#ff0000;">sqstack.cpp</span>  
    [csharp] view plain copy
    1. #include <stdio.h>  
    2. #include <malloc.h>  
    3. #include "sqstack.h"  
    4.   
    5. void InitStack(SqStack *&s)  
    6. {  
    7.     s=(SqStack *)malloc(sizeof(SqStack));  
    8.     s->top=-1;  
    9. }  
    10. void DestroyStack(SqStack *&s)  
    11. {  
    12.     free(s);  
    13. }  
    14. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
    15. {  
    16.     return(s->top+1);  
    17. }  
    18. bool StackEmpty(SqStack *s)  
    19. {  
    20.     return(s->top==-1);  
    21. }  
    22. bool Push(SqStack *&s,ElemType e)  
    23. {  
    24.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
    25.         return false;  
    26.     s->top++;  
    27.     s->data[s->top]=e;  
    28.     return true;  
    29. }  
    30. bool Pop(SqStack *&s,ElemType &e)  
    31. {  
    32.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
    33.         return false;  
    34.     e=s->data[s->top];  
    35.     s->top--;  
    36.     return true;  
    37. }  
    38. bool GetTop(SqStack *s,ElemType &e)  
    39. {  
    40.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
    41.         return false;  
    42.     e=s->data[s->top];  
    43.     return true;  
    44. }  
    45.   
    46. void DispStack(SqStack *s)  //输出栈  
    47. {  
    48.     int i;  
    49.     for (i=s->top;i>=0;i--)  
    50.         printf("%c ",s->data[i]);  
    51.     printf("\n");  
    52. }  


      main.cpp
  6.           
    [csharp] view plain copy
    1. #include <stdio.h>  
    2. #include "sqstack.h"  
    3.   
    4. int main()  
    5. {  
    6.     ElemType e;  
    7.     SqStack *s;  
    8.     printf("(1)初始化栈s\n");  
    9.     InitStack(s);  
    10.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
    11.     printf("(3)依次进栈元素a,b,c,d,e\n");  
    12.     Push(s,'a');  
    13.     Push(s,'b');  
    14.     Push(s,'c');  
    15.     Push(s,'d');  
    16.     Push(s,'e');  
    17.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
    18.     printf("(5)栈长度:%d\n",StackLength(s));  
    19.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
    20.     printf("(7)出栈序列:");  
    21.     while (!StackEmpty(s))  
    22.     {  
    23.         Pop(s,e);  
    24.         printf("%c ",e);  
    25.     }  
    26.     printf("\n");  
    27.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
    28.     printf("(9)释放栈\n");  
    29.     DestroyStack(s);  
    30.     return 0;  
    31. }  
    [csharp] view plain copy
    1. <span style="color:#ff0000;">sqstack.h</span>  

    #ifndef SQSTACK_H_INCLUDED
    #define SQSTACK_H_INCLUDED


    #define MaxSize 100
    typedef char ElemType;
    typedef struct
    {
        ElemType data[MaxSize];
        int top;                //栈指针
    } SqStack;                  //顺序栈类型定义


    void InitStack(SqStack *&s);    //初始化栈
    void DestroyStack(SqStack *&s);  //销毁栈
    bool StackEmpty(SqStack *s);     //栈是否为空
    int StackLength(SqStack *s);  //返回栈中元素个数——栈长度
    bool Push(SqStack *&s,ElemType e); //入栈
    bool Pop(SqStack *&s,ElemType &e); //出栈
    bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素
    void DispStack(SqStack *s);  //输出栈
    1. #endif // SQSTACK_H_INCLUDED
    2.  

原创粉丝点击