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;
}
}
}
程序執行時輸出
debug時初始化的Stack是空的,為什么程序跑起來的時候就不為空了呢?
為什么“輸出棧中元素”這個字符串沒有輸出啊!
望指教!
總結
以上是生活随笔為你收集整理的c语言is stack empty,C语言实现栈的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言一串大写字母转小写,C语言的基础函
- 下一篇: mysql查询单词出现的位置_在MySQ