栈的实现(C++类模板)

本文介绍了如何使用C++实现顺序栈和链式栈的数据结构。顺序栈通过动态数组实现,提供了Push、Pop、GetTop等操作;链式栈利用链表作为底层数据结构,同样支持栈的基本操作。代码中包含了类定义、构造函数、析构函数以及相关成员函数的实现。
摘要由CSDN通过智能技术生成

顺序栈的实现

1.MyStack.h

#ifndef MYSTACK_H_
#define MYSTACK_H_

#include<iostream>

template<class ElementType>
class LinearStack
{
private:
	ElementType* data;
	int length;
	int MaxSiaze;
public:
	inline ElementType& GetElement(int pos)
	{
		return this->data[pos];
	}
	LinearStack(int size);
	~LinearStack();
	bool isEmpty();
	bool isFull();
	int GetLength();
	bool Push(ElementType& e);
	bool Pop(ElementType& e);
	bool GetTop(ElementType& e);
	void Output(std::ostream& os) const;
	friend std::ostream& operator<<(std::ostream& os, LinearStack<ElementType>& stack) 
	{
		stack.Output(os);
		return os;
	}
};

#endif // !MYSTACK_H_


2.MyStack.cpp

#include"MyStack.h"

template<class ElementType>
LinearStack<ElementType>::LinearStack(int size)
{
	this->data = new ElementType[size];
	this->MaxSiaze = size;
	this->length = 0;
}

template<class ElementType>
LinearStack<ElementType>::~LinearStack()
{
	delete[] this->data;
	this->length = 0;
	this->MaxSiaze = 0;
	this->data = nullptr;
}

template<class ElementType>
bool LinearStack<ElementType>::isEmpty()
{
	return this->length == 0;
}

template<class ElementType>
bool LinearStack<ElementType>::isFull()
{
	return this->MaxSiaze == this->length;
}

template<class ElementType>
int LinearStack<ElementType>::GetLength()
{
	return this->length;
}

template<class ElementType>
bool LinearStack<ElementType>::Push(ElementType& e)
{
	if (this->isFull())
	{
		std::cout << "栈已满!" << std::endl;
		return false;
	}
	this->data[length++] = e;
	return true;
}

template<class ElementType>
bool LinearStack<ElementType>::Pop(ElementType& e)
{
	if (this->isEmpty())
	{
		std::cout << "栈空!" << std::endl;
		return false;
	}
	e = this->data[length-1];
	this->length--;
	return true;
}

template<class ElementType>
bool LinearStack<ElementType>::GetTop(ElementType& e)
{
	if (this->isEmpty())
	{
		std::cout << "栈空!" << std::endl;
		return false;
	}
	e = this->data[length];
	return true;
}

template<class ElementType>
void LinearStack<ElementType>::Output(std::ostream& os) const
{
	for (int i = 0; i < length; i++)
		os << this->data[i] << "\t";
}

2.链式栈的实现

1.LinkNode.h

#pragma once

/**
* 该类是链式栈的实现
*/
template<class T>
class LinkStack;

template<class ElementType>
class LinkNode
{
private:
	ElementType data;
	LinkNode<ElementType>* next;

public:
	//将链栈声明为友类
	template<class T>
	friend class LinkStack;

	LinkNode()
	{
		this->next = nullptr;
	}
	ElementType& GetData()
	{
		return this->data;
	}
	LinkNode<ElementType>* GetNext()
	{
		return this->next;
	}
};

2.LinkStack.h

#pragma once
#include<iostream>
#include"LinkList.h"


template<class ElementType>
class LinkStack
{
private:
	LinkNode<ElementType>* top;			//栈顶元素
	int size;
public:
	LinkStack();
	~LinkStack();
	bool isEmpty() const;
	bool Push(ElementType& e);
	bool Pop(ElementType& e);
	bool GetTop(ElementType& e);
	void OutPut(std::ostream& os) const;
	friend std::ostream& operator<<(std::ostream& os, LinkStack<ElementType>& stack)
	{
		stack.OutPut(os);
		return os;
	}
};

3.LinkStack.cpp

#include "LinkStack.h"

template<class ElementType>
inline LinkStack<ElementType>::LinkStack()
{
	//this->top->next = nullptr;
	this->size = 0;
}

template<class ElementType>
LinkStack<ElementType>::~LinkStack()
{
	ElementType e;
	while (top != nullptr)
		this->Pop(e);
}

template<class ElementType>
bool LinkStack<ElementType>::isEmpty() const
{
	return this->size == 0;
}

template<class ElementType>
bool LinkStack<ElementType>::Push(ElementType& e)
{
	LinkNode<ElementType>* new_node = new LinkNode<ElementType>;
	if (new_node == nullptr)
		return false;
	new_node->data = e;
	new_node->next = this->top;
	this->top = new_node;
	this->size++;
	return true;
}

template<class ElementType>
bool LinkStack<ElementType>::Pop(ElementType& e)
{
	if (this->top == nullptr)
		return false;
	LinkNode<ElementType>* temp = new LinkNode<ElementType>;
	if (temp == nullptr)
		return false;
	e = this->top->data;
	temp = this->top;
	this->top = this->top->next;
	this->size--;
	delete temp;
	return true;
}

template<class ElementType>
bool LinkStack<ElementType>::GetTop(ElementType& e)
{
	if (this->isEmpty())
		return false;
	e = this->top->data;
	return true;
}

template<class ElementType>
void LinkStack<ElementType>::OutPut(std::ostream& os) const
{
	LinkNode<ElementType>* p = this->top;
	for (int i = 0; i < this->size; i++)
	{
		os << p->data << "\t";
		p = p->next;
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑暗守护者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值