
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
typedef int SElemType;
struct SqStack{
SElemType *base;
SElemType *top;
int stacksize;
};
#define SqStack struct SqStack
int InitStack(SqStack *S)
{
if ( !(S->base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof(SElemType))))
exit(3);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
int DestroyStack(SqStack *S)
{
free((*S).base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return 1;
}
int ClearStack(SqStack *S)
{
S->top=S->base;
return 1;
}
int StackEmpty(SqStack *S)
{
if((*S).top == (*S).base )
return 1;
else
return 0;
}
int StackLength(SqStack *S)
{
return S->top - S->base;
}
int GetTop(SqStack *S,SElemType *e)
{
if(S->top>S->base)
{
*e=*(S->top - 1);
return 1;
}else
return 0;
}
int Push(SqStack *S,SElemType e)
{
if ( S->top - S->base >= S->stacksize )
{
S->base=(SElemType *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!S->base)
exit(3);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top)++=e;
return 1;
}
int Pop(SqStack *S)
{
if(S->top == S->base)
return 0;
*--(S->top);
return 1;
}
int StackTraverse(SqStack *S,int (*visit)(SElemType))
{
while(S->top > S->base){
visit(*(S->base)++);
}
printf("\n");
return 1;
}
int copy(int c)
{
fputc(c,stdout);
return 1;
}
void LineEdit()
{
SqStack s;
char ch,*c;
InitStack(&s);
printf("please input streams,ctrl+z to quit:\n");
ch=getchar();
while( ch != EOF )
{
while( ch != EOF && ch != '\n')
{
switch(ch)
{
case '#':Pop(&s);
break;
case '@':ClearStack(&s);
break;
default:Push(&s,ch);
}
ch=getchar();
}
StackTraverse(&s,copy);
ClearStack(&s);
if( ch != EOF)
ch = getchar();
}
DestroyStack(&s);
}
int main(){
LineEdit();
}