首页 > 代码库 > 栈的应用实例

栈的应用实例

十进制转二进制

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

#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("\n");
  return OK;
}

int main(){
  int value;
  
  printf("This is binary convertioner\n");
  printf("enter Ctrl + C to stop programe\n");
  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 != \n)
    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\n");
  printf("enter the Ctrl + C to exit progrme\n");
  while(1){
    printf("enter a string:");
    fgets(text,100,stdin);
    if(checkPalindrome(text))
      printf("yes it is palindarome\n");
    else 
      printf("no it isn‘t palindarome\n");
  }
}

判断括号是否匹配

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

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

#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 =http://www.mamicode.com/ 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] != \n){
    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\n");
  else printf("sorry it didn‘t matches\n");
  return 0;
}

 

栈的应用实例