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("
    ");
    }
  • 相关阅读:
    iOS开发之窗口和视图
    GCD
    禁止非法用户登录综合设置
    大数减法(C++实现)
    大数加法(C++实现)
    迷宫问题 (BFS ➕输出路径)
    Pots (BFS ➕ 输出路径)
    Shuffle'm Up (map ➕ BFS)
    Prime Path (BFS)
    速算24点
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/7363218.html
Copyright © 2011-2022 走看看