rstack.cpp

来自「在VC下打开SLR1.dsp」· C++ 代码 · 共 60 行

CPP
60
字号
// rstack.cpp: implementation of the rstack class.
//
//////////////////////////////////////////////////////////////////////

#include "rstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

rstack::rstack(int size)
	:m_size(size)
{
	assert(size > 0);
	m_sp = 0;
	m_sstack = new int[size];
	m_estack = new int[size];
}

rstack::~rstack()
{
	delete[] m_sstack;
	delete[] m_estack;
}
void rstack::push(int s, int e)
{
	if (m_sp < m_size)
	{
		m_sstack[m_sp] = s;
		m_estack[m_sp] = e;
		m_sp++;
	}
	else
	{
		printf("stack overflow! sp=%3d", m_sp);
		exit(1);
	}
}
int rstack::pop(int &s, int &e)
{
	if (m_sp > 0)
	{
		m_sp--;
		s = m_sstack[m_sp];
		e = m_estack[m_sp];
		return s;
	}
	else
		return -1;
}

int rstack::top(int &s, int &e)
{
	s = m_sstack[m_sp-1];
	e = m_estack[m_sp-1];
	return m_sstack[m_sp-1];
}

⌨️ 快捷键说明

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