69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#define StackSize 10
typedef struct{
int data[StackSize];
int top;
}SeqStack;
void InitStack(SeqStack *S)
{
S->top=-1;
}
int StackEmpty(SeqStack *S)
{
return S->top==-1;
}
int StackFull(SeqStack *S)
{
return S->top==StackSize-1;
}
void Push(SeqStack *S,int x)
{
if(StackFull(S))
printf("Stack overflow");
S->data[++S->top]=x;//栈顶指针加1后将x入栈
}
char Pop(SeqStack *S)
{
if(StackEmpty(S))
printf("Stack underflow");
return S->data[S->top--];//栈顶元素返回后将栈顶指针减1
}
SeqStack ReverseString(SeqStack *S) //逆转函数
{
int i,A[StackSize];
SeqStack *K;
InitStack(K);
for(i=0;!StackEmpty(S);i++) //{Push(K,Pop(S));}
{A[i]=Pop(S);}
for(i=0;A[i]!='\0';i++){Push(S,A[i]);}
//return *K;
}
void PrintStack(SeqStack *S)
{
int i;
printf("栈中元素为:");
for(i=S->top;i>=0;--i)
printf("%d ",S->data[i]);
printf("\n");
}
int main()
{
SeqStack *Stack;
char ch;
printf("ENTER");
InitStack(Stack);
while((ch=getchar())!='\n'){Push(Stack,ch);}
PrintStack(Stack);
ReverseString(Stack);
PrintStack(Stack);
system("pause");
return 0;
}
char ch;
printf("ENTER");
InitStack(Stack);
while((ch=getchar())!='\n'){Push(Stack,ch);}
我猜你是控制台输入"ENTER" 之后敲了一个回车键,
这会被第4行 ch = getchar() 读入,这样while() 循环结束了.....