zoukankan      html  css  js  c++  java
  • 链栈的C语言实现

        /*
        功能:栈的链表实现
        Author:lxm
        Date: 20160511
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ELEMTYPE int
    #define STACK_EMPTY -9999
    #define N 10
    
    typedef struct Node
    {
        ELEMTYPE data;
        struct Node * next;
    }LNode;
    
    void initStack(LNode **S);
    int isStackEmpty(LNode **S);
    void push(LNode **S,ELEMTYPE e);
    ELEMTYPE pop(LNode **S);
    void printStack(LNode **S);
    
    int main()
    {
    
        LNode *LS;
        initStack(&LS);
    
        int i;
        for(i=1;i<=N;++i)
        {
                push(&LS,i);
        }
        printStack(&LS);
        LNode *p = LS->next;
        /*while(p!=NULL)
        {
            printf("%d	",p->data);
            p = p->next;
        }
        */
        return 0;
    }
    
    
    void initStack(LNode **S)
    {
            (*S) = (LNode*)malloc(sizeof(LNode));
            (*S)->next = NULL;
    }
    
    int isStackEmpty(LNode **S)
    {
    
       return ((*S)->next == NULL);
    
    }
    
    void push(LNode **S,ELEMTYPE e)
    {
    
        LNode *p = (LNode*)malloc(sizeof(LNode));
        p->data = e;
    
        p->next =  (*S)->next;
         (*S)->next = p;
    }
    
    ELEMTYPE pop(LNode **S)
    {
        if(isStackEmpty(S)) return STACK_EMPTY;
    
        LNode *p = (*S)->next;
         (*S)->next = p->next;
        ELEMTYPE result = p->data;
        free(p);
        p = NULL;
        return result;
    }
    
    void printStack(LNode **S)
    {
       while(!isStackEmpty(S))
        {
            printf("%d	",pop(S));
        }
    }
    
  • 相关阅读:
    Redis之数据类型大全
    基于服务的SOA架构
    mybatis_个人总结
    mybatis_开发篇
    mybatis_基础篇
    mybatis_常用标签
    mybatis_映射查询
    solr_架构案例【京东站内搜索】(附程序源代码)
    Solr_全文检索引擎系统
    zabbix监控搭建步骤
  • 原文地址:https://www.cnblogs.com/yldf/p/11900172.html
Copyright © 2011-2022 走看看