动态数组实现的Stack类(完整版)

//-------------DStack.h----------------
#include<iostream>

#ifndef DSTACK
#define DSTACK

typedef int StackElement;

class Stack
{
public:
    Stack(int numElements=128);
	Stack(const Stack & original);
	~Stack();
	const Stack & operator=(const Stack &rightHandSide);
	bool empty() const;
	void push(const StackElement & value);
	void display(ostream & out) const;
	StackElement top() const;
	void pop();
private:
	int myTop;
	int myCapacity;
	StackElement *myArray;
	
};
#endif

//------------DStack.cpp------------------
#include<iostream>
#include<cassert>
#include<new>
using namespace std;

#include"DStack.h"

Stack::Stack(int numElements)
{
	assert(numElements>0);
	myCapacity=numElements;

	myArray=new (nothrow) StackElement[myCapacity];
	if(myArray!=0)
		myTop=-1;
	else
	{
		cout<<"Inadequate memory to allocate stack \n";
			exit(1);
	}
}

Stack::Stack(const Stack & original)
	:myCapacity(original.myCapacity),myTop(original.myTop)
{
	myArray=new (nothrow) StackElement[myCapacity];
	if(myArray!=0)
		for(int pos=0;pos<=myTop;pos++)
			myArray[pos]=original.myArray[pos];
	else
	{
		cerr<<"Inadequate memory to allocate stack ***\n";
		exit(1);
	}
}

Stack::~Stack()
{
	delete [] myArray;
}

const Stack & Stack::operator=(const Stack & rightHandSide)
{
	if(this!=&rightHandSide)
	{
		if(myCapacity!=rightHandSide.myCapacity)
		{
			delete [] myArray;

			myCapacity=rightHandSide.myCapacity;
			myArray=new (nothrow) StackElement[myCapacity];
			if(myArray==0)
			{
				cerr<<"*** Inadequate memory ***\n";
				exit(1);
			}
		}
		myTop=rightHandSide.myTop;
		for(int pos=0;pos<=myTop;pos++)
			myArray[pos]=rightHandSide.myArray[pos];
	}
	return *this;
}

bool Stack::empty() const
{
	return (myTop==-1);
}

void Stack::push(const StackElement & value)
{
	if(myTop<myCapacity-1)
	{
		myTop++;
		myArray[myTop]=value;
	}
	else
	{
		cerr<<"***Stack full -- can't add new value ***\n";
		exit(1);
	}
}

void Stack::display(ostream & out) const
{
	for(int i=myTop;i>=0;i--)
		out<<myArray[i]<<endl;
}

StackElement Stack::top() const
{
	if(!empty())
	{
		return (myArray[myTop]);
	}
	else
	{
		cerr<<"***Stack is empty -- returning garbage value ***"
			<<endl;
			StackElement garbage;
		return garbage;
	}
}

void Stack::pop()
{
	if(!empty())
		myTop--;
	else
		cerr<<"*** Stack is empty -- can't remove a value ***\n";
}


//-------------DStack_main.cpp---------------------
#include<iostream>
using namespace std;

#include"DStack.h"

void print(Stack st)
{ st.display(cout); }

int main()
{
	int cap;
	cout<<"Enter stack capacity: ";
	cin>>cap;

	Stack s(cap);
	cout<<"Stack created. Empty? "<<boolalpha<<s.empty()<<endl;

	cout<<"How many elements to add to the stack? ";
	int numItems;
	cin>>numItems;
	for(int i=1;i<=numItems;i++)
		s.push(100*i);
	cout<<"Stack empty? "<<s.empty()<<endl;

	cout<<"Content of stacks s (via print):\n";
	print(s); cout<<endl;
	cout<<"Check that the stack wasn't modified by print:\n";
	s.display(cout); cout<<endl;

	Stack t,u;
	t=u=s;
	cout<<"Content of stacks t and u after t=u=s(via print):\n";
	cout<<"u:\n";print(u);cout<<endl;
	cout<<"t:\n";print(t);cout<<endl;

	cout<<"Top value in t: "<<t.top()<<endl;

	while(!t.empty())
	{
		cout<<"Popping t: "<<t.top()<<endl;
		t.pop();
	}
	cout<<"Stack t empty? "<<t.empty()<<endl;
	cout<<"\nNow try to retrieve top value from t."<<endl;
	cout<<"Top value in t: "<<t.top()<<endl;
	cout<<"\nTrying to pop t: "<<endl;
	t.pop();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值