• 数据结构与算法(三)栈的顺序存储


    • 接口
    #ifndef __STACK_ARRAY_H__ 
    #define __STACK_ARRAY_H__ 
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <stdbool.h> 
    typedef int ElementType; 
    #define MAXSIZE 100 
    struct SNode 
    { 
       ElementType Data[MAXSIZE]; 
       int Top; 
    }; 
    typedef struct SNode* Stack; 
    
    
    Stack CreateStack();// 
    bool IsFull(Stack S);// 
    bool IsEmpty(Stack S);// 
    void Push(Stack S, ElementType item);// 
    ElementType Pop(Stack S);// 
    void Display(Stack S); 
    
    
    #endif
    • 函数实现

     

     

     

    #include "StackArray.h" 
    Stack CreateStack() 
    { 
       Stack temp = (Stack)malloc(sizeof(struct SNode)); 
       temp->Top = -1; 
       return temp; 
    } 
    bool IsFull(Stack S) 
    { 
       if (S->Top == MAXSIZE - 1){ 
           return true; 
       } 
       return false; 
    }

    bool IsEmpty(Stack S)
    { 
       if (S->Top == -1){ 
           return true; 
       } 
       return false; 
    } 
    void Push(Stack S, ElementType item) 
    { 
       if (IsFull(S,100)){ 
           printf("stack is full.\n"); 
           return; 
       } 
       S->Data[++(S->Top)] = item; 
    } 
    ElementType Pop(Stack S) 
    { 
       if (IsEmpty(S)) { 
           printf("stack is empty.\n"); 
           return 0; 
       } 
       return S->Data[(S->Top)--]; 
    } 
    void Display(Stack S) 
    { 
       while (!IsEmpty(S)) 
       { 
           printf("%d ", Pop(S)); 
       } 
       printf("\n"); 
    }

     

     

    • 测试函数

     

    int main(int argc, char**argv) 
    { 
       Stack myStack = CreateStack(100); 
       for (int i = 0; i < 10; i++){ 
           Push(myStack, i); 
       } 
       Display(myStack); 
       return 0; 
    }

     

     

    • 测试结果

  • 相关阅读:
    初识线段树
    win7下vc6.0打开文件未响应的解决方法
    SQL 2008安装
    20200730 Div.2模拟赛题解
    20190928 Div3模拟赛题解
    Codeforces Round #664 (Div. 2) 题解
    CSP2019回忆录
    [SHOI2014]三叉神经树(加强版)题解
    Linux 下修改Swap区设置
    LoadRunner监控Linux与Windows方法
  • 原文地址:https://www.cnblogs.com/wuyouxiaocai/p/15640631.html
Copyright © 2020-2023  润新知