zoukankan      html  css  js  c++  java
  • 技术学习博1

    补个档,第八周学的栈

     1 //顺序栈的定义,老师给的
     2 typedef char DataType;//栈中的元素类型为char
     3 typedef struct LNode
     4 {
     5     DataType elem[MAX];
     6     int top;//栈顶位置
     7 }SeqStack;
     8 
     9 //基本操作
    10 //栈的初始化
    11 void initStack (SeqStack *S)
    12 {
    13     S->top=-1;
    14 }
    15 //进栈
    16 int push(SeqStack *S, DataType x)
    17 {
    18     if(S->top==MAX-1)
    19     {    
    20         printf("full
    ");
    21         return ERROR;
    22     }
    23     S->top++;
    24     S->elem[S->top]=x;
    25     return OK;
    26 }
    27 //出栈
    28 int pop(SeqStack *S, DataType *x)
    29 {
    30     if(S->top==-1)
    31     {
    32         printf("empty
    ");
    33         return ERROR;
    34     }
    35     *x=S->elem[S->top];
    36     S->top--;
    37     return OK;
    38 }
    39 //取栈顶元素
    40 int getTop(SeqStack *S, DataType *x)
    41 {
    42     if(S->top==-1)
    43     {
    44         printf("empty
    ");
    45         return ERROR;
    46     }
    47     *x=S->elem[S->top];
    48     return OK;
    49 }

    然后是判断回文的

     1 void huiwen()
     2 {
     3     char x[10],c[10],*t;
     4     int i,hhw,n=0;
     5     SeqStack S;
     6     initStack(&S);//建空栈
     7     scanf("%s",x);
     8     t=x;
     9     while(x[n]!='@')//以@结束
    10     {    
    11         if(x[n]=='&')//跳过&
    12             n++;
    13         else
    14             push(&S,x[n++]);//进栈
    15     }
    16     for(i=0;i<((n-1)/2);i++)
    17         pop(&S,&c[i]);//出栈
    18 
    19     /*printf("
    ");//测试用
    20     for(i=0;i<((n-1)/2);i++)
    21         printf("%c",c[i]);
    22     printf("
    ");
    23     printf("%s
    ",t);*/
    24 
    25     for(i=0;i<((n-1)/2);i++,t++)
    26     {
    27         if(c[i]!=*t)//比较
    28         {
    29             hhw=0;
    30             break;
    31         }
    32         else
    33             hhw=1;
    34     }
    35     if(hhw)
    36         printf("它是回文
    ");
    37     else
    38         printf("它不是回文
    ");
    39 
    40 }

    void main(){printf("请输入一段字符串: ");huiwen();printf("请输入一段字符串: ");huiwen();}

  • 相关阅读:
    ionic platform add ios, Error:spawn EACCES
    OC中分类(Category)和扩展(Extension)
    JSON.stringify() 格式化 输出log
    JavaScript 闭包
    vue路由跳转到指定页面
    vue使用路由跳转到上一页
    vue子传父多个值
    vue里router-link标签设置动态路由的3个方法
    地址栏的路由输入不匹配时候,设置默认跳转页面(redirect)
    把router-link标签渲染成指定的标签
  • 原文地址:https://www.cnblogs.com/hhw-/p/12863425.html
Copyright © 2011-2022 走看看