一聚教程网:一个值得你收藏的教程网站

热门教程

栈的行编辑

时间:2022-07-02 11:03:26 编辑:袖梨 来源:一聚教程网


#include
#include
#include
#include
#define STACK_INIT_SIZE 50
#define OK 1
#define TRUE 1
#define FALSE  0
#define ERROR 0
#define ESC 27
typedef char ElemType;
typedef struct STACK /*定义栈类型*/
{
  ElemType *base;
  ElemType *top;
  int stacksize;
  int length;
}SqStack,*Stack;
typedef int Status;
void InitStack(Stack *S) /*初始化栈*/
{
  *S=(SqStack *)malloc(sizeof(SqStack));
  (*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
  if(!(*S)->base)exit(-1);
  (*S)->top=(*S)->base;
  (*S)->stacksize=STACK_INIT_SIZE;
  (*S)->length=0;
}
Status DestroyStack(Stack *S) /* 销毁栈*/
{
 free((*S)->base);
 free((*S));
 return OK;
}
void ClearStack(Stack *S)  /*把栈置为空*/
{
  (*S)->top=(*S)->base;
  (*S)->length=0;
}
Status StackEmpty(SqStack S) /*判断栈空否*/
{
  if(S.top==S.base) return TRUE;
  else
    return FALSE;
}
void Push(Stack *S,ElemType e)  /*把数据压入栈*/
{
  if((*S)->top - (*S)->base>=(*S)->stacksize)
   {
     (*S)->base=(ElemType *) realloc((*S)->base,
     ((*S)->stacksize + 10) * sizeof(ElemType));
     if(!(*S)->base)exit(-1);
     (*S)->top=(*S)->base+(*S)->stacksize;
     (*S)->stacksize +=10;
   }
  *((*S)->top++)=e;
  ++(*S)->length;
}
Status Pop(Stack *S,ElemType *e)/*删除栈顶元素*/
{
  if((*S)->top==(*S)->base) return ERROR;
  *e=*((*S)->top-1);
  --(*S)->length;
  (*S)->top--;
  return OK;
}
void LineEdit()
{ Stack S;
 FILE *fp;
 char ch,c,a[50];
 int i,b;
 InitStack(&S);  /* 构造空栈 ,用来当缓冲区*/
 fp=fopen("wenjian.c","w");
 if(!fp)
    { printf(" cannot");exit(0);}
 ch=getchar();
 while(ch!='Q')
     {while(ch!='Q'&&ch!='n')<