1. 程式人生 > >棧的應用實例

棧的應用實例

check ket std tex col 回文 rac clu stat

十進制轉二進制

十進制通過除而取余數得到的二進制,最後需要倒過來展示。

#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 = 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;
}

棧的應用實例