zoukankan      html  css  js  c++  java
  • C语言——栈的基本运算在顺序栈上的实现

    头文件

    Seqstack.h

    #define maxsize 6
    //const int maxsize = 6;
    
    // 顺序栈
    
    typedef struct seqstack
    {
        int data[maxsize];
        int top; // 标志栈顶位置的变量
    }SeqStk;

    main.c

    #include <stdio.h>
    #include "Seqstack.h"
    
    // 栈的基本运算在顺序栈上的实现
    // 1. 初始化
    int InitStack(SeqStk *stk)
    {
        stk->top = 0;
        return 1;
    }
    // 2. 判断栈空
    int EmptyStack(SeqStk *stk)
    {
        if(stk->top == 0) return 1;
        else return 0;
    }
    // 3.进栈
    int Push(SeqStk *stk, int x)
    {
        if(stk->top == maxsize - 1)
        {
            printf("栈已满
    ");
            return 0;
        }
        else
        {
            stk->data[stk->top] = x;
            stk->top++;
            return 1;
        }
    }
    // 4. 出栈
    int Pop(SeqStk *stk)
    {
        if(EmptyStack(stk))
        {
            printf("下溢");
            return 0;
        }
        else
        {
            stk->top--;
            return 1;
        }
    }
    // 5. 取栈顶元素
    int GetTop(SeqStk *stk)
    {
        if(EmptyStack(stk)) return 0;
        else return stk->data[stk->top];
    }
    
    main()
    {
        SeqStk stk;
        int i;
        i = InitStack(&stk);
        if(i) printf("初始化成功
    ");
        else printf("初始化失败
    ");
    
        Push(&stk, 1);
        Push(&stk, 2);
        Push(&stk, 3);
    
        for(i = 0;i < 3;i++)
            printf("%d
    ", stk.data[i]);
    
        printf("
    ");
    }
  • 相关阅读:
    redis改配置
    redis主从复制
    nginx做维护页面
    go_http
    DRF源码-views.py
    DRF教程10-关系字段
    语言特性
    DRF源码-fields.py
    python_@classmethod
    HTML5:定位
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/7363218.html
Copyright © 2011-2022 走看看