栈的链表实现

//  栈的链表实现
#include <cstdio>
#include <cstdlib>
//#define _OJ_

typedef struct Lnode
{
    int data;
    struct Lnode *next;
} Lnode, *stack;

stack
creat_list(stack s)
{
    s = (stack) malloc (sizeof(Lnode));
    s->next = NULL;
}

int
isEmpty(stack s)
{
    if(s->next == NULL)
    return 1;
    else return 0;
}

void
push(int x,stack s)
{
    stack p;
    p = (stack) malloc (sizeof(Lnode));
    p->data = x;
    p->next = s->next;
    s->next = p;
    printf("%d\n", x);
}

int
pop(stack s)
{
    int e;
    stack p;
    p = s->next;
    e = p->data;
    s->next = p->next;
    free(p);
    return e;
}


int main(int argc, char const *argv[]) {
#ifndef _OJ_  //ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif

    int i, n, x;
    stack s;
    s = creat_list(s);
    printf("empty == %d\n", isEmpty(s));
    scanf("%d", &n);
    for(i = 0;i < n; i++) {
    scanf("%d", &x);
    push(x,s);
     }

    for(i = 0;i < n; i++) {
    printf("pop == %d\n", pop(s));
     }


    return 0;
}




posted @ 2015-10-28 20:17  别笑  阅读(108)  评论(0编辑  收藏  举报