关闭

链栈2

80人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <Stdlib.h>
typedef struct node
{
    int data;
    struct node *pNext;
}NODE,*PNODE;
typedef struct Stack
{
    PNODE pTop;
    PNODE pBottom;
}STACK,* PSTACK;
void init(PSTACK pS)
{
    pS->pTop=(PNODE)malloc(sizeof(NODE));
    if(!pS->pTop)
        exit(0);
    pS->pBottom=pS->pTop;
    pS->pTop->pNext=NULL;
}
void push(PSTACK pS,int val)
{
    PNODE pNew=(PNODE)malloc(sizeof(NODE));
    pNew->data=val;
    pNew->pNext=pS->pTop;
    pS->pTop=pNew;
}
void traverse(PSTACK pS)
{
    PNODE p=pS->pTop;
    while(p!=pS->pBottom)
    {
        printf("%d ",p->data);
        p = p->pNext;

    }
    printf("\n");
}
int empty(PSTACK pS)
{
    if (pS->pTop==pS->pBottom)
    {
        return 0;
    }
    else
        return 1;
}
int pop(PSTACK pS,int * pVal)
{
    if(empty(pS)==0)
        return 0;
    else
    {// return true;

        PNODE r  =pS->pTop;
        *pVal = r->data;
        pS->pTop=r->pNext;
        free(r);
        r=NULL;
        return 1;
    }
}
int main()
{
    STACK S;
    int val;
    init(&S);
    push(&S,1);
    push(&S,2);
    push(&S,23);
    push(&S,43);
    push(&S,9);
    pop(&S,&val);
    traverse(&S);
    if (pop(&S,&val)==1)
    {
        /* code */
        printf("success! %d\n",val);
    }
    else
        printf("FAil\n");
    traverse(&S);





    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4653次
    • 积分:342
    • 等级:
    • 排名:千里之外
    • 原创:30篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类