zoukankan      html  css  js  c++  java
  • C语言练习stack

    #define _CRT_SECURE_NO_DEPRECATE
    #include<stdio.h>
    #include<stdlib.h>
    #define datatype int
    
    typedef struct node{
        datatype data;
        struct node* next;
    }STACK;
    
    //initial the stack
    STACK* Init(STACK* s){
        s = NULL;
        return s;
    }
    
    //push in stack
    STACK* Push(STACK* s, datatype d){
        STACK *p = (STACK*)malloc(sizeof(STACK));
        p->data = d;
        p->next = s;
        s = p;
        return s;
    }
    
    //print the stack
    void Print(STACK* s){
        STACK *p = s;
        while (p){
            printf("%4d", p->data);
            p = p->next;
        }
        printf("
    ");
    }
    
    void Free(STACK* s){
        STACK *p = s;
        while (p){
            s = p;
            p = p->next;
            free(s);
        }
    }
    
    int main()
    {
        int d;
        STACK *stk = NULL;
    
        stk = Init(stk);
        while (1){
            scanf("%d", &d);
            if (d == 0)break;
            stk = Push(stk, d);
        }
        Print(stk);
        Free(stk);
        system("pause");
        return 0;
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    02-模板字符串
    01-学习vue前的准备工作
    21-z-index
    20-定位
    19-background
    18-超链接导航栏案例
    17-文本属性和字体属性
    16-margin的用法
    jBPM
    Table of Contents
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3581258.html
Copyright © 2011-2022 走看看