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; 
    }
    • 测试结果

  • 相关阅读:
    定时器
    自定义个性化 EditPeople控件
    infopath 2010 调试.
    MOSS 查询
    网站项目建设流程概述
    跨站点显示列表数据 ListViewWebPart
    VIM记事——大小写转换
    事务码记录 程序优化常用st12
    SAP各模块字段与表的对应关系
    固定资产一览
  • 原文地址:https://www.cnblogs.com/wuyouxiaocai/p/15640635.html
Copyright © 2011-2022 走看看