C语言练习stack

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("\n");
}

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;
}
C语言练习stack

C语言练习stack,布布扣,bubuko.com

C语言练习stack

上一篇:解决相册/拍照文件上传问题


下一篇:【PHP数据结构】队列的相关逻辑操作