为什么代码没有正常输出?
#include
#include
#include "stack.h"
int main()
{
Stack S;
int isEmpty;
printf("##########栈操作########\n");
S = CreateStack();
printf("输出栈中元素\n");
return 0;
}
下面是stack.h代码
#ifndef _STACK_H
#define _STACK_H
typedef int ElementType;
struct StackNode;
typedef struct StackNode *PtrToStackNode;
typedef PtrToStackNode Stack;
int IsEmpty( Stack S );
Stack CreateStack( void );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push( ElementType E, Stack S );
ElementType Top( Stack S );
void Pop( Stack S );
/*实现栈---带特殊头结点的链表*/
struct StackNode
{
ElementType Element;
PtrToStackNode Next;
};
#endif //LINKEDLIST_STACK_H
int IsEmpty( Stack S )
{
return S->Next == NULL;
}
Stack CreateStack( void )
{
Stack S;
printf("What do you echo?\n");
S = malloc( sizeof( struct StackNode ) );
if( S == NULL )
exit(1);
MakeEmpty( S );
printf("\nIs something wrong here?\n");
return S;
}
void MakeEmpty( Stack S )
{
printf("\nMakeEmpty Stack\n");
int tmp;
if( S == NULL )
{
printf("S == NULL\n");
exit(1);
}
else
{
tmp = IsEmpty(S);
printf("tmp is%d\n",tmp);
while( !IsEmpty( S ) ){
Pop( S );
}
}
}
void Pop( Stack S )
{
PtrToStackNode FirstCell;
printf("Pop Stack\n");
if( IsEmpty( S ) )
{
printf("Pop Empty Stack\n");
exit(0);
}
else
{
printf("free firstCell\n");
FirstCell = S->Next;
S->Next = S->Next->Next;
free( FirstCell );
}
}
void Push( ElementType E, Stack S )
{
PtrToStackNode TmpCell;
TmpCell = malloc( sizeof( struct StackNode ) );
if( TmpCell == NULL )
exit(0);
else
{
TmpCell->Element = E;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
void DisplayStack( Stack S )
{
if( S == NULL )
exit(0);
else
{
while( S->Next != NULL){
printf("echo something");
printf("***%d***",S->Element);
S = S->Next;
}
}
}
程序执行时输出
debug时初始化的Stack是空的,为什么程序跑起来的时候就不为空了呢?
为什么“输出栈中元素”这个字符串没有输出啊!
望指教!