用顺序栈解决将读入的数按相反的方向解决

本文探讨如何利用顺序栈这一数据结构,将读入的数字序列按相反顺序进行处理。通过介绍顺序栈的基本操作,阐述了将数字逆序的实现过程,帮助读者理解栈的特性及其在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef _SQSTACK_H_
#define _SQSTACK_H_
#include <iostream>
using namespace std;

enum StatusCode{SUCCESS,RANGE_ERROR,OVER_FLOW,UNDER_FLOW};
const int DEFAULT_SIZE=10;
template<class ElemType>
class SqStack
{
	protected:
		int count;
		int maxSize;
		ElemType *elems;

		void Init(int size);
		bool Full()const;
	public:
		SqStack(int size=DEFAULT_SIZE);
		virtual ~SqStack();//注意虚构函数要是虚函数
		bool Empty()const;
		void Clear();
		int Length()const;
		void Traverse(void(* visit)(ElemType &e))const;

		StatusCode Push(const ElemType &e);
		StatusCode Top(ElemType &e)const;
		StatusCode Pop(ElemType &e);

		SqStack(const SqStack<ElemType> &copy);
		SqStack<ElemType>& operator = (const SqStack<ElemType> &copy);
};

template<class ElemType>
SqStack<ElemType>::SqStack(int size)
{
	elems=NULL;
	Init(size);
}

template<class ElemType>
SqStack<ElemType>::~SqStack()
{
	delete []elems;
}

template<class ElemType>
void SqStack<ElemType>::Clear()
{
	count=0;
}

template<class ElemType>
void SqStack<ElemType>::Init(int size)
{
	maxSize=size;
	count=0;
	if(elems!=NULL)
		delete []elems;
	elems=new ElemType[maxSize];
}

template<class ElemType>
bool SqStack<ElemType>::Full()const
{
	return count==maxSize;
}

template<class ElemType>
bool SqStack<ElemType>::Empty()const
{
	return count==0;
}

template<class ElemType>
int SqStack<ElemType>::Length()const
{
	return count;
}

template<class ElemType>
StatusCode SqStack<ElemType>::Pop(ElemType &e)
{
	if(Empty())
		return UNDER_FLOW;
	else
	{
		e=elems[count-1];
		--count;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SqStack<ElemType>::Push(const ElemType &e)
{
	if(Full())
		return OVER_FLOW;
	else
	{
		elems[count++]=e;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode SqStack<ElemType>::Top(ElemType &e)const
{
	if(Empty())
		return UNDER_FLOW;
	else
	{
		e=elems[count-1];
		return SUCCESS;
	}
}

template<class ElemType>
SqStack<ElemType>::SqStack(const SqStack<ElemType> &copy)
{
	elems=NULL;
	Init(copy.maxSize);
	for(int i=0;i<copy.count;i++)
		elems[i]=copy.elems[i];
}

template<class ElemType>
SqStack<ElemType>& SqStack<ElemType>::operator = (const SqStack<ElemType> &copy)
{
	if(this=&copy)
	{
		elems=NULL;
		Init(copy.maxSize);
		for(int i=0;i<copy.count;i++)
			elems[i]=copy.elems[i];
	}
	return *this;
}

#endif


#include "SqStack.h"
#include <iostream>
using namespace std;

template<class ElemType>
void Reserve()
{
    int n,e;
    SqStack<int> tmps;
    cout<<"input an integer:";
    cin>>n;

    while(n<=0)
    {
        cout<<"the integer cann't be negative,please input n again:";
        cin>>n;
    }
    cout<<"please input n integers:";
    for(int i=0;i<n;i++)
    {
        cin>>e;
        tmps.Push(e);
    }
    cout<<"output on the opposite direction:";
    while(!tmps.Empty())
    {
        tmps.Pop(e);
        cout<<e<<" ";
    }
}
void main()
{
    Reserve<int>();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值