链式栈

#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 main()
{
    STACK S;
    init(&S);
    push(&S,1);
    push(&S,2);
    push(&S,32);

    push(&S,324);

    push(&S,43);

    traverse(&S);




    return 0;
}

劳资总算明白了

阅读更多
个人分类: 日常练习 C语言
上一篇单链表的程序实现
下一篇链栈2
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭