当前位置:编程学习 > C#/ASP.NET >>

栈的行编辑


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#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')<
<
补充:asp.net教程,C语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,