• 顺序栈基本操作


    #include<stdio.h>
    #include<stdlib.h>
    #define MAXSIZE 1024
    typedef int elemtype;
    typedef struct SequenStack
    {
        elemtype data[MAXSIZE];
        int top;                        //设置顺序栈的栈顶指针 
    }SequenStack;                        //顺序栈的结构类型
    
    SequenStack * Init_SequenStack();
    int SequenStack_Length(SequenStack *S);
    int SequenStack_Empty(SequenStack *S);
    int SequenStack_Full(SequenStack *S);
    int Push_SequenStack(SequenStack *S,elemtype x);
    int Pop_SequenStack(SequenStack *S);
    void menu();
    
    void menu()
    {   system("cls");
        printf("		1-initial stack
    ");
        printf("		2-input data
    ");
        printf("		3-get length
    ");
        printf("		4-判断栈空
    ");
        printf("		5-判断栈满
    ");
        printf("		6-出栈
    ");
        printf("		7-输出
    ");
        printf("		#-quit
    ");
        printf("Please select:  ");
    }
    int main()
    {
        char cmd;
        SequenStack *S;
        elemtype x;
        int isdo,i,len;
        system("cls");
        menu();
        while((cmd=getchar())!='#')
        {
            switch(cmd)
            {    case '1':   S= Init_SequenStack( );
                            printf("
    Creatied the stack!
    ");
                            printf("
    
    
    			");
                            break;
                case '2':    printf("请插入数据 0结束
    ");
                            scanf("%d",&x);
                            while(x!=0)
                            {
                                isdo=Push_SequenStack(S,x);
                                if(isdo==0)
                                {    printf("栈满结束
    ");
                                    break;
                                }
                                scanf("%d",&x);
                            }
                            printf("
    
    
    			");
                            break;
                case '3':    len=SequenStack_Length(S);
                            printf("
    Caculated the Length of the list...
    ");
                            printf("len=%d
    ",len);
                            printf("
    
    
    			");
                            break;
                case '4':    isdo=SequenStack_Empty(S);
                            if(isdo==0)
                            {
                                printf("栈不是空
    ");
                            }
                            else if(isdo==1)
                            {
                                printf("栈为空
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '5':    isdo=SequenStack_Full(S);
                            if(isdo==0)
                            {
                                printf("栈不满
    ");
                            }
                            else if(isdo==1)
                            {
                                printf("栈为满
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '6':    printf("
    Geting the top data of the stack...
    ");
                            isdo=Pop_SequenStack(S);
                            if(isdo==1)
                            {
                                len--;
                                printf("出栈成功
    ");
                                printf("新的栈顶元素:%d
    ",S->data[S->top]);
                            }
                            else if(isdo==0)
                            {
                                printf("出栈失败
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '7':    while(S->top>=0)
                            {
                                printf("%d	",S->data[S->top]);
                                S->top--;                        
                            }
                            for(i=0;i<len;i++)
                            {
                                S->top++;
                            }
                            printf("
    
    
    			");
                            break;
            }
            fflush(stdin);
            system("pause");
            menu();
        }
        return 0;
    }
    SequenStack * Init_SequenStack()        //初始化 
    {
        SequenStack *S;                    //定义顺序栈指针变量 
        S=(SequenStack *)malloc(sizeof(SequenStack));            //申请内存空间 
        S->top=-1;
        return S; 
    }
    //判断栈空
    int SequenStack_Empty(SequenStack *S)
    {
        if(S->top==-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    //判断栈满
    int SequenStack_Full(SequenStack *S)
    {
        if(S->top+1==MAXSIZE)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    //求顺序栈的长度
    int SequenStack_Length(SequenStack *S)
    {
        return S->top+1;
    } 
    //入栈
    int Push_SequenStack(SequenStack *S,elemtype x)
    {
        if(S->top>=MAXSIZE-1)
        {
            return 0;                                        //栈满,插入失败,返回0; 
        }
        else
        {
            S->top++;
            S->data[S->top]=x;
            return 1;
        }
    }
    //出栈
    int Pop_SequenStack(SequenStack *S)
    {
        if(S->top==-1)
        {
            return 0;                                    //栈空 
        }
        else
        {
            S->top--; 
            return 1;
        } 
    } 
  • 相关阅读:
    写了这么久前端,你知道浏览器原理吗?
    史上最全的web前端开发程序员学习清单!
    常见前端面试题及答案
    “下辈子再也不当程序员了,我被黑够了”
    什么是web前端,全栈工程师就业前景怎么样?
    要嫁就嫁程序员,钱多话少死得早!
    想转行做web前端工程师,必学这6大技能
    测试用例设计总结
    python读取一个英文文件,并记录每个单词出现的次数,降序输出
    python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中
  • 原文地址:https://www.cnblogs.com/xxs812/p/7977201.html
Copyright © 2020-2023  润新知