顺序栈操作的实现(最详细)

Posted 垚垚是小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序栈操作的实现(最详细)相关的知识,希望对你有一定的参考价值。

  # define _CRT_SECURE_NO_WARNINGS
  #include<stdio.h>
  #include<iostream>
  using namespace std;
# define MAXSIZE 100

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


  //初始化栈
  void StackInit(SqStack* ps) {
	  ps->base = new int[MAXSIZE];
	  if (ps->base==NULL) {
		  cout << "失败" << endl;
	  }
	  else cout << "成功" << endl;
	  ps->stacksize = 100;
	  ps->top = ps->base;
  }
  //入栈
  void Push(SqStack*ps, int  e)
  {
	  if (ps->top - ps->base == ps->stacksize)
		  cout << "栈满" << endl;
	  *(ps->top) = e;
	  (ps->top)++;
  }

  //打印栈的内容
  void printStack(SqStack* s) {
	  while (s->base!=s->top)
	  {
		  cout << *(s->base);
		  (s->base)++;
	  }
  }
  //出栈
  int Pop(SqStack& S){
	  if (S.top == S.base) return 0;    //栈空
	  S.top--;
  }

  //取栈顶元素
  int GetTopStack(SqStack* ps) {
	  return  *(ps->top - 1);

  }

  int main() {
	  SqStack S;
	  StackInit(&S);
	  Push(&S, 1);
	  Push(&S, 1);
	  //Pop(S);
	 cout<< GetTopStack(&S)<<endl;
	  printStack(&S);
	  cout << endl;
  }

以上是关于顺序栈操作的实现(最详细)的主要内容,如果未能解决你的问题,请参考以下文章