内容发布更新时间 : 2025/6/23 8:12:28星期一 下面是文章的全部内容请认真阅读。
//栈的出栈
int pop(struct Stack * s) { if(s->top != -1) return s->data[s->top--] ; else { printf(\ return -1 ; } }
//栈的入栈
int push(struct Stack *s , int n) { if(MAX -1 != s->top) return s->data[++s->top] = n ; else { printf(\ return -1 ; } }
int main() {
struct Stack s ; char str[MAX] ; int len , i ;
s.top = -1 ;
scanf(\ len = strlen(str) ;
for(i = 0 ; i < len / 2 ; ++i) push(&s , str[i]) ; if(len % 2) ++i ;
for( ; i < len ; ++i) if(pop(&s) != str[i]) break ; if(i == len) printf(\是回文数\ else printf(\不是回文数\ return 0 ; } 4.
Push(1) Pop(1) Push(2) Pop(2) Push(3) Pop(3) 此时输出序列为1 2 3
Push(1) push(2) pop(2) pop(1) push(3) pop(3) 此时输出序列为2 1 3
Push(1) push(2)pop(2) push(3) pop(3) pop(2) pop(3) 此时输出序列为2 3 1 Push(1) push(2) push(3) pop(3) pop(2) pop(1) 此时输出为3 2 1 算法实现就是按上述步骤进行,就省略了 5.
#include
#define MAX 100
struct Stack {
char data[MAX] ; int top ; };
//栈的出栈
int pop(struct Stack * s) { if(s->top != -1) return s->data[s->top--] ; else { printf(\ return -1 ; } }
//栈的入栈
int push(struct Stack *s , int n) { if(MAX -1 != s->top) return s->data[++s->top] = n ; else { printf(\ return -1 ; } }
int main() {
struct Stack s ; char str[MAX] ; int i ; char ch ;
s.top = -1 ;
scanf(\
for(i = 0 ; str[i] != '\\0' ; ++i) { if('(' == str[i]) push(&s , str[i]) ; else if(')' == str[i]) {
}
if(-1 == s.top) break ; ch = pop(&s) ; if(ch != '(') break ;