C ++ data structures stack chain (XX)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_32642107/article/details/102733115

Cheetah Wang Xiao quote

Here Insert Picture Description

main.cpp

#include <iostream>
#include "LinkedStack.h"

using namespace std;

int main()
{
	cout << "测试链式栈" << endl;

	LinkedStack<int> s;

	s.Push(10);
	cout << s.Top() << endl;
	s.Push(20);
	cout << s.Top() << endl;
	s.Push(30);
	cout << s.Top() << endl;

	s.Pop();
	cout << s.Top() << endl;
	return 0;
}

Linked Stack.h

#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H

template<class T> class LinkedStack;

template<class T>
class ChainNode
{
	friend class LinkedStack<T>;
private:
	ChainNode(const T& theData, ChainNode *n = 0)
		: data(theData), link(n) {}
	T data;
	ChainNode<T> *link;
};

template<class T>
class LinkedStack
{
public:
	LinkedStack() : top(0) {}
	~LinkedStack() { MakeEmpty();}
	bool IsEmpty() const;
	T& Top() const;
	void Push(const T& e);
	void Pop();
	void MakeEmpty();
private:
	ChainNode<T> *top;
};

template<class T>
bool LinkedStack<T>::IsEmpty() const
{
	return top == 0;
}

template<class T>
void LinkedStack<T>::Push(const T &e)
{
	top = new ChainNode<T>(e,top);
}

template<class T>
T& LinkedStack<T>::Top() const
{
	if(this->IsEmpty())
		throw "Stack is empty.";
	return top->data;
}

template<class T>
void LinkedStack<T>::Pop()
{
	if(this->IsEmpty())
		throw "Stack is empty. Cannot delete.";
	ChainNode<T> *delNode = top;
	top = top->link;
	delete delNode;
}

template<class T>
void LinkedStack<T>::MakeEmpty()
{
	while(!IsEmpty())
		Pop();
}

#endif

Guess you like

Origin blog.csdn.net/qq_32642107/article/details/102733115