zoukankan      html  css  js  c++  java
  • 栈的应用实例

    十进制转二进制

    十进制通过除而取余数得到的二进制,最后需要倒过来展示。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define ERROR 0
    #define MAX 100
    
    typedef int ElemType;
    typedef int Status;
    
    typedef struct{
       ElemType * base;
       ElemType * top;
    } Stack;
    
    
    Status initStack(Stack *s){
      s->base = (ElemType*)malloc(MAX * sizeof(ElemType));
      s->top = s->base;
      return OK;
    }
    
    Status Push(Stack *s,ElemType e){
      if( s->top - s->base == MAX )return ERROR;
      *s->top++ = e;
      return OK;
    }
    
    Status Pop(Stack *s,ElemType *e){
      if( s->top - s->base == 0 )return ERROR;
      *e = *(--s->top);
      return OK;
    }
    
    Status convertion(int val){
      int popvalue;
      Stack s;
      initStack(&s);
    
      do{
        Push(&s,val%2);
      }while(val/=2);
    
      while(s.top!=s.base){
        Pop(&s,&popvalue);
        printf("%d",popvalue);
      }
     
      printf("
    ");
      return OK;
    }
    
    int main(){
      int value;
      
      printf("This is binary convertioner
    ");
      printf("enter Ctrl + C to stop programe
    ");
      while(1){
        printf("enter a value:");
        scanf("%d",&value);
        convertion(value);
      }
    }
    
    
    /*
    8 / 2 = 4 ------ 0  push first
    4 / 2 = 2 ------ 0  push second
    2 / 2 = 1 ------ 0  push third
    1 / 2 = 0 ------ 1  push fourth
    */
    
    /*
    1  pop first
    0  pop second
    0  pop third
    0  pop fourth
    */

    判断是否为回文

    回文是指无论是正读,还是倒读都是一样的。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    #define ERROR 0
    #define MAX 100
    
    typedef char ElemType;
    typedef int Status;
    typedef struct{
      ElemType * base;
      ElemType * top;
    } Stack;
    
    Status initStack(Stack * s){
      s->base = (ElemType*)malloc(MAX * sizeof(ElemType));
      s->top  = s->base;
      return OK;
    }
    
    Status Push(Stack *s,ElemType e){
      if(s->top - s->base == MAX)return ERROR;
      *(s->top++)=e;
      return OK;
    }
    
    Status Pop(Stack *s,ElemType *e){
      if(s->top - s->base == 0)return ERROR;
      *e = *(--s->top);
      return OK;
    }
    
    Status checkPalindrome(char * str){
      char c;
      Stack s;
      char * p = str;
      initStack(&s);
    
      while( *p != '
    ')
        Push(&s,*p++);
    
      while(s.top != s.base){
        Pop(&s,&c);
        if(c != *str++)return ERROR;
      }
    
      return OK;
    }
    
    int main(){
      char text[100];
    
      printf("This is check text if it is palindarome
    ");
      printf("enter the Ctrl + C to exit progrme
    ");
      while(1){
        printf("enter a string:");
        fgets(text,100,stdin);
        if(checkPalindrome(text))
          printf("yes it is palindarome
    ");
        else 
          printf("no it isn't palindarome
    ");
      }
    }

    判断括号是否匹配

    括号有:圆括号,方括号,花括号三种

    下面的程序可以判断括号是否一对一对存在且不乱序。

    #include <stdio.h>
    #include <stdlib.h>
    #define OK 1
    #define ERROR 0
    
    typedef char ElemType;
    typedef int Status;
    
    typedef struct Node{
      ElemType data;
      struct Node * next;
    } Node;
    
    Node * initStack(){
      Node * s = (Node*)malloc(sizeof(Node));
      s->next =NULL;
      return s;
    }
    
    Status Push(Node *s,ElemType e){
      Node * new = (Node*)malloc(sizeof(Node));
      new->data = e;
      new->next = s->next;
      s->next = new;
      return OK;
    }
    
    Status Pop(Node *s,ElemType *e){
      Node * top = s->next;
      *e = top->data;
      s->next = top->next;
      free(top);
      return OK;
    }
    
    Status stackEmpty(Node *s){
      if(!s->next)return OK;
      else return ERROR;
    }
    
    Status checkBrackets(char str[]){
      Node * s = initStack();
      char v;
      int i = 0;
      while(str[i] != '
    '){
        switch(str[i]){
          case '(':
            Push(s,str[i]);
            break;
    
          case '[':
            Push(s,str[i]);
            break;
         
          case '{':
            Push(s,str[i]);
            break;
    
          case ')':
            if(stackEmpty(s))return ERROR;
            else{
               Pop(s,&v);
               if(v == '(')break;
               else return ERROR;
            }
           
          case ']':
            if(stackEmpty(s))return ERROR;
            else{
               Pop(s,&v);
               if(v=='[')break;
               else return ERROR;
            }
    
          case '}':
            if(stackEmpty(s))return ERROR;
            else{
               Pop(s,&v);
               if(v == '{')break;
               else return ERROR;
            }
    
        }
        i++;
      }
    
      if(stackEmpty(s))return OK;
    }
    
    int main(){
      char str[100];
      printf("enter a string:");
      fgets(str,100,stdin);
      if(checkBrackets(str))printf("it is matches all
    ");
      else printf("sorry it didn't matches
    ");
      return 0;
    }
  • 相关阅读:
    分区表的一些操作例子
    MySQL 主从复制
    使用pipeline的函数
    主键字段使用不同数据类型的简单比较
    Flashback Query笔记
    基于Liquibase的数据库持续集成
    MySQL安装
    格式化SYS_GUID()成为标准格式
    Silverlight Treeview 相关操作:加载,保存,索引节点,节点移动,模板节点
    Silverlight TreeView组件的研究[2]
  • 原文地址:https://www.cnblogs.com/demonxian3/p/7136144.html
Copyright © 2011-2022 走看看