请教大佬 为什么出栈后的结果是6487612
#include<stdio.h>#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);//初始化
void push(PSTACK pS,int val);//压栈
void traverse(PSTACK);//遍历输出
bool empty(PSTACK pS);//判断栈是否为空
bool pop(PSTACK,int *); //出栈
void clear(PSTACK pS);//清空栈
int main(void){
STACK S;
int val=0;
init(&S); //完成初始化 ,目的就是造出一个空栈
push(&S,1);//压栈
push(&S,2);
push(&S,3);
push(&S,4);
push(&S,5);
push(&S,6);
traverse(&S);//遍历输出
if(pop(&S,&val)){
printf("出栈成功,出栈的数是%d\n",&val);
}
else{
printf("出栈失败!\n");
}
traverse(&S);//遍历输出
clear(&S);
traverse(&S);//遍历输出
return 0;
}
//初始化
void init(PSTACK pS){
pS->pTop=(PNODE)malloc(sizeof(NODE));
if(NULL==pS->pTop){
printf("动态内存分配失败!\n");
exit(-1);
}
else{
pS->pBottom=pS->pTop;
pS->pTop->pNext=NULL;
}
}
//压栈
void push(PSTACK pS,int val){
PNODE pNew=(PNODE)malloc(sizeof(NODE));//动态造出一个新的结点
pNew->data=val;//把val放到新结点pNew的数据域中
pNew->pNext=pS->pTop; //不能改成pS->pBottom
pS->pTop=pNew; //使pS->pTop指向新的结点pNew
return;
}
//遍历输出
void traverse(PSTACK pS){
PNODE p=pS->pTop;
while(p!=pS->pBottom){
printf("%d\t",p->data);
p=p->pNext;
}
printf("\n");
return;
}
//判断栈是否为空
bool empty(PSTACK pS){
if(pS->pTop==pS->pBottom){
return true;
}
else{
return false;
}
}
//出栈
bool pop(PSTACK pS,int * pVal){
if(empty(pS)){
return false;
}
else{
PNODE r=pS->pTop;
*pVal=r->data;
pS->pTop=r->pNext;
free(r);
r=NULL;
return true;
}
}
//清空栈
void clear(PSTACK pS){
if(empty(pS)){
return;
}
else{
PNODE p=pS->pTop;
PNODE q=NULL;
while(p!=pS->pBottom){
q=p->pNext;
free(p);
p=q;
}
pS->pTop=pS->pBottom;
}
}