#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; //定义一个栈结构Schar ch;InitStack(&S); //初始化栈while ((ch=getchar())!='/n') //从键盘输入字符,直到输入换行符为止Push(&S ,ch); //将输入的每个字符入栈while (!StackEmpty(S)) { //依次退栈并输出退出的字符Pop(&S,&ch);putchar(ch);}putchar('/n');}void main(){ReverseRead();}
posted on 2009-08-17 15:38 Hibernate4 阅读(407) 评论(0) 编辑 收藏 举报