[数据结构]Linked_stack

//Link_Stack.h
#pragma once
enum Error_code{success,underflow,overflow};
template<class Stack_entry>
class Linked_Stack
{
public:
	Linked_Stack();
	bool empty()const;
	Error_code pop();
	Error_code top(Stack_entry &item)const;
	Error_code push(const Stack_entry &item);
	~Linked_Stack();
	void operator=(const Linked_Stack &original);
	Linked_Stack(const Stack_entry &item);
	void print();
private:
	struct Node
	{
		Node();
		Node(const Stack_entry &item, Node *add_on = NULL);
		Node *next;
		Stack_entry entry;
	};
	Node *head;
};

template<class Stack_entry>
inline Linked_Stack<Stack_entry>::Node::Node()
{
	next = NULL;
}

template<class Stack_entry>
inline Linked_Stack<Stack_entry>::Node::Node(const Stack_entry & item, Node * add_on)
{
	next = add_on;
	entry = item;
}


//Linked_stack.cpp
#include<iostream>
#include"Link_Stack.h"
using namespace std;

template<class Stack_entry>
Linked_Stack<Stack_entry>::Linked_Stack(){
	head = NULL;
}

template<class Stack_entry>
bool Linked_Stack<Stack_entry>::empty() const
{
	if (head)return true;
	else return false;
}

template<class Stack_entry>
Error_code Linked_Stack<Stack_entry>::pop()
{
	if (!head)return underflow;
	Node *temp = head;
	head = head->next;
	delete temp;
	return success;
}

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

template<class Stack_entry>
Error_code Linked_Stack<Stack_entry>::push(const Stack_entry & item)
{
	head = new Node(item, head);
	return success;
}

template<class Stack_entry>
Linked_Stack<Stack_entry>::~Linked_Stack()
{
	while (head) {
		Node *temp = head;
		head = head->next;
		delete temp;
	}
}

template<class Stack_entry>
void Linked_Stack<Stack_entry>::operator=(const Linked_Stack & original)
{
	Node *temp = original.head;
	Linked_Stack <Stack_entry> middle;
	if (head == original.head)return;
	while (temp) {
		middle.push(temp->entry);
		temp = temp->next;
	}
	while (head) pop();
	temp = middle.head;
	while (temp) {
		push(temp->entry);
		temp = temp->next;
	}
}
template<class Stack_entry>
Linked_Stack<Stack_entry>::Linked_Stack(const Stack_entry & original)
{
	Node *new_head, *new_temp, *original_temp = original.head;
	if (original == NULL) { new_head == NULL; return; }
	new_temp = new_head = new Node(original_temp->entry);
	while (original_temp) {
		original_temp = original_temp->next;
		new_temp->next = new Node(original_temp->entry);
		new_temp = new_temp->next;
	}
}

template<class Stack_entry>
void Linked_Stack<Stack_entry>::print()
{
	Node *temp = head;
	while (temp) {
		cout << temp->entry << ' ';
		temp = temp->next;
	}
	cout << endl;
}
//main.cpp
#include<iostream>
#include"Linked_Stack.cpp"
using namespace std;
void main() {
	cout << "1.push test1." << endl;
	cout << "2.push test2." << endl;
	cout << "3.pop test1." << endl;
	cout << "4.pop test1." << endl;
	cout << "5.make test1=test2." << endl;
	cout << "6.make test2=test1." << endl;
	cout << "7.print test1." << endl;
	cout << "8.print test2." << endl;
	Linked_Stack<int> test1, test2;
	while (1) {
		int key=0;
		
		cin >> key;
		switch (key)
		{
		case 1: {
			int item;
			cout << "please cin item." << endl;
			cin >> item;
			test1.push(item);
			cout << "--------------------------------------------------" << endl;
			break;
		}
		case 2: {
			int item;
			cout << "please cin item." << endl;
			cin >> item;
			test2.push(item);
			cout << "--------------------------------------------------" << endl;
			break;
		}
		case 3:
			test1.pop();
			cout << "--------------------------------------------------" << endl;
			break;
		case 4:
			test2.pop();
			cout << "--------------------------------------------------" << endl;
			break;
		case 5:
			test1 = test2;
			cout << "--------------------------------------------------" << endl;
			break;
		case 6:
			test2 = test1;
			cout << "--------------------------------------------------" << endl;
			break;
		case 7:
			test1.print();
			cout << "--------------------------------------------------" << endl;
			break;
		case 8:
			test2.print();
			cout << "--------------------------------------------------" << endl;
			break;
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值