栈,输入I入栈,输入o出栈,输入10个数看是否正确,如果栈内没有元素出栈就错误,最后栈不空也错误,错哪里了。。
#include<stdio.h>#include <stdlib.h>
#include <math.h>
#define SIZE 50
#define ADD 20
typedef char ElemType ;
typedef struct node{
ElemType *base;
ElemType *top;
int stacksize;
}stack;
void initstack (stack *s)
{
s->base=(ElemType*)malloc (SIZE* sizeof(ElemType));
if ( !s->base )
{
exit(0);
}
s->top=s->base;
s->stacksize=SIZE;
}
void push(stack *s,ElemType e)
{
if(s->top-s->base >= s->stacksize)
{
s->base=(ElemType *)realloc(s->base,(s->stacksize+ADD)*
sizeof (ElemType));
if(!s->base)
{
exit (0);
}
}
*s->top++=e;
}
void pop (stack*s,ElemType *e)
{
if(s->base ==s->top)
{
return;
}
*e=*--(s->top);
}
void empty(stack *s)
{
if(s->top==s->base)
return 1;
return 0;
}
void stacksize1(stack s)
{
return (s.top-s.base);
}
int main()
{
stack s;
char ch;
initstack(&s);
scanf("%c",&ch);
while(ch!='\n')
{
if(ch=='I'||ch=='i')
push(&s,ch);
if(ch=='o'||ch=='O')
{
if(empty (&s))
{
printf("false");
break;
}
else{
pop(&s,&ch);
}
}
scanf("%d",&ch);
}
if(empty(&s))
{
printf("true");
}
else{
printf("false");
}
return 0;
}