#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char SElemType;
typedef char Status;
struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
};
Status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)exit(ERROR);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status 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(ERROR);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;
return OK;
}
Status GetTop(SqStack S)
{
SElemType e;
if(S.top==S.base)return ERROR;
e=*(S.top-1);
return e;
}
Status StackLength(SqStack S)
{
return S.top-S.base;
}
Status StackTraverse(SqStack S)
{
SElemType *p = (SElemType *)malloc(sizeof(SElemType));
p = S.top;
if(p==S.base)printf("The Stack is Empty!");
else
{
printf("The Stack is: ");
p--;
while(p!=S.base-1)
{
printf("%c", *p);
p--;
}
}
printf("\n");
return OK;
}
Status DestoryStack(SqStack &S)
{
free(S.base);
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
Status LinkClear(SqStack &S)
{
S.top = S.base;
return OK;
}
void LinkEdit(SqStack &S)
{
char ch;
SElemType e;
ch = getchar();
while(ch != EOF && ch != '\n')
{
switch(ch)
{
case '@':
LinkClear(S);
break;
case '#':
Pop(S,e);
break;
default:
Push(S,ch);
}
ch = getchar();
}
}
int main()
{
SqStack S,T;
SElemType e;
InitStack(S);
InitStack(T);
while(1)
{
LinkEdit(S);
while(StackLength(S))
{
Pop(S,e);
Push(T,e);
}
StackTraverse(T);
LinkClear(T);
}
DestoryStack(T);
DestoryStack(S);
return 0;
}
行编辑程序(栈实现)
最新推荐文章于 2024-03-03 18:52:30 发布