1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAX 100 4 5 typedef struct 6 { 7 char c[MAX]; 8 int top; 9 }*seqstack; 10 11 void InitStack(seqstack s);//建立一个空栈 12 int push(seqstack s,char a);//进栈 13 int pop(seqstack s,char *a);//出栈 14 15 int main() 16 { 17 seqstack s; 18 s=(seqstack)malloc(sizeof(seqstack)); 19 InitStack(s); 20 char a; 21 a=getchar(); 22 while(a!=‘\n‘) 23 { 24 push(s,a); 25 a=getchar(); 26 } 27 while(pop(s,&a)) 28 putchar(a); 29 printf("\n"); 30 return 0; 31 } 32 33 void InitStack(seqstack s)//建立一个空栈 34 { 35 s->top=-1; 36 } 37 38 int push(seqstack s, char a)//进栈 39 { 40 if(s->top==MAX-1) 41 return 0; 42 s->top++; 43 s->c[s->top]=a; 44 return 1; 45 } 46 47 int pop(seqstack s,char *a)//出栈 48 { 49 if(s->top==-1) 50 return 0; 51 else 52 { 53 *a=s->c[s->top]; 54 s->top--; 55 return 1; 56 } 57 }
时间: 2024-09-30 23:27:50