初学编程C++之栈的应用

代码示例:

#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate
{
public:
	Coordinate(int x=0,int y=0);
	//~Coordinate();
	void printCoordinate();
private:
	int m_iX;
	int m_iY;
};
#endif

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

Coordinate::Coordinate(int x,int y)
{
	m_iX=x;
	m_iY=y;
}
void Coordinate::printCoordinate()
{
	cout<<"("<<m_iX<<","<<m_iY<<")"<<endl;

}

#ifndef MYSTACK_H
#define MYSTACK_H
#include"Coordinate.h"
class MyStack
{
public:
	MyStack(int size);//分配内存初始化空间,设定栈容量,栈顶
	~MyStack();//回收栈空间内存
	bool stackEmpty();//判断栈是否为空,为空返回true,非空返回false
	bool stackFull();//判断栈是否为满,为满返回true,非满返回false
	void clearStack();//清空栈
	int stackLength();//已有元素个数
	bool push(Coordinate elem);//元素入栈,栈顶上升
	bool pop(Coordinate &elem);//元素出栈,栈顶下降
	void stackTraverse(bool isFromButtom);//遍历栈中的元素
private:
	
	Coordinate *m_pBuffer;//栈空间指针
	int m_iSize;//栈容量
	int m_iTop;//栈顶,栈中元素个数
};
#endif

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

MyStack::MyStack(int size)
{
	m_iSize=size;
	m_pBuffer=new Coordinate[size];
	m_iTop=0;
}
MyStack::~MyStack()
{
	delete[]m_pBuffer;
	m_pBuffer=0;
}
bool MyStack::stackEmpty()
{
	if(0==m_iTop)
	{
		return true;
	}
	return false;
}
bool MyStack::stackFull()
{
	if(m_iTop==m_iSize)//(m_iTop>=m_iSize)
	{
		return true;
	}
	return false;
}
void MyStack::clearStack()
{
	m_iTop=0;
}
int MyStack::stackLength()
{
	return m_iTop;
}
bool MyStack::push(Coordinate elem)
{
	if(stackFull())
	{
		return false;
	}
	m_pBuffer[m_iTop]=elem;
	m_iTop++;
	return true;
}
//char MyStack::pop ()
//{
//	if(stackEmpty())
//	{
//		throw 1;
//	}
//	else
//	{
//		m_iTop--;
//		return m_pBuffer[m_iTop];
//	}
//}
bool MyStack::pop(Coordinate &elem)
{
	if(stackEmpty())
	{
		return false;
	}
	m_iTop--;
	elem=m_pBuffer[m_iTop];
	return true;
}
void MyStack:: stackTraverse(bool isFromButtom)
{
	if(isFromButtom)
	{
		  for(int i=0;i<m_iTop;i++)//栈底到栈顶遍历
	  {
		//cout<<m_pBuffer[i]<<","<<endl;
		  m_pBuffer[i].printCoordinate();
	  }
	}
	else
	{
		for(int i=m_iTop-1;i>=0;i--)//栈顶到栈底遍历
	  {
		//cout<<m_pBuffer[i]<<","<<endl;
		   m_pBuffer[i].printCoordinate();
	  }
	}
}

//栈机制,栈底和栈顶为栈要素,后进先出
#include<iostream>
#include"stdlib.h"
#include"MyStack.h"
using namespace std;
/*
  栈
  要求:
     1、定义Coordinate坐标类
	 2、改造栈类,使其可以适用于坐标类
	 目的:灵活掌握栈机制,理解抽象数据类型在栈中的应用
	  */ 
int main()
{
	MyStack*pStack=new MyStack(5);
	pStack->push (Coordinate(1,2));//底
	pStack->push (Coordinate(3,4));
	pStack->push (Coordinate(5,6));
	pStack->push (Coordinate(7,8));
	pStack->push (Coordinate(9,0));//顶
	pStack->stackTraverse(true);
	/*cout<<endl;
	char elem=0;
	pStack->pop(elem);
	cout<<elem<<endl;
	cout<<pStack->stackLength()<<endl;
	pStack->stackTraverse(true);
	cout<<endl;
	cout<<endl;*/
	pStack->stackTraverse(false);
	/*char ele =0;
	pStack->pop(ele );
	cout<<ele <<endl;*/
	cout<<pStack->stackLength()<<endl;
	pStack->stackTraverse(true);
	cout<<endl;
	cout<<endl;
	//pStack->clearStack();
	cout<<pStack->stackLength()<<endl;
	if(pStack->stackEmpty())
	{
		cout<<"empty"<<endl;
	}
	if(pStack->stackFull())
	{
		cout<<"full"<<endl;
	}
	delete pStack;
	pStack=NULL;
	system("pause");
	return 0;
}

打印结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值