数据结构与算法设计---链式栈的实现(C++)

数据结构与算法设计---链式栈的实现(C++)

    利用指针结点来实现链式堆栈,最难的部分应该是操作符重载的问题,那里设了几个新变量,还有的是运行环境是VS2013,其他的应该注意指针指向即可,所以不做注释了,有问题可以在评论区留言,有bug的地方欢迎批评指正

#include<iostream>
#include<vector>
using namespace std;

enum Error_code
{
	success, overflow, underflow
};

template<class Node_entry>
struct Node{
	Node_entry entry;
	Node*next;

	Node();
	Node(Node_entry item, Node*add_on = NULL);
};

template<class Stack_entry>
class Stack {
public:
	Stack();
	bool empty() const;
	Error_code push(const Stack_entry& item);
	Error_code pop();
	Error_code top(Stack_entry &item) const;

	~Stack();
	Stack(const Stack &original);
	void operator = (const Stack &original);

private:
	Node<Stack_entry>*top_node;
};

template<class Node_entry>
Node<Node_entry>::Node() {
	next = NULL;
}

template<class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node*add_on = NULL) {
	next = add_on;
	entry = item;
}

template<class Stack_entry>
Stack<Stack_entry>::Stack() {
	top_node = NULL;
}

template<class Stack_entry>
bool Stack<Stack_entry>::empty() const {
	return top_node == NULL;
}

template<class Stack_entry>
Error_code Stack<Stack_entry>::push(const Stack_entry& item) {
	Node<Stack_entry>*new_node = new Node<Stack_entry>(item, top_node);
	if (new_node == NULL) return overflow;
	top_node = new_node;
	return success;
}

template<class Stack_entry>
Error_code Stack<Stack_entry>::pop() {
	if (top_node == NULL) return underflow;
	Node<Stack_entry>*old_node = top_node;
	top_node = top_node->next;
	delete old_node;
	return success;
}

template<class Stack_entry>
Error_code Stack<Stack_entry>::top(Stack_entry &item) const {
	if (top_node == NULL) return underflow;
	item = top_node->entry;
	return success;
}

template<class Stack_entry>
Stack<Stack_entry>::~Stack() {
	while (!empty())
		pop();
}

template<class Stack_entry>
void Stack<Stack_entry>::operator = (const Stack &original) {
	Node<Stack_entry>*new_copy, *new_top, *original_node = original.top_node;
	if (original_node == NULL) new_top = NULL;
	else {
		new_top = new_copy = new Node<Stack_entry>(original_node->entry);
		while (original_node->next != NULL) {
			original_node = original_node->next;
			new_copy->next = new Node<Stack_entry>(original_node->entry);
			new_copy = new_copy->next;
		}
	}
	while (!empty())
		pop();
	top_node = new_top;
}

template<class Stack_entry>
Stack<Stack_entry>::Stack(const Stack &original) {
	*this = original;
}

int main() {
	Stack<int> stack;
	int temp;
	for (int i = 0; i < 10; i++) {
		cin >> temp;
		stack.push(temp);
	}
	cout << stack.empty() << endl;
	Stack<int> stack2 = stack;
	for (int i = 0; i < 10; i++) {
		stack.top(temp);
		cout << temp << endl;
		stack.pop();
	}
	cout << stack.empty() << endl;
	for (int i = 0; i < 10; i++) {
		stack2.top(temp);
		cout << temp << endl;
		stack2.pop();
	}
	cin >> temp;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值