C++中的代码重用(四)=>对于指针栈的一些简单操作

//pointor_stack.h
#ifndef POINTER_STACK_H_
#define POINTER_STACK_H_
#include<iostream>
template<typename Type>
class stack
{
private:
	enum{size=8};
	int stacksize;//栈分配的内存空间存放个数
	Type *item;//需要构造深度复制构造函数和重载赋值运算符
	int top;//栈内元素个数
public:
	stack(int ss = 0);
	stack(const stack &s);
	~stack(){ delete[] item; }
	bool isempty(){ return top == 0; }
	bool isfull(){ return top == stacksize; }
	bool push(const Type &i);
	bool pop(Type &i);
	stack& operator=(const stack &s);
};

/*对于模板类需要将定义和声明放在同一个文件中,因为在编译阶段并不为模板类分配内存*/
template<typename Type>
stack<Type>::stack(int ss) :stacksize(ss), top(0)
{
	item = new Type[stacksize];
}

template<typename Type>
stack<Type>::stack(const stack &s)
{
	stacksize = s.stacksize;
	top = s.top;
	item = new Type[stacksize];
	for (int i = 0; i < top; i++)
		item[i] = s.item[i];
}
template<typename Type>
bool stack<Type>::push(const Type &i)
{
	if (isfull()) return false;
	item[top++] = i;
	return true;
}
template<typename Type>
bool stack<Type>::pop(Type &i)
{
	if (isempty()) return false;
	else
	{
		i = item[--top];//注意这里一定要是item[--top],因为item[top--]==item[top]不存在
		return true;
	}
}
template<typename Type>
stack<Type>& stack<Type>::operator=(const stack &s)//不要忘记stack<Type>
{
	if (this == &s) return *this;
	else
	{
		stacksize = s.stacksize;
		top = s.top;
		delete[] item;
		item = new Type[stacksize];
		for (int i = 0; i < top; i++)
			item[i] = s.item[i];
		return *this;
	}
}
#endif //POINTER_STACK_H_

//main.cpp
/**
*Copyright U+00A9 2018 XXXX. All Right Reserved.
*Filename:
*Author:XXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
#include<cstdlib>
#include<ctime>
#include"windows.h"
#include"pointer_stack.h"
using namespace std;
const int N = 6;
int main()
{
	srand(time(0));
	cout << "please input the size of stack: ";
	int stacksize;
	cin >> stacksize;
	stack<const char *>st(stacksize);//创建指针栈
	const char *in[N] = { " 1.Jack", " 2:Tom", " 3.Jerry",
		                  " 4.Edison", " 5.X-man", " 6.Chinese" };
	const char *out[N];
	int process = 0;
	int innext = 0;
	while (process < N)
	{
		if (st.isempty())
			st.push(in[innext++]);
		else if (st.isfull())
			st.pop(out[process++]);
		else if (rand()%2&&innext<N)
			st.push(in[innext++]);
		else
			st.pop(out[process++]);
	}
	cout << "Result:\n";
	for (int i = 0; i < N; i++)
		cout << out[i] << endl;
	cout << "Done!\n";
  system("pause");
  return 0;
}

程序运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
环境:Windows XP S3、VC++ 6.o 目的:学习C++程序开发语言 使用步骤:下载之后,双击.dsw文件即可打开该示例工程 说明: 在学习《Thinking in C++》一书关于数组声明与定义时,Bruce说如果这样声明一个数组: int b[6] = {0}; Here, the compiler will use the first initializer for the first array element, and then use zero for all the elements without initializers.(意思是说如果这样声明并且定义一个数组,那么编译器会把0赋给第一个数组元素,其它五个元素会赋值0).于是我使用class声明一个类型Test。在这个类有一个成员方法叫getArray(),在该方法使用以上方式声明一个数组,然后返回数组的指针,然后在另一个成员方法showPointerOfArray(int*)接收传过来的int指针,在这个方法操作数组。 但是在运行时没有出现我想要的结果,于是其它的方法测试这样声明方式,却是运行正确的。于是让我很纳闷?带这个问题与本心庄鹏飞老师讨论之后,发现原来我没有搞清楚在C++指针分为指针和堆指针。参见int* Test::getArray()方法关于数组的声明以及本人非常详细的说明,那么我想会给学习C++编程的人员带来收获。 结论:C++不是纯粹的OO语言,这是bruce说的。本人在学习过程确实感觉C++这种语言比Java难得多。不像Java那么直观易学,这可能也就是为什么世界上所有程序员有20%左右的人是Java程序员,而不是C++程序员的原因吧。 另外,本人使用QT的g++编译器编译通过了,因为是使用记事本手写的,所以完全是Java的书写风格^_^ 把它搞成VC++的工程是为了大家方便学习。。。 学习对象:希望编写效率高于Java应用的程序员。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值