zoukankan      html  css  js  c++  java
  • 数据结构与算法(四)栈的链式存储

    • 接口
    #ifndef __STACK_LIST_H__ 
    #define __STACK_LIST_H__ 
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <stdbool.h> 
    typedef int ElementType; 
    typedef struct SNode 
    { 
       ElementType Data; 
       struct SNode*Next; 
    }SNode; 
    typedef SNode* Stack; 
    
    
    Stack CreateStack();// 
    bool IsEmpty(Stack S);// 
    void Push(Stack S, ElementType item);// 
    ElementType Pop(Stack S);// 
    void Display(Stack S); 
    #endif
    • 函数实现
    #include "StackList.h" 
    Stack CreateStack() 
    { 
       Stack temp = (Stack)malloc(sizeof(struct SNode)); 
       temp->Next = NULL; 
       return temp; 
    } 
    bool IsEmpty(Stack S) 
    { 
       return (S->Next == NULL); 
    } 
    void Push(Stack S, ElementType item) 
    { 
       Stack temp = (Stack)malloc(sizeof(struct SNode)); 
       temp->Data = item; 
       temp->Next = S->Next; 
       S->Next = temp; 
    } 
    ElementType Pop(Stack S) 
    { 
       Stack firstCell; 
       ElementType topItem; 
       if (IsEmpty(S)) { 
           return 0; 
       } 
       firstCell = S->Next; 
       S->Next = firstCell->Next; 
       topItem = firstCell->Data; 
       free(firstCell); 
       return topItem; 
    } 
    void Display(Stack S) 
    { 
       while (!IsEmpty(S)) { 
           printf("%d ", Pop(S)); 
       } 
       printf("\n"); 
    }
    • 测试函数
    int main(int argc, char**argv) 
    { 
       Stack myStack = CreateStack(); 
       for (int i = 0; i < 10; i++) { 
           Push(myStack, i); 
       } 
       Display(myStack); 
       return 0; 
    }
    • 测试结果

  • 相关阅读:
    正则表达式
    npm 和package.json 文件
    React Element /组件/JSX
    Express 入门
    vue-router 基本使用
    content_form.class.php文件不完整 解决方案
    Yii查询count()
    android之在view中内嵌浏览器的方法
    YII中利用urlManager将URL改写成restful风格
    关于YII中layout中的布局和view中数据的关系
  • 原文地址:https://www.cnblogs.com/wuyouxiaocai/p/15640635.html
Copyright © 2011-2022 走看看