关于数据结构释放栈的问题


#include 
#include 
typedef struct SLinkNode
{
    int data;
    struct SLinknode* next;
    struct SLinkNode* head;
}Node,*Stack;
Stack initStack(Stack S)
{
    S->head=(Node*)malloc(sizeof(Node));
    S->head->next=NULL;
}
Stack push(Stack S,int e)
{
    Node* p=S->head->next;
    Node* s=(Stack)malloc(sizeof(Node));
    s->data=e;
    s->next=p;
    S->head->next=s;
}
int pop(Stack S)
{
    Node* p=S->head;
    Node* q=p->next;
    if(q)
    {
        int e=q->data;
        p->next=q->next;
        free(q);
        return e;
    }
}
void conversion(Stack S)
{
    initStack(S);
    int N;
    scanf("%d",&N);
    if(N<=0)
    {
        scanf("%d",&N);
    }
    while(N)
    {
        push(S,N%8);
        N=N/8;
    }
    int e;
    while(S->head->next!=NULL)
    {
        e=pop(S);
        printf("%d",e);
    }
}
int main()
{
    Stack S;
    conversion(&S);
    return 0;
}
#include 
#include 
typedef struct SLinkNode
{
    int data;
    struct SLinknode* next;
    struct SLinkNode* head;
}Node,Stack;
Stack initStack(Stack* S)
{
    S->head=(Node*)malloc(sizeof(Node));
    S->head->next=NULL;
    S->next=NULL;
}
Stack push(Stack* S,int e)
{
    Node* p=S->head->next;
    Node* s=(Node*)malloc(sizeof(Node));
    s->data=e;
    s->next=p;
    S->head->next=s;
}
int pop(Stack* S)
{
    Node* p=S->head;
    Node* q=p->next;
    if(q)
    {
        int e=q->data;
        p->next=q->next;
        free(q);
        return e;
    }
}
void conversion(Stack* S)
{
    int N;
    scanf("%d",&N);
    while(N<=0)
    {
        scanf("%d",&N);
    }
    while(N)
    {
        push(S,N%8);
        N=N/8;
    }
    int e;
    while(S->head->next!=NULL)
    {
        e=pop(S);
        printf("%d",e);
    }
}
int main()
{
    Stack S;
    initStack(&S);
    conversion(&S);
    free(S.head);
    return 0;
}


第一个是我自己写的,第二个是应该是teacher拿我的改的,想问一下我的问题在哪,为什么要改成后面这种,teacher说我要释放栈,不然内存泄漏,然后就发给了后面的这个程序。

free(S.head); 多了这句话, 你的应该head没有释放。 S->head 是单独在init的时候申请了,push和pop是一对malloc和free