I am still learning the C language. I have a problem to simulate a simple stack function, such as push, pop and so on. I found that the date
and next
are not initialized in the PTAIL. At that time, the program end. Is that counting as a memory leak?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct Node{
int date;
struct Node * next;
}Node,*PNode;
typedef struct Stack{
PNode pHead;
PNode pTail;
}Stack;
void init(Stack *pS){
PNode n=(PNode)malloc(sizeof(Node));
pS->pHead=n;
pS->pTail=n;
n->next=NULL;
}
void push(Stack *pS,int val){
PNode p=(PNode)malloc(sizeof(Node));
p->date=val;
p->next=pS->pHead;
pS->pHead=p;
}
void travel(Stack *pS){
PNode p=pS->pHead;
while(p->next){
printf("%d ",p->date);
p=p->next;
}
printf("\n");
}
int main(void){
Stack s;
init(&s);
push(&s,1);
travel(&s);
push(&s,1);
travel(&s);
system("pause");
return 0;
}