数据结构_行编辑

来源:互联网 发布:零基础java就业班 编辑:程序博客网 时间:2024/05/21 09:23
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define STACK_SIZE 100  //储存空间的容量
#define STACK_INC  10   //增加储存空间的容量
#define  OK 1
#define  ERROR 0
typedef int Elme;
typedef struct  
{
Elme *base;
Elme *top;
int size;
}SqStack;
typedef int status;
//新建一个空栈,栈顶和栈低相等就是空栈
status CreatStack(SqStack &S)
{
S.base=(Elme *)malloc(STACK_SIZE*sizeof(Elme));
S.top=S.base;
S.size=STACK_SIZE;
return OK;
}
//进栈
status Push(SqStack &S,char ch)
{
if(S.top-S.base>=S.size)   //如果栈满了,就要扩大
{
S.base=(Elme *)realloc(S.base,(S.size+STACK_INC)*sizeof(Elme));
S.top=S.base+S.size;
S.size+=STACK_INC;
}
*S.top=ch;
S.top+=1;
return OK;
}
//出栈
status Pop(SqStack &S,Elme e) //删除栈顶元素
{
S.top-=1;
*S.top=NULL;
return OK;
}
//检查栈空
status StackEmpty(SqStack S)
{
if(S.base-S.top==0)
return OK;
return ERROR;
}
//清空栈
status ClearStack(SqStack &S)
{
if(!StackEmpty(S)) //不是空栈就清空
{
for(;;)
{
S.top--;
*S.top=NULL;
if(S.top-S.base==0)   //当达到栈顶与栈低指针相等就是空栈
break;
}
}
return OK;
}
//输出栈的元素
void print(SqStack S)
{
for(;S.base<S.top;S.base++)
printf("%s",S.base);
}

void main()
{
printf("请输入你想编辑的对象");
SqStack S;
CreatStack(S);
char ch;
Elme c=1;
ch=getchar();
while(ch!='\n'&&ch!=EOF)
{
switch(ch)
{
case '#': Pop(S,c);break;
case '@':ClearStack(S);break;
default:Push(S,ch);
}
ch=getchar();
}
print(S);
}
原创粉丝点击