链式栈

#include "stdio.h"
#include "malloc.h"
typedef struct Node
{
    int info;
    Node *link;
}*PNode;
typedef struct LinkStack
{
    PNode top;
}*PLinkStack;
PLinkStack createEmpty_link()
{
    PLinkStack plastack;
    plastack=(PLinkStack)malloc(sizeof(LinkStack));
    if(plastack!=NULL)
        plastack->top=NULL;
    else
        printf("Out of space!\n");
    return plastack;
}
int isEmptyStack_link(PLinkStack plastack)
{
    return (plastack->top==NULL);
}
void push_link(PLinkStack plastack,int x)
{
    PNode p;
    p=(PNode)malloc(sizeof(Node));
    if(p==NULL)
        printf("Out of space");
    else
    {
        p->info=x;
        p->link=plastack->top;
        plastack->top=p;
    }
}
void pop_link(PLinkStack plastack)
{
    PNode p;
    if(isEmptyStack_link(plastack))
        printf("Empty stack pop.\n");
    else
    {
        p=plastack->top;
        plastack->top=plastack->top->link;
        free(p);
    }
}
int top_link(PLinkStack plastack)
{
    return (plastack->top->info);
}
int main()
{
    PLinkStack plastack;
    int N;
    plastack=createEmpty_link();
    printf("Please input number to N!\n");
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        int x;
        printf("Please input number to x!\n");
        scanf("%d",&x);
        push_link(plastack,x);
    }
    printf("The top number is %d",top_link(plastack));
}

你可能感兴趣的:(栈)