您的位置:首页 > 其它

用栈实现逆序输出键盘输入

2009-08-17 15:38 246 查看
#include"stdlib.h"
#include"stdio.h"

typedef char StackEntry;
typedef struct node {    //链栈的结点结构
StackEntry item;  //栈的数据元素类型
struct node *next;  //指向后继结点的指针
}NODE;
typedef struct stack{
NODE *top;
}STACK;
//初始化栈
void InitStack(STACK *S)
{
S->top=NULL;
}
//入栈
void Push(STACK *S,StackEntry item)
{
NODE* p;

p=(NODE*)malloc(sizeof(NODE));
if (!p)
exit(0);
else {
p->item=item;
p->next=S->top;
S->top=p;
}
}
//是否为空栈
int StackEmpty(STACK S)
{
if (S.top==NULL) return true;
else return false;
}

//出栈
void Pop(STACK*S, StackEntry *item)
{
NODE *p;

if (StackEmpty(*S))
exit(0);
else {
*item=S->top->item;
p=S->top;
S->top=p->next; free(p);
}
}

void ReverseRead( )
{
STACK S;                //定义一个栈结构S
char ch;
InitStack(&S);             //初始化栈
while ((ch=getchar())!='\n')
//从键盘输入字符,直到输入换行符为止
Push(&S ,ch);
//将输入的每个字符入栈
while (!StackEmpty(S)) {
//依次退栈并输出退出的字符
Pop(&S,&ch);
putchar(ch);
}
putchar('\n');
}
void main(){
ReverseRead();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐