数据结构 栈(动态数组)

 

由静态数组实现的栈的的缺陷是明显的,他很容易造成空间的浪费或则空间不足;倘若我们在运行程序的时候是可以知道栈的大小的话,那么动态数组实现的栈无疑使最好的选择;当然,在动态数组中的栈,其构造函数要做的事远比静态的要多,此外还需要一些其他的函数成员:

(1)构造函数:处理动态数据成员的内存分配,并初始化所有的数据成员;

(2)析构函数:释放一个对象中所有的动态分配内存;

(3)复制构造函数:创建对象的复制;

(4)赋值运算符:将一个对象赋予另一个对象;

其他和静态实现的栈基本相同;静态实现的栈参考:http://blog.csdn.net/u013946585/article/details/37612547

1.构造函数:在静态数组的栈中因为数组已经存在,构造函数只需要初始化栈空即可,但在动态数组中则需要先创建一个合法的数组,然后再初始化栈空;

算法描述如下:

(1)检查给定的栈容量是否合法,若非法,则跳转至(6),否则执行(2)
(2)设置capacity为number,执行(3)
(3)分配一个容量为number的数组,由myArray指向其首地址
(4)检查myArray是否为空,若空,则跳转至(6),否则执行(5)
(5)设置myTop为-1;
(6)结束程序

具体代码如下:

#include <iostream>
using namespace std;

typedef int stack_element;


//动态栈的声明部分
class stack
{
	public :
		stack (int number = 128);
		stack (const stack & original);  //复制构造函数
		~stack ();
		const stack & operator = (const stack & RightStack); //赋值运算符
		bool empty () const;
		void push (stack_element item);
		void display (ostream & out) const;
		stack_element top () const;
		void pop ();
	private :
		stack_element * myArray;  //存储元素的动态数组
		int myTop; //栈顶
		int capacity;  //栈的容量
};


//动态栈的实现部分
//构造函数
stack::stack (int number)
{
	if (number < 0)
		return;
	else
	{
		capacity = number;
		myArray = new (nothrow) stack_element[number];
		if (myArray)
			myTop = -1;
		else
			cerr << "new a new myArray Error\n";
	}
}

//stack的复制构造函数的定义
stack::stack (const stack & original) : capacity (original.capacity),myTop(original.myTop)
{
	//为复制创建一个新的数组
	myArray = new (nothrow) stack_element[capacity];
	if (myArray)
		for (int i=0; i<myTop; i++)
			myArray[i] = original.myArray[i];
	else
		cerr << "Inadequate memeory to allocate stack\n";
}

//赋值运算符的定义
const stack & stack::operator = (const stack & RightStack)
{
	if (this != &RightStack)//确定不是自我赋值
	{
		if (RightStack.capacity != capacity) //检查大小是否一样
		{
			delete [] myArray;
			capacity = RightStack.capacity;
			myArray = new (nothrow) stack_element[capacity];
			if (myArray==0)
				cerr << "Inadequate memeory to allocate stack\n";
			else
			{
				myTop = RightStack.myTop;
				for (int i=0; i<myTop; i++)
					myArray[i] = RightStack.myArray[i];
			}
		}
	}
	return *this;
}

stack::~stack ()
{
	delete [] myArray;
}
bool stack::empty () const
{
	return myTop == -1;
}

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

void stack::push (stack_element item)
{
	if (myTop < capacity-1)
	{	myTop++;
		myArray[myTop] = item;
	}
	else
		cerr << "Push Error\n";
}

stack_element stack::top () const
{
	if (myTop != -1)
		return myArray[myTop];
	else
		cerr << "Stack is Empty\n";
}

void stack::pop ()
{
	if (myTop != -1)
		myTop--;
	else
		cerr << "Stack is Empty\n";
}

void print (stack s)
{
	s.display (cout);
}

//动态数组栈的测试部分
int main ()
{
	int cap;
	cout << "Enter stack capacity: \n";
	cin >> cap;
	stack s(cap);
	cout << "stack created. Empty ?\n" << ((s.empty () ? "yes":"no")) << endl;

	cout << "How many elements to add to the stack ?\n";
	int number;
	cin >> number;
	for (int i=0; i<number; i++)
		s.push (i);
	cout << "stack empty ?\n" <<((s.empty () ? "yes":"no")) << endl;
	
	cout << "contents of stack s (via  print ) :\n";
	print (s);
	cout << "\ncheck that stack wasn't modified by print :\n";
	s.display (cout);
	stack u,t;
	t = u = s;
	cout <<"contents of stack t and u after t = u = s :\n";
	cout << "u: \n";print (u);
	cout << "\nt: \n";print (t);
	cout << "\ntop value int t: "<< t.top () << endl;
	while (!t.empty ())
	{
		cout << "Poping t: "<<t.top() <<endl;
		t.pop ();
	}
	cout << "stack t empty ?\n" << ((t.empty () ? "yes":"no")) << endl;
	cout << "stack t top value " <<t.top () <<endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值