链表实现的Stack类

//---------------LStack.h-------------------
#include<iostream>
using namespace std;
#ifndef LSTACK
#define LSTACK

typedef int StackElement;

class Stack
{
public:
	Stack();
	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:
	class Node
	{
	public:
		StackElement data;
		Node *next;
	Node(StackElement value,Node *link=0)
		:data(value),next(link)
	     {}
	};

	typedef Node *NodePointer;
	NodePointer myTop;
};
#endif

//---------------LStack.cpp-------------------
#include<new>
using namespace std;

#include"LStack.h"

Stack::Stack()
	:myTop(0)
    {}

Stack::Stack(const Stack & original)
{
	myTop=0;
	if(!original.empty())
	{
		myTop=new Stack::Node(original.top());
		Stack::NodePointer lastPtr=myTop,
			               origPtr=original.myTop->next;
		while(origPtr!=0)
		{
			lastPtr->next=new Stack::Node(origPtr->data);
			lastPtr=lastPtr->next;
			origPtr=origPtr->next;
		}
	}
}

Stack::~Stack()
{
	Stack::NodePointer currPtr=myTop,
	                   nextPtr;
	while(currPtr!=0)
	{
		nextPtr=currPtr->next;
		delete currPtr;
		currPtr=nextPtr;
    }
}

const Stack & Stack::operator=(const Stack & rightHandSide)
{
	if(this!=&rightHandSide)
	{
		this->~Stack();
		if(rightHandSide.empty())
			myTop=0;
		else
		{
			myTop=new Stack::Node(rightHandSide.top());
			Stack::NodePointer lastPtr=myTop,
				               rhsPtr=rightHandSide.myTop->next;

			while(rhsPtr!=0)
			{
				lastPtr->next=new Stack::Node(rhsPtr->data);
				lastPtr=lastPtr->next;
				rhsPtr=rhsPtr->next;
			}
		}
	}
	return *this;
}

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

void Stack::push(const StackElement & value)
{
	myTop=new Stack::Node(value,myTop);
}

void Stack::display(ostream & out) const
{
	Stack::NodePointer ptr;
	for(ptr=myTop;ptr!=0;ptr=ptr->next)
		out<<ptr->data<<endl;
}

StackElement Stack::top() const
{
	if(!empty())
		return (myTop->data);
	else
	{
		cerr<<"***Stack is empty "
			 "-- returning garbage ***\n";
		StackElement *temp=new(StackElement);
		StackElement garbage=*temp;
		delete temp;
		return garbage;
	}
}

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

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

#include"LStack.h"

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

int main()
{
	Stack s;
	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<<"Top value in t: "<<t.top()<<endl;
	cout<<"Trying to pop t: "<<endl;
	t.pop();
}
//会闪退?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值