zoukankan      html  css  js  c++  java
  • C语言实现顺序栈的初始化&进栈&出栈&读取栈顶元素

    Code

    /*顺序表实现栈的一系列操作*/ 
    
    #include<stdio.h>
    #include<stdlib.h> 
    
    #define Stack_Size 50             //设栈中元素个数为50 
    #define OK 1
    #define ERROR 0
    
    typedef struct                
    {
        int elem[Stack_Size];          //用来存放栈中元素的一维数组 
        int top;                       //用来存放栈顶元素的下标,top为 -1 表示空栈 
    }SeqStack;
    
    /**********************各个子函数的定义*********************/
    int initStack(SeqStack *S);         //初始化顺序栈 
    void push(SeqStack *S,int n);          //顺序栈进栈运算 
    void pop(SeqStack *S);                //顺序栈出栈运算 
    int getTop(SeqStack *S,int *s);     //读取栈顶元素 
     
    int main()
    {
        SeqStack *S;
        int choice;
        while(true)
        {
            printf("*****************Please enter your choice*****************
    
    ");
            printf("                choice 1:Stack initialization
    ");
            printf("                choice 2:Into the stack
    ");
            printf("                choice 3:Out of the stack
    ");
            printf("                choice 4:Read the stack elements
    ");
            printf("                choice 0:exit
    
    ");
             scanf("%d",&choice);
            switch(choice)
            {
                case 1:
                    (initStack(S)==1)?printf("initStck success.
    "):printf("initStack ERROR
    ");
                    break;
                case 2:
                    int n;
                    printf("Please enter the number into the stack elements:");
                    scanf("%d",&n);
                    push(S,n);
                    break;
                case 3:
                    pop(S);
                    break;
                case 4:
                    int* s;
                    (getTop(S,s)==1)? printf("栈顶元素是:%d.
    ",*s):printf("An empty stack error!!!!
    "); //三目运算符
                    break;        
                case 0:
                    exit(0);
                    break;
                default:
                    printf("ERROR!!
    ");
                    exit(0);
                    break;
            }
        }
        return 0;
    } 
    
    /**********************各个子函数功能的实现*********************/
    int initStack(SeqStack *S)    //初始化顺序栈 
    {         
        if(S!=NULL)
        {
            S->top=-1;             //置为空栈 
            return OK;
        }
        else return ERROR;        //内存空间不足 
    }
    void push(SeqStack *S,int n)  //进栈 ,将元素压入栈中 
    {   
        int n1,n2;
        if(((S->top)+n)<=Stack_Size-1)   //压入栈中的元素不能超过栈的最大存储 
        {  
            printf("Please enter into the stack elements in turn:
    ");  
            for(n1=0;n1<n;n1++)
            {
                scanf("%d",&n2);
                S->top++;                    //移动栈顶指针 
                S->elem[S->top]=n2;
            }
            printf("%d个元素依次进栈成功
    ",n);
        }
        else
        {                    //栈空间不够 
            printf("ERROR There is insufficient space on the stack.
    ");            
        }                 
    }
    void pop(SeqStack *S)
    {     //栈顶元素出栈 
        int a;
        if(S->top==-1)
        {               //栈为空,操作失败 
            printf("An empty stack error!!!!
    ");
        }
        else
        {
            a=S->elem[S->top];
            S->top--;
            printf("栈顶元素%d出栈成功.
    ",a);  //出栈成功 
        }  
    
    }
    int getTop(SeqStack *S,int *s)   //获取栈顶元素 
    {    
        if(S->top==-1)
        {               //栈为空,操作失败 
            return ERROR;
        }
        else
        {
            *s=S->elem[S->top];   //读取栈顶元素成功 
            return OK;
        }
    }
  • 相关阅读:
    软件架构阅读笔记04
    软件架构阅读笔记03
    TortoiseGit和intellij idea配置秘钥
    linux关闭在线登录用户
    汉化gitlab
    GitLab服务器搭建
    redis 中如何切换db
    弹性伸缩问题
    Filebeat+Logstash自定义多索引
    logstash
  • 原文地址:https://www.cnblogs.com/qftm/p/10317155.html
Copyright © 2011-2022 走看看