链式堆栈实现

#ifndef STACK_LINKED_H
#define STACK_LINKED_H
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;

template<typename T>
struct Node_stack
{
	T entry;
	Node_stack* pNext;
};

template<typename T>
class Stack_linked
{
public:
	Stack_linked();

	// Safeguards
	Stack_linked(const Stack_linked&);
	~Stack_linked();
	Stack_linked& operator = (const Stack_linked&);

	// Stack operations
	void push(const T&);
	void pop();
	T top() const;

	bool empty() const;
	int size() const;

	void clear();
	void display() const;

private:
	Node_stack<T> *pTop;
};

template<typename T>
Stack_linked<T>::Stack_linked()
{
	pTop = NULL;
}

// Copy Constructor
template<typename T>
Stack_linked<T>::Stack_linked(const Stack_linked<T>& ano)
{
	Node_stack<T> *temp, *ano_temp;
	ano_temp = ano.pTop;
	if (ano_temp == NULL)
		pTop = NULL;
	else
	{
		pTop = temp = new Node_stack<T>;
		temp->entry = ano_temp->entry;
		ano_temp = ano_temp->pNext;
		while (ano_temp != NULL)
		{
			temp->pNext = new Node_stack<T>;
			temp = temp->pNext;
			temp->entry = ano_temp->entry;
			ano_temp = ano_temp->pNext;
		}
		temp->pNext = NULL;
	}
}

// Destructor
template<typename T>
Stack_linked<T>::~Stack_linked()
{
	Node_stack<T> *temp;
	while (pTop != NULL)
	{
		pTop = pTop->pNext;
		delete temp;
		temp = pTop;
	}
}

// Equivalent operator overload
template<typename T>
Stack_linked<T>& Stack_linked<T>::operator = (const Stack_linked<T>& ano)
{
	Node_stack<T> *temp, *ano_temp,*new_top;
	ano_temp = ano.pTop;
	if (ano_temp == NULL)
		new_top = NULL;
	else
	{
		new_top = temp = new Node_stack<T>;
		temp->entry = ano_temp->entry;
		ano_temp = ano_temp->pNext;
		while (ano_temp != NULL)
		{
			temp->pNext = new Node_stack<T>;
			temp = temp->pNext;
			temp->entry = ano_temp->entry;
			ano_temp = ano_temp->pNext;
		}
		temp->pNext = NULL;
	}
	clear();
	pTop = new_top;
	return *this;
}

template<typename T>
void Stack_linked<T>::push(const T& item)
{
	Node_stack<T> *temp = new Node_stack<T>;
	temp->entry = item;
	temp->pNext = pTop;
	pTop = temp;
}

template<typename T>
void Stack_linked<T>::pop()
{
	if (pTop == NULL)
		throw runtime_error("Error: Pop an item from an empty stack.");

	Node_stack<T> *temp = pTop;
	pTop = pTop->pNext;
	delete temp;
}

template<typename T>
T Stack_linked<T>::top() const
{
	if (pTop == NULL)
		throw runtime_error("Error: Pop from an empty stack.");

	return pTop->entry;
}

template<typename T>
bool Stack_linked<T>::empty() const
{
	if (pTop == NULL)
		return true;
	return false;
}

template<typename T>
int Stack_linked<T>::size() const
{
	int count = 0;
	Node_stack<T> *temp = pTop;
	while (temp != NULL)
	{
		count++;
		temp = temp->pNext;
	}
	return count;
}

template<typename T>
void Stack_linked<T>::clear()
{
	Node_stack<T> *temp = pTop;
	while (pTop != NULL)
	{
		pTop = pTop->pNext;
		delete temp;
		temp = pTop;
	}
}

template<typename T>
void Stack_linked<T>::display() const
{
	cout << "Top -> ";
	if (pTop == NULL)
	{
		cout << endl;
		return;
	}

	cout << pTop->entry << endl;
	for (Node_stack<T> *temp = pTop->pNext;temp != NULL;temp = temp->pNext)
	{
		cout << "       " << temp->entry << endl;
	}
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值