zoukankan      html  css  js  c++  java
  • 堆C数组实现

    堆栈是一个最后出来该数据结构.

    栈的基本操作包含:入栈,出栈,初始化栈,清空栈,遍历栈.


    C代码例如以下:

    #include <stdio.h>
    
    #define MaxSize 20
    typedef int ElemType;
    
    
    typedef struct stack
    {
        ElemType Data[MaxSize];
        int top;
    }Stack;
    
    //初始化栈
    void InitStack(Stack *S)
    {
        S->top=-1;
    }
    
    //入栈
    void PushStackValue(Stack *S)
    {
        printf("Input the Value of stack member:
    (0-exit)
    ");
        int value;
        printf("Please input the 1st value of stack:
    ");
        scanf("%d",&value);
        S->Data[++S->top]=value;
        while(value)
        {
            S->top++;
            printf("Please input the %dst value of stack:
    ",S->top+1);
            scanf("%d",&value);
            S->Data[S->top]=value;
        }
    }
    
    //出栈
    void PopStackValue(Stack *S)
    {
        if(S->top>=0)
        {
            printf("the stack %dst value pop out: %d
    ",S->top+1,S->Data[--S->top]);
        }
        else
        {
            printf("The Stack is empty
    ");
        }
    }
    
    //推断栈空
    void IsEmpty(Stack *S)
    {
        if(S->top==-1)
        {
            printf("The Stack is empty.
    ");
        }
        else
        {
            printf("The stack is not empty.
    ");
        }
    }
    
    
    //清空栈
    void ClearStack(Stack *S)
    {
        S->top=-1;
    }
    
    //遍历栈
    void ScanStack(Stack *S)
    {
        int i;
        int len=S->top-1;
        int StackArray[len];
        for(i=len;i>0;i--)
        {
            StackArray[i]=S->Data[i--];
        }
        printf("The all stack member(from top to bottom) is:
    ");
        while(len>=0)
        {
            printf("%d ",S->Data[len--]);
        }
        printf("
    ");
    }
    
    void main()
    {
        Stack S;
    
        InitStack(&S);
        PushStackValue(&S);
        ScanStack(&S);
        IsEmpty(&S);
        PopStackValue(&S);
        PopStackValue(&S);
        PopStackValue(&S);
        PopStackValue(&S);
    }
    


    执行结果例如以下:




    转载请注明:刘

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    iOS 9音频应用播放音频之第一个ios9音频实例2
    隐写术: 基地组织如何用视频隐藏绝密文档?
    stm32 Fatfs 读写SD卡
    STM32应用笔记转载
    STM32-NVIC中断管理实现[直接操作寄存器]
    FatFs源码剖析
    STM32固件库详解(转)
    Arduino线程库ProtoThreads
    ARM9(TQ2440)裸机代码分享
    将USBASP改造成STK502编程器(转)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4622133.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com