c语言is stack empty,C语言实现栈的问题

为什么代码没有正常输出?

#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;

}

}

}

程序执行时输出

bVNK6B?w=259&h=198

debug时初始化的Stack是空的,为什么程序跑起来的时候就不为空了呢?

为什么“输出栈中元素”这个字符串没有输出啊!

望指教!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值