栈的一般操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int Elem;
struct Node
{
    Elem data;
    struct Node *pNext;
};

typedef struct Stack
{
    Node *pTop;
    Node *pBottom;
    int iLength;
}Stack;

/*
*   @creat      初始化栈
*   @push       压栈
*   @pop        出栈
*   @destory     销毁栈
*/
void creat(Stack *sta);
void push(Stack *sta, Elem data);
int pop(Stack *sta);
void destory(Stack *sta);

void print(Stack *sta);

int main()
{
    Stack sta;
    creat(&sta);

    int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    for(int i(0); i<9; i++)
    {
        push(&sta, arr[i]);
    }
    printf("原栈:\n");
    print(&sta);
    
    printf("--------------------\n");
    
    printf("990进栈:\n");
    push(&sta, 990);
    print(&sta);
    
    printf("出栈:\n");
    pop(&sta);
    print(&sta);
    return 0;
}

void creat(Stack *sta)
{
    sta->pTop = (Node*)malloc(sizeof(Node));    //初始化栈
    sta->pBottom = sta->pTop;
    sta->pBottom->pNext = NULL;
    sta->iLength = 0;
}
void push(Stack *sta, Elem data)
{
    Node *pTmp  = (Node*)malloc(sizeof(Node));   //新建节点
    pTmp->data  = data;
    pTmp->pNext = sta->pTop->pNext;              //新建节点放在原来栈顶的上面
    sta->pTop->pNext = pTmp;                     //更新栈顶

    sta->iLength++;
}
Elem pop(Stack *sta)
{
    Node *pTmp = sta->pTop->pNext;               //暂时保存栈顶
    sta->pTop->pNext = pTmp->pNext;              //更新栈顶

    Elem data = pTmp->data;

    free(pTmp);
    sta->iLength--;

    return data;
}

void destory(Stack *sta)
{
    Node *pCursor = sta->pTop->pNext;
    free(sta->pTop);
    while(pCursor)
    {
        Node *pTmp = pCursor->pNext;
        free(pTmp);
        pCursor = pTmp->pNext;
    }
}

void print(Stack *sta)
{
    Node *pCursor = sta->pTop->pNext;
    while(pCursor)
    {
        if(pCursor->pNext)
            printf("%d ", pCursor->data);
        else
            printf("%d\n", pCursor->data);
        pCursor = pCursor->pNext;
    }
}

运行结果