Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104528
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: C/C++

2013-10-27 20:45:43

#include
#include


#define STACK_INIT_SIZE 10
#define STACK_INCR_SIZE 2


#define OK 1
#define ERROR 1
#define TURE 1
#define FALSE 0


typedef  int  STATUS; 
typedef  int SELemType;


typedef struct {
SELemType *top;
SELemType *base;
             int stacksize;
}SeqStack;
//++++++++++++++++++++++++++栈的初始化++++++++++++++++++++++++++++++++
int InitStack (SeqStack *S)
{
   S->base = (SELemType *)malloc(STACK_INIT_SIZE * sizeof (SeqStack));
   if(!(*S->base))
   {
  printf("The memory allocation is failed !");
  return ERROR;
   }
   S->top=S->base;
   S->stacksize = STACK_INIT_SIZE;


  return OK;
}
//++++++++++++++++++++++++++获得栈顶元素+++++++++++++++++++++++++++++
int GetTop(SeqStack S,SELemType *e)
{
  if(S.base==S.top)
  {
    printf("The Stack is empty !\n");
return ERROR;
  } 
  *e=*(S.top-1);


 return OK;
}
//+++++++++++++++++++++++++入栈push操作+++++++++++++++++++++++++++++++++
int Push(SeqStack *S, char e)
{
  if((S->top - S->base )== S->stacksize)
  {
    S->base = (SELemType *)malloc((S->stacksize+STACK_INCR_SIZE) * sizeof (SeqStack));
    if(!S->base)
{
printf("The memory allocation is failed !\n");
return ERROR;

 S->top=S->base+S->stacksize;
 S->stacksize+=STACK_INCR_SIZE;
  }
 *S->top++=e;


 return OK;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++出栈pop操作++++++++++++++++++++++++++++++++
int Pop(SeqStack *S,char *e)
{
   if (S->base == S->top)
   {
    printf("The stack is empty! Couldn't pop element from stack!\n ");
   }
   *e = *(--S->top);


   return OK;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++判断堆栈是否为空++++++++++++++++++++++++++++++
int StackIsEmpty(SeqStack S)
{
  if (S.base==S.top)
 return TURE;
  else 
      return FALSE;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++清空堆栈+++++++++++++++++++++++++++++++++++++
STATUS ClearStack(SeqStack *S)
{
    if(S->top == S->base) 
return ERROR;
    S->top = S->base;
    return OK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++括号匹配主函数++++++++++++++++++++++++++++++++++++
STATUS brack_match(SeqStack *S,char *str)
{
int i=0,flag=0;
char e;
while(str[i]!='\0')
{
    switch(str[i]){
          case '(':
 Push(S,str[i]);
 break;
 case '[':
 Push(S,str[i]);
 break;
 case ')':
{
Pop(S,&e);
if(e!='(') 
flag=1;
}
break;
   case ']':
{
Pop(S,&e);
if(e!='[')
flag=1;
}
break;
        default:break;
}
if(flag)
break;
         i++;
}
   if(!flag&&StackIsEmpty(*S))
       printf(" Parenthesis is match!\n");
    else
       printf("Parenthesis isn't match!\n");
   return OK;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void main()
{
char str[100],enter;
SeqStack S;


InitStack(&S);
    printf("Please input the bracks strings:");
    scanf("%s",str);
    brack_match(&S,str);
    getchar();
}
阅读(1031) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~