⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack2.cpp

📁 这是我学习数据结构时用过的程序。索性打包传上去。数据结构实验用。
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
//#include<alloc.h>
#include<process.h>


#define mySIZE  100;
#define ENT   10;

#define OK  1
#define ERROR 0
#define OVERFLOW -1

typedef int Status;
typedef char ElemType;
typedef char SElemType;


typedef struct{
  SElemType  *base;
  SElemType  *top;
  int stacksize;
}SqStack;




Status InitStack(SqStack &S){
  S.base=(SElemType *)malloc(100*sizeof(ElemType));
  if(!S.base) exit(OVERFLOW);
  S.top=S.base;
  S.stacksize=mySIZE;
  return OK;
}


Status Push(SqStack &S,SElemType e){
   if (S.top-S.base>=S.stacksize){
     S.base=(ElemType*)realloc(S.base,(S.stacksize+10)*sizeof(ElemType));
     if(!S.base)  exit(OVERFLOW);
     S.top=S.base+S.stacksize;
     S.stacksize+=10;
     }
   *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);
   printf("e=%c",e);
   return OK;
}


main()
{
  SqStack s;
  ElemType e;
  InitStack(s);  
  Push(s,'9');
  GetTop(s,e);
  Push(s,'5');
  Pop(s,e);
  return OK; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -